Select to view content in your preferred language

python script to calculate field values used to work - now it doesn't.

938
5
05-27-2010 11:03 AM
JoshuaChan
Deactivated User
I have a python script that adds a hectares field and then calculates its
value using the Area field. This worked fine last week but for some
reason it gives null values this week.

code :
# final resultant
gp.Identity_analysis (interimFC, "THLB_Lyr", finalFC )

# Getting the name of the area field for our resultant
descObj = gp.Describe(finalFC)
areaField = descObj.AreaFieldName
print areaField, 'is the fieldname for Area'

# ADD a HECTARES COLUMN = 'AREA_HA'
gp.AddField_management(finalFC, "AREA_HA", "DOUBLE", "", "", "", "",
"NULLABLE", "NON_REQUIRED", "")

# CALCULATE AREA_HA using AREA FIELD
print 'calculating Hectares field'
hectaresCalc = '"' + '[' + areaField + ']/10000' + '"'
print hectaresCalc
gp.CalculateField_management(finalFC, "AREA_HA", hectaresCalc, "VB",
"")
I got that syntax from the ArcToolbox. I don't know why it suddenly
stopped working. I swear this worked last week. Now I get a resultant
dataset that has AREA_HA added but it's values are all null.

Any suggestions or ideas?
0 Kudos
5 Replies
JoshuaChan
Deactivated User
I tried doing this in model builder and it still doesn't work. Now the error message is :

Executing (Calculate Field): CalculateField W:\srm\sry\Workarea\jhchan\wksp\testing_ground.gdb\DCK_FD AREA_HA [GEOMETRY_Area]/10000 PYTHON # W:\srm\sry\Workarea\jhchan\wksp\testing_ground.gdb\DCK_FD
Start Time: Thu May 27 16:35:23 2010
ERROR 000539: Error running expression: [GEOMETRY_Area]/10000 <type 'exceptions.NameError'>: name 'GEOMETRY_Area' is not defined
A locator with this name does not exist.
Failed to execute (Calculate Field).
End Time: Thu May 27 16:35:23 2010 (Elapsed Time: 0.00 seconds)
But if I look at the feature class the item name "GEOMETRY_Area" does in fact exist. So why does Python and Model builder think it does not exist?
0 Kudos
justinperez
Deactivated User
I encountered something slightly similar where I was doing a search cursor to search for values that I wanted to calculate field in a seperate new clipped dataset.  I solved the issue by using an update cursor on the target instead of using calculate field.  (When I was using calculate field sometimes it would work and sometimes not and it would never hit my print statements!!)

Use an update cursor.  Good luck.
0 Kudos
DaleHoneycutt
Deactivated User
Seems that you're mixing Python syntax with VB syntax in the CalculateField expression.  VB uses the square bracket notation for field names, as in [my_field] and in Python the notation is exclamation point, as in !my_field!  I noticed that you're using Python in your modelbuilder version but VB in your script.  Try using Python and the exclamation point throughout and see what happens.
0 Kudos
JoshuaChan
Deactivated User
Seems that you're mixing Python syntax with VB syntax in the CalculateField expression.  VB uses the square bracket notation for field names, as in [my_field] and in Python the notation is exclamation point, as in !my_field!  I noticed that you're using Python in your modelbuilder version but VB in your script.  Try using Python and the exclamation point throughout and see what happens.


I've actually tried VB and Python (and every punctuation mark I could think of) and neither works. I ran out of things to try and ended up just doing this in ArcMap. I think the Calculate_Field tool is buggy or something because it works sometimes and othertimes it doesn't.
0 Kudos
KimOllivier
Honored Contributor
I've actually tried VB and Python (and every punctuation mark I could think of) and neither works. I ran out of things to try and ended up just doing this in ArcMap. I think the Calculate_Field tool is buggy or something because it works sometimes and othertimes it doesn't.


It is more likely that you have data that causes a crash in the expression. If you use the field calculator you do not have the opportunity to trap data errors in a try/except block and its impossible to see what record caused it.

Since the CalculateField simply wraps the expression in a cursor, it is much much better to use your own cursor in a script and do the expression inside a try/except block. CalculateField is more useful in interactive processes or models.
0 Kudos