Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
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. –  Dan Patterson Oct 29 '11 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 Oct 31 '11 at 5:32
add comment

2 Answers 2

up vote 2 down vote accepted

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
share|improve this answer
1  
I tested the script using some dummy data and it works fine. Please explain the downvote. –  blah238 Oct 30 '11 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 Oct 31 '11 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 Oct 31 '11 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 Oct 31 '11 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 Oct 31 '11 at 3:36
show 3 more comments

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.

best,

share|improve this answer
    
-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 Oct 29 '11 at 21:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.