|
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
|
2723
|
|
POST
|
Can you upload a copy of the actual input data table?
... View more
05-08-2015
10:22 AM
|
0
|
0
|
2723
|
|
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
|
746
|
|
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
|
1840
|
|
POST
|
I tested it with your sample data, but I don't know how well it will work with more a more complex dataset. Note that the output field that lists all of the ItemID values has to have a limit, which I arbitrarily set to 255. Of course, you'll also need to change the paths and names accordingly. def main():
import arcpy
import os
# Local variables
mygdb = r"N:\TechTemp\BlakeT\Work\TEMP.gdb"
mytable = os.path.join(mygdb, "SegmentSequence")
# Read input table into dictionary
itemsdict = {}
with arcpy.da.SearchCursor(mytable, ["ItemID", "Begin", "End"]) as s_cursor:
for item, begin, end in s_cursor:
itemsdict[item] = (begin, end)
# Identify segments
allsegments = [i[0] for i in itemsdict.values()] + [i[1] for i in itemsdict.values()]
segments = tuple(sorted(set(allsegments)))
del allsegments
# Create output table
arcpy.CreateTable_management(mygdb, "SegmentSequence_output")
mytable_out = os.path.join(mygdb, "SegmentSequence_output")
arcpy.AddField_management(mytable_out, "Begin", "SHORT")
arcpy.AddField_management(mytable_out, "End", "SHORT")
arcpy.AddField_management(mytable_out, "SegmentItems", "TEXT", field_length=255)
# Write segment items to output table
with arcpy.da.InsertCursor(mytable_out, ["Begin", "End", "SegmentItems"]) as i_cursor:
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]
row = (begin, end, str(seg_items).strip('[]'))
i_cursor.insertRow(row)
if __name__ == '__main__':
main()
... View more
05-07-2015
04:47 PM
|
2
|
5
|
2879
|
|
POST
|
So it's kind of like this? 1004__________
1003_____
1002______________________________
1001_________________________
1000_____
0 1 2 3 4 5 6 7 So the segment from 0 to 1 includes items 1000, 1001 1 to 2 includes items 1001, 1002 2 to 3 includes items 1001, 1002 3 to 4 includes items 1001, 1002, 1004 4 to 5 includes items 1001, 1002, 1003, 1004 5 to 6 includes items 1002 6 to 7 includes items 1002 But it looks like you dissolve the segments that have the same items. So 1-2 and 2-3 become 1-3? Same with 5-6 and 6-7 becoming 5-7?
... View more
05-07-2015
03:21 PM
|
0
|
1
|
2879
|
|
POST
|
In the output table, how did you get a Begin range value of 5 when there was no Begin value of 5 in the input table?
... View more
05-07-2015
02:56 PM
|
0
|
3
|
2879
|
|
POST
|
Are all of the the "Item ID" values unique in the source table?
... View more
05-07-2015
02:31 PM
|
0
|
1
|
2879
|
|
POST
|
There's no easy, built-in way to do this. Have you considered using Subtypes? You can have different default field values based on whatever is chosen for the subtype.
... View more
05-06-2015
09:53 AM
|
0
|
0
|
4866
|
|
POST
|
Check out the Intersect Help Topic, there are some helpful Python code examples at the bottom. The function can take all three of your output files as a list for input.
... View more
05-05-2015
10:48 AM
|
0
|
0
|
1485
|
|
POST
|
Sorry, here's the help topic for ArcGIS Pro The in_memory workspace—ArcGIS Pro
... View more
05-05-2015
08:38 AM
|
1
|
0
|
1595
|
|
POST
|
I think symbolizing your polygons by population with a fill using a graduated color ramp is the easiest way to visually identify which feature is greater or less than the other. Could you describe the overall goal here? Maybe there is a different way to approach your problem.
... View more
05-04-2015
10:50 AM
|
1
|
0
|
1882
|
|
POST
|
I typically use the method Tim Witt described to just print whatever I want to the Python Interpreter window. If you're using Esri geoprocessing tools, you can also print the original Esri messages with GetMessages (arcpy).
... View more
05-04-2015
10:31 AM
|
0
|
0
|
1102
|
|
POST
|
ArcGIS Help 10.1 - Using in-memory workspace Just make sure you also delete your in-memory workspace afterwards to keep things clean.
... View more
05-04-2015
10:24 AM
|
1
|
2
|
1595
|
|
POST
|
If you like, post the code you used and we can help you debug it. I just tested my logic and it worked in a file geodatabase. Here is updated code that is a little more flexible. def main():
import arcpy
import os
temp_gdb = r"N:\TechTemp\BlakeT\Work\TEMP.gdb"
fc = os.path.join(temp_gdb, "TEMP_Line")
sortFieldName = "Note1"
seqFieldName = "SeqID"
# Step 1
existingFields = [field.name for field in arcpy.ListFields(fc)]
if seqFieldName not in existingFields:
arcpy.AddField_management(fc, seqFieldName, "SHORT")
# Step 2
cursorFields = [sortFieldName, seqFieldName]
sql_postfix = 'ORDER BY {}'.format(sortFieldName)
with arcpy.da.UpdateCursor(fc, cursorFields, sql_clause=(None, sql_postfix)) as u_cursor:
# Step 3
for seqid, row in enumerate(u_cursor, start=1):
# Step 4
row[1] = seqid
u_cursor.updateRow(row)
if __name__ == '__main__':
main()
... View more
05-04-2015
09:44 AM
|
1
|
2
|
6535
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 10-23-2025 03:53 PM | |
| 1 | 04-28-2026 07:25 AM | |
| 1 | 03-19-2026 08:59 AM | |
| 1 | 02-12-2026 01:37 PM | |
| 1 | 12-01-2025 06:19 AM |