Sorting Issue

610
6
Jump to solution
10-26-2021 11:18 AM
TonyAlmeida
Occasional Contributor

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)

 

0 Kudos
1 Solution

Accepted Solutions
JeffK
by MVP Regular Contributor
MVP Regular Contributor

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'))]

 

View solution in original post

6 Replies
JoshuaBixby
MVP Esteemed Contributor

Can you elaborate on "it is making features bigger?"  And can you give an example?

0 Kudos
TonyAlmeida
Occasional Contributor

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.

 

no2.png1.png

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

Did you try Sort (Data Management) instead?



Think Location
0 Kudos
TonyAlmeida
Occasional Contributor

I was trying to use the arcpy.da.UpdateCursor because I need to add an increment to the features that are sorted.

0 Kudos
JoshKalovGIS
New Contributor III

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 "*"

 

JeffK
by MVP Regular Contributor
MVP Regular Contributor

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'))]