Select to view content in your preferred language

KeyError: '#' When Calculating Geometry in Arcpy

5299
5
Jump to solution
02-25-2021 03:23 PM
JamesWilcox1970
Occasional Contributor

As part of a larger script, I have code that checks to see if a particular field exists. If not, it creates it and calculate its area. The script works just fine until it goes to calculate the area of the newly created field, where it gives me KeyError: '#'. Is this error because the field was just created? If so, how do I work around it?

I am running Python 3.6 with PyCharm.

Here is the code I am having issues with--"projArea" is the project area feature class.

# Test to see if there is an "Acres" field in the Project Area FC.
projFields = arcpy.ListFields(projArea)  # List of fields in project area fc
x = False  # Default condition is that the field doesn't exist
for f in projFields:
    if f.name == "Acres":  # If there is a field named "Acres"
        x = True
if x is not True:  # If none of the fields are named "Acres"
    arcpy.AddMessage("{} does not have an Acres field. Creating field.".format(projArea))
    arcpy.AddField_management(projArea, 'Acres', 'DOUBLE')  # Create the field
    arcpy.AddMessage("Calculating area.")
    arcpy.management.CalculateGeometryAttributes(projArea, 'Acres', area_unit="ACRES")  # Calculate the area
    arcpy.AddMessage("Area calculated.")

Any help would be appreciated. 

Tags (2)
1 Solution

Accepted Solutions
DavinWalker2
Esri Contributor

Replace 

arcpy.management.CalculateGeometryAttributes(projArea, 'Acres', area_unit="ACRES")

With arcpy.management.CalculateGeometryAttributes(projArea, [["Acres", "AREA"]], area_unit ="ACRES")

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor

["Left", "EXTENT_MIN_X"]

in your case

[projArea, 'AREA']

Calculate Geometry Attributes (Data Management)—ArcGIS Pro | Documentation

a list is required for the first parameter, the field name and the property


... sort of retired...
DavinWalker2
Esri Contributor

Replace 

arcpy.management.CalculateGeometryAttributes(projArea, 'Acres', area_unit="ACRES")

With arcpy.management.CalculateGeometryAttributes(projArea, [["Acres", "AREA"]], area_unit ="ACRES")

JohannesLindner
MVP Alum

What Dan and Davin said.

Plus, you can just do this:

 

#projFields = arcpy.ListFields(projArea)
#x = False
#for f in projFields:
#    if f.name == "Acres":
#        x = True

projFieldNames = [f.name for f in arcpy.ListFields(projArea)]  # extract field names
x = "Acres" in projFieldNames  # Test if field "Acres exists

 


Have a great day!
Johannes
JamesWilcox1970
Occasional Contributor

That worked! I *knew* it was something that was staring me in the face. Thank you, all!

0 Kudos
ConradSchaefer__DOIT_
Regular Contributor

Encountered this issue too. The trick ended up being that I had to pay attention to the Data Type for the geometry_type parameter in the function arcpy.CalculateGeometryAttributes(). Documentation says it is a Value Table type, which was new to me. 

https://pro.arcgis.com/en/pro-app/3.4/tool-reference/data-management/calculate-geometry-attributes.h... 

geometry_property parameter data typegeometry_property parameter data type

Value Table documentation https://pro.arcgis.com/en/pro-app/3.4/arcpy/classes/valuetable.htm 

The solution using the list of lists appears to be satisfying the Value Table format.

value_table = arcpy.ValueTable(2) # 2 is the number of columns I needed
value_table.addRow(["field name 1", "data type 1"]) # 0 index
value_table.addRow(["field name 2", "data type 2"]) # 1 index
# NOTE: see https://pro.arcgis.com/en/pro-app/3.4/arcpy/geoprocessing_and_python/defining-parameter-data-types-in-a-python-toolbox.htm for options on data types

arcpy.management.CalculateGeometryAttributes(in_features=feature_layer_name, geometry_property=value_table.getRow(0), coordinate_system=sr, coordinate_format="Same as input")
# NOTE: sr is an arcpy.SpatialReference() object

 

If you call value_table.exportToString() on my example table above you get the following string format. Maybe the list of lists and the below quoted space separated and semi-colon delimited string also does the trick (?)

  • "'field name 1' 'data type 1';'field name 2' 'data type 2'"

 

0 Kudos