|
POST
|
Is it okay to have the gaps? The code can be adjusted if you need to leave those out.
... View more
05-11-2015
12:29 PM
|
0
|
12
|
1456
|
|
POST
|
I updated the code to accommodate missing dates. Attached is the updated code and copy of the output table. I did brief review of the output and there doesn't seem to be any duplicate segments. The only possible issue I see is that it is creating segments that do not have an ItemID. This is because there is a gap in begin and end values. See UniqueID 1446100 for an example.
... View more
05-11-2015
11:54 AM
|
0
|
20
|
1304
|
|
POST
|
Is that a data cleanup issue or does the code need to accommodate missing dates?
... View more
05-11-2015
09:36 AM
|
0
|
23
|
1304
|
|
POST
|
Can you briefly describe your data and the rules between valve type and junction type? What would be the two domains you want and how are they related?
... View more
05-11-2015
09:35 AM
|
1
|
6
|
2024
|
|
POST
|
That error is because some of your records are missing Date values. My code does not have any error handling and assumes there will always be at least one date.
... View more
05-11-2015
09:23 AM
|
0
|
25
|
1441
|
|
POST
|
Sorry, but I don't have that problem with the test data you posted. Please attach a copy of your input table (not a copy/paste) so I can test like for like. See my attachment on this post as an example.
... View more
05-11-2015
08:54 AM
|
0
|
28
|
1441
|
|
POST
|
Are you only looking at file geodatabases? Are they all in the same folder? Please describe your situation with more detail.
... View more
05-08-2015
04:35 PM
|
1
|
0
|
1413
|
|
POST
|
My eyes are starting to cross. Please test this and see if it's accurate. def main():
import arcpy
import os
# Local variables
sourcegdb = r"N:\TechTemp\BlakeT\Work\TEMP.gdb"
table_in = os.path.join(sourcegdb, "SegmentSequence")
# Create output table
arcpy.CreateTable_management(sourcegdb, "SegmentSequence_output")
table_out = os.path.join(sourcegdb, "SegmentSequence_output")
arcpy.AddField_management(table_out, "UniqueID", "LONG")
arcpy.AddField_management(table_out, "Begin", "DOUBLE")
arcpy.AddField_management(table_out, "End", "DOUBLE")
arcpy.AddField_management(table_out, "SegmentItems", "TEXT", "", "", 255)
arcpy.AddField_management(table_out, "MaxDate", "DATE")
# Identify observation groups
sqlprefix = "DISTINCT UniqueID"
sqlpostfix = "ORDER BY UniqueID"
observations = tuple(uid[0]
for uid in arcpy.da.SearchCursor(
table_in,
["UniqueID"],
sql_clause=(sqlprefix, sqlpostfix)
)
)
fields_in = [
"ItemID",
"Begin_Station_Num_m",
"End_Station_Num_m",
"TestDate"
]
fields_out = [
"UniqueID",
"Begin",
"End",
"SegmentItems",
"MaxDate"
]
with arcpy.da.InsertCursor(table_out, fields_out) as i_cursor:
for obsv in observations:
# Read table into dictionary with rows as item: (begin, end, date)
where_clause = "UniqueID = {}".format(obsv)
itemsdict = {r[0]:(r[1], r[2], r[3])
for r in arcpy.da.SearchCursor(
table_in,
fields_in,
where_clause
)
}
# Identify segments
allsegments = [s[0] for s in itemsdict.values()] + [s[1] for s in itemsdict.values()]
segments = tuple(sorted(set(allsegments))) ## creates only unique segments
del allsegments
# Identify items and date in each segment
for i in range(len(segments)-1):
begin = segments
end = segments[i + 1]
seg_itemsdict = {k: v[2]
for k, v in itemsdict.items()
if v[0] <= begin and v[1] >= end
}
# Write segment items to output table
itemstext = str(seg_itemsdict.keys()).strip('[]')
maxsegdate = max(seg_itemsdict.values())
row = (obsv, begin, end, itemstext, maxsegdate)
i_cursor.insertRow(row)
if __name__ == '__main__':
main()
... View more
05-08-2015
03:57 PM
|
0
|
34
|
1441
|
|
POST
|
I just noticed you have a negative number in there. Is that correct? Is that supposed to come before 0? Like this: -9.193 to 0 0 to 32.918 32.918 to 1303.02 ...
... View more
05-08-2015
01:52 PM
|
0
|
1
|
1643
|
|
POST
|
Have you ever heard of the term, "Scope Creep"? Seriously though, are there any other requirements we need to know about before spending time to solve your problem?
... View more
05-08-2015
01:15 PM
|
0
|
1
|
1643
|
|
POST
|
I think you have some errors in the output table you put together here, but I think this modified code should accomplish what you're after. I also rearranged some things so it only opens the insert cursor once. def main():
import arcpy
import os
# Local variables
sourcegdb = r"N:\TechTemp\BlakeT\Work\TEMP.gdb"
table_in = os.path.join(sourcegdb, "SegmentSequence")
# Create output table
arcpy.CreateTable_management(sourcegdb, "SegmentSequence_output")
table_out = os.path.join(sourcegdb, "SegmentSequence_output")
arcpy.AddField_management(table_out, "UniqueID", "SHORT")
arcpy.AddField_management(table_out, "Begin", "SHORT")
arcpy.AddField_management(table_out, "End", "SHORT")
arcpy.AddField_management(table_out, "SegmentItems", "TEXT", field_length=255)
# Identify observation groups
sqlprefix = "DISTINCT UniqueID"
sqlpostfix = "ORDER BY UniqueID"
observations = tuple(uid[0] for uid in arcpy.da.SearchCursor(table_in, ["UniqueID"], sql_clause=(sqlprefix, sqlpostfix)))
fields_out = ["UniqueID", "Begin", "End", "SegmentItems"]
with arcpy.da.InsertCursor(table_out, fields_out) as i_cursor:
for obsv in observations:
# Read item and (begin, end) into dictionary
fields_in = ["ItemID", "Begin", "End"]
where_clause = "UniqueID = {}".format(obsv)
itemsdict = {r[0]:(r[1], r[2]) for r in arcpy.da.SearchCursor(table_in, fields_in, where_clause)}
# Identify segments
allsegments = [i[0] for i in itemsdict.values()] + [i[1] for i in itemsdict.values()]
segments = tuple(sorted(set(allsegments)))
del allsegments
# Identify items in each segment
for enum, seg in enumerate(segments):
try:
begin = seg
end = segments[enum + 1]
except IndexError:
break
seg_items = [k for k, v in itemsdict.items() if v[0] <= begin and v[1] >= end]
# Write segment items to output table
row = (obsv, begin, end, str(seg_items).strip('[]'))
i_cursor.insertRow(row)
if __name__ == '__main__':
main()
... View more
05-08-2015
12:07 PM
|
0
|
51
|
1643
|
|
POST
|
Can you upload a copy of the actual input data table?
... View more
05-08-2015
10:22 AM
|
0
|
0
|
1643
|
|
POST
|
I knew there had to be a numpy solution too; unfortunately, I know squat about it. I also like your dictionary comprehension line with the search cursor better than mine. Thanks for posting, Xander Bakker
... View more
05-08-2015
06:12 AM
|
0
|
2
|
439
|
|
POST
|
This appears to be a duplicate post. The other thread has more responses. Dynamic segmentation with respect to a control factor
... View more
05-08-2015
06:10 AM
|
1
|
0
|
1259
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 12-01-2025 06:19 AM | |
| 1 | 07-31-2025 11:59 AM | |
| 1 | 07-31-2025 09:12 AM | |
| 2 | 06-18-2025 03:00 PM | |
| 1 | 06-18-2025 02:50 PM |