I have a shapefile that I am trying to sort a specific field but for some weird reason it changing the shape of the features, . The following does short the field I want but it is making the features bigger. I am not sure why this is happening?
BPs ="BPs1"
data = []
with arcpy.da.SearchCursor(BPs,"*") as cursor:
for row in cursor:
data.append(row)
data.sort(key=lambda tup: tup[21]) # sorts based on the 21st field
count = 0
with arcpy.da.UpdateCursor(BPs,"*") as cursor:
for row in cursor:
row = data[count]
count += 1
cursor.updateRow(row)
Solved! Go to Solution.
You can also sort within the cursor using the sql clause so you could probably avoid having to create that sort list,
Edited: but it lookes liek you are trying to created a sorted featureclass I'd probably create a new featureclass instead of updating the old.
with arcpy.da.UpdateCursor(BPs,fields, sql_clause=(None, ' ORDER BY ' + field + ' DESC'), where_clause=(field + ' IS NOT NULL')) as cursor:
count=0
for row in cursor:
row[0] = count # or other calc based on what you need.
count += 1
cursor.updateRow(row)
or to create the sorted list:
data = [row for row in with arcpy.da.SearchCursor(BPs,"*", sql_clause=(None, ' ORDER BY ' + field + ' DESC'), where_clause=(field + ' IS NOT NULL'))]
Can you elaborate on "it is making features bigger?" And can you give an example?
The first image is what the actual feature should be but after I run the sort I post it sorts the features but changes the shape, makes them bigger.
no
Did you try Sort (Data Management) instead?
I was trying to use the arcpy.da.UpdateCursor because I need to add an increment to the features that are sorted.
I don't know if this would cause your issues but I would recommend specifying the field names (searchcursor and updatecursor docs recommend this) and using SHAPE@ for the geometry because it returns a geometry object which is less likely to get rounded or manipulated.
So above your SearchCursor I would add a line:
fields = [f.name for f in arcpy.ListFields(BPs)]
fields = fields - ['Shape'] + ['SHAPE@']
And then in your SearchCursor and UpdateCursor, pass the fields variable instead of "*"
You can also sort within the cursor using the sql clause so you could probably avoid having to create that sort list,
Edited: but it lookes liek you are trying to created a sorted featureclass I'd probably create a new featureclass instead of updating the old.
with arcpy.da.UpdateCursor(BPs,fields, sql_clause=(None, ' ORDER BY ' + field + ' DESC'), where_clause=(field + ' IS NOT NULL')) as cursor:
count=0
for row in cursor:
row[0] = count # or other calc based on what you need.
count += 1
cursor.updateRow(row)
or to create the sorted list:
data = [row for row in with arcpy.da.SearchCursor(BPs,"*", sql_clause=(None, ' ORDER BY ' + field + ' DESC'), where_clause=(field + ' IS NOT NULL'))]