Calculating field in ArcGIS Desktop using ArcPy cursor?
Tested using some dummy data and works fine. There were numerous logic problems such as referencing uninitialized variables, using field names instead of field values in calculations, incorrect arguments for setValue
, etc.
import arcpy
inputFC = arcpy.GetParameterAsText(0) # Input feature class
timeField = arcpy.GetParameterAsText(1) # Name of time field in input feature class
speedField = arcpy.GetParameterAsText(2) # Name of speed field in input feature class
weight = float(arcpy.GetParameterAsText(3)) # Input weight value in kg
arcpy.AddField_management(inputFC, 'Calories', 'LONG') #create calorie field
rows = arcpy.UpdateCursor(inputFC)
for row in rows:
time = row.getValue(timeField)
speed = row.getValue(speedField)
calories = time * (speed * 3.5 * weight)/200
row.setValue('Calories', calories)
rows.updateRow(row)
del row, rows