AnsweredAssumed Answered

ArcPy Field Calculation within Loop

Question asked by pmckinneyccpa on Mar 31, 2016
Latest reply on Apr 1, 2016 by pmckinneyccpa

I am writing an ArcPy script that will run from a tool with user inputs.  The user will select an input layer and output name/directory.  I have my current code below.  The tool will then do the following:

 

  1. Loop through each record in the input layer and do the following within each loop cycle:
    1. clip a census blocks layer by the input feature
    2. Create a field in the output clip layer to hold an area ratio value
    3. Create a field in the output clip layer to hold an estimated population value
    4. Perform a field calculation between two fields in the output clip layer to produce the area ratio (new area divided into original area)
    5. Perform a field calculation bewteen two fields in the output clip layer to produce the estimated population (area ratio value calculated in #4 mulitplied by the population field)

 

Let's say the input layer contains three records (1500-ft, 2000-ft, and 2500-ft).  There would be three output layers that are clipped census block layers based upon the records from the input, each with estimated census populations based upon the distances from the input feature.  The layers would have names like Output_ft_1500, Output_ft_2000, and Output_ft_2500.

 

My code can do everything except complete the field calculations.  I'm not sure if I'm missing something simple with the syntax for a SQL expression using fields or what.

 

Please let me know if you have any questions. I tried to explain things as best as I could.

 

Here is the error I get:

 

Traceback (most recent call last):

  File "F:\Scripts\ArcGIS Geoprocessing\SARA Tool\EstimateCensusPopulation.py", line 73, in <module>

    fieldExpression = newArea / orgArea

TypeError: unsupported operand type(s) for /: 'Field' and 'Field'

 

Failed to execute (EstimateCensusPopulation).

 

----------------

# Import arcpy
import arcpy

#Set workspace

# Sara Facility
sara = arcpy.GetParameterAsText(0)

# Clip Feature - U.S. Census Blocks
censusBlocks = r'\\CCPASR07\ncgs$\Scripts\ArcGIS Geoprocessing\SARA Tool\SARA_Tool_DEV.gdb\CensusBlocks_2010'

# Output
output = arcpy.GetParameterAsText(1)


# Search cursor
cursor = arcpy.SearchCursor(sara)

for row in cursor:
  
    # Clip features
    feat = row.Shape
  
    # Append buffer distance and units to name
    # Buffer distance
    buffDist = str(int(row.BUFFDIST))
  
    # Buffer units to name
    buffUnits = row.UNITS
  
    # Appended output name variable
    buffAppend = '_' + buffUnits + '_' + buffDist


    # Execute clip tool on each row
    newInput = arcpy.Clip_analysis(censusBlocks, feat, output + buffAppend)

    # Add message that Clip is completed
    arcpy.AddMessage('Feature Clip operation completed')

    # Add field to hold clip area to original area ratio
    areaRatioFieldName = 'AREARATIO'
    areaRatioFieldType = 'DOUBLE'
    # Execut tool
    arcpy.AddField_management(newInput, areaRatioFieldName, areaRatioFieldType)

    # Add message that Area Ratio Field has been added
    arcpy.AddMessage('Area Ratio field added')

    # Add field to hold estimated population
    estPopFieldName = 'ESTPOP'
    estPopFieldType = 'LONG'
    # Execut tool
    arcpy.AddField_management(newInput, estPopFieldName, estPopFieldType)

    # Add message that Estimated Population Field has been added
    arcpy.AddMessage('Estimated Population field added')

    ### Code does not work after this point ###
    areaInField = arcpy.ListFields(newInput, 'AREARATIO')[0]
    newArea = arcpy.ListFields(newInput, 'Shape_Area')[0]
    orgArea = arcpy.ListFields(newInput, 'ORAREA')[0]
    fieldExpression = newArea / orgArea
    arcpy.CalculateField_management(newInput, areaInField, fieldExpression, 'PYTHON_9.3')

del cursor

Outcomes