AnsweredAssumed Answered

Creating multiple fields with ranks using arcpy

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

I have a field named "arrondissement" (district), which contains as attributes integers from 1 to 20. My task is to create a column for each arrondissement with the ranks (short integers) relative to the shape area for the connecting areas to voting stations of the same district.

 

I hope it makes sense, not really sure how to do it to be honest I'm lost at the moment. Besides adding a field, perhaps multiple ones with a loop by using ListFields and AddField, I don't really know what else to do.

 

EDIT:

While figuring out what I have to do, practically creating a field for each arrondise, and in each, rank each FID (which are part of the respective arrondise) by the size of its shape area.

 

import arcpy

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

fc = "zones_rattachement.shp"

try:
    fieldRoot = "RANG_R"
    for counter in range(1,21):
        arcpy.AddField_management(fc, fieldRoot + str(counter),'SHORT')

    
    size_rank = 1
    numlist = list(range(1,21))
    for num in numlist:
        arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", "arrondisse = '%c'")
        rows = arcpy.UpdateCursor(fc, sort_fields="shape_area D")
        for row in rows:
            row.setValue("RANG_R1", size_rank)
            size_rank += 1
            rows.updateRow(row)

            
except:
    arcpy.GetMessages()

 

Message was edited by: Florin-Daniel Cioloboc

 

Message was edited by: Florin-Daniel Cioloboc

Outcomes