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
Solved! Go to Solution.
Then you will need to add .shp to the end of your feature class name.
fc = "zones-de-rattachement-des-bureaux-de-vote-en-2014.shp"
Also, your field name is too long, they need to be 10 or fewer. Since you are going to be tacking on up to two characters to the end, you will need to have a field name 8 characters or fewer.
How about
import arcpy fc = 'yourFeatureClass' #insert your feature class here fieldRoot = 'RANK' #root name of the field for counter in range(1,20): arcpy.AddField_management(fc, fieldRoot + str(counter), 'LONG')
From what I can read, it should create 20 columns with a different name, not sure why in my case it's not working.
import arcpy arcpy.env.workspace = "D:/M1 Geomatique/Programmation II/Dossier" fc = "zones-de-rattachement-des-bureaux-de-vote-en-2014" try: fieldRoot = "RANG_RAPPORT" for counter in range(1,20): arcpy.AddField_management(fc, fieldRoot + str(counter),'LONG') except: arcpy.GetMessages()
What type of data are you working with? That is a very long feature class name, and I tend to avoid dashes in my feature class names because it has caused me problems in the past.
If you are using a file geodatabase doesn't your workspace variable need to have the .gdb extension?
arcpy.env.workspace = "D:/M1 Geomatique/Programmation II/Dossier.gdb"
Or if you are working with a shapefile you are missing the .shp extension in your fc variable and the field name is probably too long.
I'm working with a shapefile, not with a geodatabase, in that case, I omitted the .shp extension.
Then you will need to add .shp to the end of your feature class name.
fc = "zones-de-rattachement-des-bureaux-de-vote-en-2014.shp"
Also, your field name is too long, they need to be 10 or fewer. Since you are going to be tacking on up to two characters to the end, you will need to have a field name 8 characters or fewer.
Done, it worked, now I have a script that actually creates 20 fields. I've also modified the name of .shp, as it was just too long.
Interesting solution, I think I ran into it yesterday but if:
Note: dBase (and shapefiles) does not support ORDER BY as used above by arcpy.da.UpdateCursor’s sql_clause argument.
is it possible, how to go around it, what can substitute the SQL clause? I guess, sorted() would work?
Then don't use shapefiles. It's pretty simple to migrate your data to a File Geodatabase, a scratch workspace, or I'd even consider just adding the source data you intend to process to in_memory feature class if possible. You won't have to deal with shapefile limitations.