0

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?

1

Please check the syntax of the UpdateCursor, your parameters are not given in the right order.

If you skip the where_clause, spatial_reference and field parameters, write:

rows = arcpy.UpdateCursor(fc, sort_fields="shape_area D")

Also the pop_rank variable you increment should be size_rank instead.

EDIT:

And "INT" is not a valid field type, use "SHORT" instead, see the help page for Add Field.

1

Here are a couple of things you can try.

First, change the name of your shapefile so that it does not have any "-" characters, if you want you can replace them with underscores. Second put the shapefile in a directory path that does not have any spaces in any of the folder names. Finally, you can also try to change the name of the shapefile so that it is only eight characters long.

I don't know if these things will help but I have run into similar issues in the past when working with shapefiles. In general I try to avoid working with them and use geodatabases instead.

  • I'm pretty sure that wouldn't have solved the problem, but your suggestions are very good on a longer term basis, and I'll keep them in mind. Indeed, geodatabase is the way to go, this is more or less part of an assignment. Many thanks! – Geosphere Apr 29 '15 at 8:18
0

I've made the modifications you suggested but it's still not working, not getting anything. Quite odd. I even made some additional modifications because it's not even creating my field:

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()
  • 1
    If you comment the answer, put your message as comment under the answer, not as a new question. You can also edit your question with new elements. See also my updated answer. – GISGe Apr 29 '15 at 8:05
  • Good idea, I couldn't find the edit button to add the code. I any case, not the column is created but with 0 as a value for each. – Geosphere Apr 29 '15 at 8:09
  • Solved it, "fc" shouldn't have been there in rows anymore after my last modification. Now it's working. Many thanks. – Geosphere Apr 29 '15 at 8:12
  • 1
    Glad I coulp help! – GISGe Apr 29 '15 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.