# Calculate geometry leads to KeyError: '#'

366
4
04-30-2022 02:58 PM
by
New Contributor III

import arcpy as a

a.env.overwriteOutput=1
a.env.workspace=r'F:\route\to\database\database.gbd'

a.management.MakeFeatureLayer('feature_class', 'feature_layer')
a.CalculateGeometryAttributes_management('feature_layer', ["new_field", "AREA_GEODESIC"], area_unit="SQUARE_KILOMETERS")

Tried to calculate it with both arcpy.management.CalculateGeometryAttribute and arcpy.CalculateGeometryAttributes_management as in https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/calculate-geometry-attribute... always led to:

KeyError: '#',

Failed to execute (CalculateGeometryAttributes),

geomValue = eval(geomCalcs[calcProp[1].upper()]).

I´m using PythonWin 3.7.11 and ArcGIS Pro 2.9.0

4 Replies
MVP Esteemed Contributor

Where are

'feature_class', 'feature_layer'

defined?

CalculateGeometryAttributes(in_features, geometry_property, {Length_Unit}, {Area_Unit}, {Coordinate_System}

The parameters don't include field names

... sort of retired...
by
New Contributor III

'feature_class', 'feature_layer' are not variables, 'feature_class' exists in .gdb. Not sure I understand what you mean with "The parameters don't include field names", field name should be 1st part of 2nd parameter.a

MVP Esteemed Contributor

the help shows a list of lists, whereas an exported run shows a semi-colon delimited string.  maybe they are interchangeable

one field

arcpy.management.CalculateGeometryAttributes("pnts", "xs POINT_X", '', '', None, "SAME_AS_INPUT")

two fields

arcpy.management.CalculateGeometryAttributes("pnts", "xs POINT_X;ys POINT_Y", '', '', None, "SAME_AS_INPUT")

try [["new_field", "AREA_GEODESIC"]] since the help shows a list enclosed in a list for multiple parameters, perhaps it needs the enclosing list even if one is chosen

... sort of retired...
MVP Esteemed Contributor

arcpy.CalculateGeometryAttributes_management is simply an alias for arcpy.management.CalculateGeometryAttributes, so you should never get different results between them.  If you do get different results, there is a ArcPy defect somewhere.

According to the documentation you reference, the geometry_property parameter should be in the form of a Value Table, which is one form of a multi-value input.  Working with multivalue inputs—ArcGIS Pro | Documentation .  Working with Value Table objects is quite clunky, so there are Python list and string representations of them, which is what you commonly see in documentation examples.