2

I am working on a practice problem, and i have hit a wall. Here is what the question is asking.

Write a script that updates the attribute of SIZE with the values ‘Large’ or ‘Small’, depending on the value of the TOT_ENP (the number of passengers) field. If the number of passengers is larger than 100,000, then it’s a large airport (SIZE = ‘Large’); otherwise, it’s a small airport (SIZE = ‘Small’).

Heres what I have thus far:

import arcpy

arcpy.env.workspace = "E:/lecture08data2"

fc = "airports.shp"
cursor = arcpy.da.UpdateCursor(fc, ["SIZE"], '"SIZE" = \'Large\' and TOT_ENP > 100000 or "SIZE" = \'Small\' and TOT_ENP < 100000')
for row in cursor:
    cursor.updateRow()

del row
del cursor
2
  • 1
    You're trying to calculate the values 'Large' or 'Small' so they wont (or shouldn't) exist yet in the table so no need for the where clause. When you updateRow you need to supply the row like cursor.updateRow(row).. you should have 'SIZE' and 'TOT_ENP' in your cursor so you can calculate it UpdateCursor(fc, ["SIZE","TOT_ENP"].. Mar 24, 2017 at 4:19
  • There should be lots of existing examples quite similar to yours amongst gis.stackexchange.com/… When using arcpy.da.updatecursor it is, I think, best to wrap it in a with ... as cursor: statement.
    – PolyGeo
    Mar 24, 2017 at 4:20

1 Answer 1

1

As you're honest about this being a homework question and have at least had a try at it here's a few tips:

import arcpy
arcpy.env.workspace = "E:/lecture08data2"

fc = "airports.shp"
with arcpy.da.UpdateCursor(fc,['SIZE','TOT_ENP']) as cursor:
    for row in cursor:
        if row[1] > 100000:
            row[0] = 'Large'
        else:
            row[0] = 'Small'
        cursor.updateRow(row)
  1. You need to have both fields in the cursor.
  2. Use a with statement, it gets rid of the need to del the cursor.
  3. A simple if/else statement should get the result of Large or Small.
  4. Update the row in the cursor by supplying the row.

A few things to consider:

  1. What happens if 'SIZE' or 'TOT_ENP' doesn't exist in the table. Use ListFields to check if the fields exist first.
  2. What if 'TOT_ENP' isn't numeric but a string or is blank (NULL). Again ListFields but check the type is numeric. A try/catch block will protect from NULL values and still run to completion.
  3. What if 'SIZE' isn't a text field or is too small to contain the data. ListFields of type 'String' to ensure it's a string field.
  4. What can you do if "airports.shp" isn't found. arcpy.Exists(fc) will let you check the feature class exists before you attempt to modify it.
1
  • 1
    +1 Excellent answer, and great tips and list of things to consider (those are the things I usually forget to work through until something goes wrong, and then slowly I have to add checks for them all)
    – Midavalo
    Mar 24, 2017 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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