Wednesday, 7 August 2019

arcgis 10.0 - Incorrect field name resulted in RuntimeError: ERROR 999999 (Also, better code for adding and calculating fields)


This is the bottom half of my code (and it's been edited somewhat to share publicly). The code works perfectly fine when the fieldList has only three fields in it, but when I populate it with all 108 fields, I receive the following error, just before it starts to calculate fields:


Traceback (most recent call last):

File "C:\GIS\1GIS_DATA\Folder1\Folder2\Python and Tools\RunThisPythonCode.py", line 125, in
value = row.getValue(field)* row.anotherField #<--This is the line the error is referring to
File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\arcobjects.py", line 944, in getValue
return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args)))
RuntimeError: ERROR 999999: Error executing function.

Here's the code:


....

fc = arcpy.CopyFeatures_management(layerName, outFeatures)


#---------------------------------------------
# Add fields to a feature class:
#---------------------------------------------
fieldList = ["field1", "field2", "field3", ............., "field99"] #<-- fieldList
fieldListx = []
for f in fieldList:
fieldx = f+"x"
arcpy.AddField_management(fc, fieldx, "DOUBLE")
fieldListx.append(fieldx)


#---------------------------------------------
# Calculate fields:
#---------------------------------------------
rows = arcpy.UpdateCursor(fc)
for row in rows:
for fieldx in fieldListx:
field = fieldx.rstrip('x')
value = row.getValue(field)* row.anotherField #<--This is the line the error is referring to
row.setValue(fieldx,value)

rows.updateRow(row)
del row, rows

Is this an issue of memory or something? Why does it produce an error before Calculate Fields when I add the full list of 108 fields, but not when I run a small sample of only 3? Thanks!!



Answer



OK, there are two solutions to the above. I will paste them both below... Thanks everyone for all of your help!


Ultimately the problem WAS with a field name. My field#11 had at some point lost its last letter. When it was being appended to fieldList and later asked to "strip" itself of the "x" that had been added to it, it was looking for a field in the feature class that didn't exist. Despite the fact that locating that problem was like finding a needle in a haystack, I fixed it, and I didn't receive the error again. Lesson learned: always check your field names!!


Below, however, are "solutions" simply to writing better code to do what I was trying to do above:


Solution #1:
This one is the best. It requires fewer lines and runs faster. It is the result of a suggestion from @ccn in this post. I tweaked it just slightly by including quotations around the static field name UNpctOfEd.



....

fc = arcpy.CopyFeatures_management(layerName, outFeatures)

fieldList = ["field1", "field2", "field3", ............., "field105"] #<-- this has to be done manually for various reasons so I can't just use ListFields

# Loop through fieldList (Add and Calculate fields)
for field in fieldList:
# local variables
fieldx = field + "x"

expression = "!" + field + "! * !" + "UNpctOfEd" + "!" #<-- Jackpot!! Figuring out how to write the expression
# add/calculate new field
arcpy.AddField_management(fc, fieldx, "DOUBLE")
arcpy.CalculateField_management(fc, fieldx, expression, "PYTHON_9.3")

Solution #2:
This one is a working/much better version of what I had in the question above, using UpdateCursor although it requires more lines, is more complicated, and is slower than Solution #1:


....

fc = arcpy.CopyFeatures_management(layerName, outFeatures)


fieldList = ["field1", "field2", "field3", ............., "field105"] #<-- again, this has to be done manually for various reasons so I can't just use ListFields

# Creating new fields in the feature class
fieldListx = []
for f in fieldList:
fieldx = f+"x"
arcpy.AddField_management(fc, fieldx, "DOUBLE")
fieldListx.append(fieldx)
print fieldListx


# Calculating those new fields in the feature class
for fieldx in fieldListx:
rows = arcpy.UpdateCursor(fc)
field = fieldx.rstrip('x')
for row in rows:
value = row.getValue(field)* row.UNpctOfEd
row.setValue(fieldx,value)
rows.updateRow(row)
del row, rows

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...