AnsweredAssumed Answered

Ranking values by shape area using ArcPy

Question asked by master.florin on Apr 29, 2015
Latest reply on Apr 29, 2015 by master.florin

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?

Outcomes