6

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
  • 1
    Could you fix your indentation in the script to confirm that you have that correct because as it appears now, the script won't work at all.
    – user681
    Commented Oct 29, 2011 at 12:01
  • Indentation is key in Python. The row del row, rows should not be indented in the code as written. Right now you're deleting the cursor in the first iteration.
    – blah238
    Commented Oct 31, 2011 at 5:32

2 Answers 2

3

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
8
  • 1
    I tested the script using some dummy data and it works fine. Please explain the downvote.
    – blah238
    Commented 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
    Commented 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
    Commented 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
    Commented 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
    Commented Oct 31, 2011 at 3:36
1

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
  • -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. :p
    – blah238
    Commented Oct 29, 2011 at 21:56

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.