Select to view content in your preferred language

Creating multiple fields with ranks using arcpy

5070
10
Jump to solution
04-29-2015 06:47 AM
deleted-user-3rTxRfVTcNm-
New Contributor III

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

Tags (4)
0 Kudos
10 Replies
deleted-user-3rTxRfVTcNm-
New Contributor III

Trying to rank rows which are part of a certain arrondise, in terms of size of shape_area. To increase visibility I updated the first post.

0 Kudos