I'm trying to calculate a field based on the values of others in the same layer in a calculation. I can't work out why its not working. I've even cut everything down to just be based on one field and even that won't work, so frustrating!
This is based on one field
rows = arcpy.UpdateCursor(inputFC)
for row in rows:
val = row.getValue(Infield1)
row.setValue(outfld , val)
rows.updateRow(row)
del rows, row
arcpy.AddMessage("Completed")
this is the final script. Its meant to calculate calories based on minutes, speed and weight (weight as a number you input rather than a field) import sys, arcpy, math
inputFC = arcpy.GetParameterAsText(0) # Input feature class
timeField = arcpy.GetParameterAsText(1) # Input time in minutes
speedField = arcpy.GetParameterAsText(2) # Input speed
SlopeField = arcpy.GetParameterAsText(3) # Input Slope
weight = float(arcpy.GetParameterAsText(4)) #input weight 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)
slope = row.getValue(SlopeField)
calories = time * (speed * 3.5 * weight)/200 * slope
row.setValue('Calories', calories)
rows.updateRow(row)
del row, rows
this says completed but the calories field ends up being null
2 Answers 2
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
-
1I tested the script using some dummy data and it works fine. Please explain the downvote.blah238– blah2382011年10月30日 20:11:11 +00:00Commented Oct 30, 2011 at 20:11
-
Not sure the reason for the downvote but this works, thanks. The only problem is it works if there is a weight field, but I wanted to insert weight as a parameter value without having to create a weight field.Nevu– Nevu2011年10月31日 02:38:44 +00:00Commented Oct 31, 2011 at 2:38
-
Easy enough to fix that, just be sure to convert it to the right numeric data type and skip the
getValue
call. Your comments and variable names should clarify whether it's a field or an input value being called for.blah238– blah2382011年10月31日 02:59:06 +00:00Commented Oct 31, 2011 at 2:59 -
Edited the script to take the weight value parameter instead of a field name. It's converted to float, you could also use
int(arcpy.GetParameterAsText(3))
to convert to an integer.blah238– blah2382011年10月31日 03:03:18 +00:00Commented Oct 31, 2011 at 3:03 -
thanks! it completes, but doesn't seem to be using the weight number I enter. Is it meant to be set as "double" in parameters?Nevu– Nevu2011年10月31日 03:36:43 +00:00Commented Oct 31, 2011 at 3:36
One solution is to use the field calculator with the logic explained in this question that I asked. Nested "if" statements in ArcGIS Field Calculator
What is the error that you currently get
I think Infield1 * (Infield2 * 3.5 * Infield3)/200
should be
calories=Infield1 * (Infield2 * 3.5 * Infield3)/200
as otherwise it seems you are running a calculation but not storing it in a variable.
-
-1 because you can't multiply strings (the field name variables Infield1, Infield2, and Infield3). You have to get the fields' values first and then multiply those. Also using Field Calculator in Python gives me the creeps. :pblah238– blah2382011年10月29日 21:56:46 +00:00Commented Oct 29, 2011 at 21:56
Explore related questions
See similar questions with these tags.
del row, rows
should not be indented in the code as written. Right now you're deleting the cursor in the first iteration.