1

I'm trying to run a for loop to calculate acreage percentages for a shapefile. I've converted field1 and field2 to float. I'm having trouble executing the CalculateField tool using the updateCursor. I want values where the area is greater than 0.000. I would also, eventually, like to run a more complex SQL query than only calculates percentages for a specified date range, values greater than 0.000 and values that don't have a null area. Is it possible to run multiple queries using the UpdateCursor?

Here is the part of the code I'm using to execute the calculatefield tool.

import arcpy
from arcpy import env
env.workspace = "X:/UserFolders/AnneGilligan/WC_74_Testing"
fc= "Export_Output_3.shp"
rows= arcpy.da.UpdateCursor(fc, '"Area" > 0.000')
newfield="ac_std"
field1="!acres!"
field2="!Area!"
expression1="(!acres!/!Area!) * 100" #standard calculation
expression2="(!Area!)/!acres!) * 100" #standard calculation
expression_type="PYTHON_9.3"
code_block1=""
code_block2=""
for row in rows:
    if field1 < field2:
        arcpy.CalculateField_management(fc,
                            newfield,
                            expression1,
                            expression_type,
                            code_block1)
    else:
        arcpy.CalculateField_management(fc,
                            newfield,
                            expression2,
                            expression_type,
                            code_block2)
    rows.UpdateCursor(row)
del row
del rows
  • The answer has a point, it is redundant to use both. In fact this condition can be tested and calculated in one line with Calculate Field tool with Python parser. – fatih_dur Jul 8 '18 at 1:54
4

In answer to your question, yes, the UpdateCursor can do as many queries as you'd like. If you want to operate on a subset of your shapefile, use the where_clause argument in the cursor. Check the documentation for more information.

There are a few issues with your code, but primarily you are using the calculate field tool to perform an operation on each individual cell, when it is intended to be used on an entire field (all values) or a selection from that field. I suggest that you stay away from the calculate field tool, and instead use only the UpdateCursor. This syntax should get you up and running:

arcpy.AddField_management(fc, "ac_std", "DOUBLE")
with arcpy.da.UpdateCursor(fc, ["ac_std","acres","SHAPE@AREA"]) as cursor:
    for row in cursor:
        if row[2] > 0:
            if row[1] < row[2]:
                row[0] = row[1] / row[2] * 100
                cursor.updateRow(row)
            else:  # maybe put a condition here using elif?
                row[0] = row[2] / row[1] * 100  # Double check that 'acres' is never 0.
                cursor.updateRow(row)

As you can imagine, you can write as many operations as you'd like for each row, making multiple queries very simple.

0

I would simply use Field Calculator after selecting records satisfying this condition; Area > 0.

Then the expression below should work:

(!acres!/!Area!) * 100 if !Area! > !acres! else (!Area!)/!acres!) * 100

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.