Ranking values by shape area using ArcPy

2579
2
Jump to solution
04-29-2015 12:10 AM
deleted-user-3rTxRfVTcNm-
New Contributor III

I'm trying to make a script that performs the following: it creates a new field that will hold values based on size of the shape area (from 1 to end of table), basically ranking them.

I have a few ideas to do it, and managed to write a script, however it is not working, no errors, nothing.

import arcpy

fc = "D:/M1 Geomatique/Programmation II/Dossier/zones-de-rattachement-des-bureaux-de-vote-en-2014.shp"

try:
    arcpy.AddField_management(fc, "RANG", "INT")
    size_rank = 1
    rows = arcpy.UpdateCursor(fc, ["shape_area"], "D")
    for row in rows:
        row.setValue("RANG", size_rank)
        pop_rank += 1
        rows.updateRow(row)

except:
    arcpy.GetMessages()

Another idea would be to somehow adapt this code, but in any case it is mentioned that shapefiles do not support ORDER BY as used above by arcpy.da.UpdateCursor’s sql_clause argument.

One other idea, to use a list (ListFeatureClasses probably) to get all values, sort them by size, make a loop that takes each value and places a number based on their size. This one seems a bit complicated and I don't have a clear image on how to get through with it.

What would you recommend me to do?

0 Kudos
1 Solution

Accepted Solutions
deleted-user-3rTxRfVTcNm-
New Contributor III

Problem solved.

"INT" isn't a proper value for AddField, and then again fc shouldn't be there anymore after my last modification.

View solution in original post

0 Kudos
2 Replies
deleted-user-3rTxRfVTcNm-
New Contributor III

So far, I've done some corrections to it but still not working, not even creation of a new field which I find it odd.

import arcpy

arcpy.env.workspace = "D:/M1 Geomatique/Programmation II/Dossier"

try:
    arcpy.AddField_management("zones-de-rattachement-des-bureaux-de-vote-en-2014", "RANG", "INT")
    size_rank = 1
    rows = arcpy.UpdateCursor(fc, sort_fields="shape_area D")
    for row in rows:
        row.setValue("RANG", size_rank)
        size_rank += 1
        rows.updateRow(row)

except:
    arcpy.GetMessages()
0 Kudos
deleted-user-3rTxRfVTcNm-
New Contributor III

Problem solved.

"INT" isn't a proper value for AddField, and then again fc shouldn't be there anymore after my last modification.

0 Kudos