I have a large project I am working on and am stumped on one step.
There is a large group of layers (over 40), for each of which I need to (1) add an area column to the attribute table and then (2) calculate the area using "Calculate Geometry". The intent is to get two lines of arcpy, one for each of these tasks, that I can use in a loop and apply to all the layers.
I have figured out the ArcPy for making a new column, but am stumped at part (2) because I can't figure out the ArcPy for "Calculate Geometry".
I am used to ArcMap and have been doing the whole project in ArcMap up until now, so I tried ArcMap first. I did not see the necessary arcpy command available in ArcMap. When you type in "arcpy.CalculateGeometryAttributes_management" in the python window as per this link https://desktop.arcgis.com/en/arcmap/10.6/tools/data-management-toolbox/calculate-geometry-attributes.htm nothing comes up. I am assuming my version of ArcMap does not have this arcpy command.
I have ArcGIS Pro too, but don't really know how to use it well yet since our organization is only in the earliest stages of switching to it. When I try this command "arcpy.management.CalculateGeometryAttributes()", as per this link https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/calculate-geometry-attributes.htm, the command does exist, but I cannot figure out the parameters. It looks like it requires an input feature (self-explanatory), but the next three fields I cannot figure out. For the second parameter I tried putting the name of a new area column, but then, the third parameter requires a length unit, even though I'm interested in area. I tried leaving this length unit parameter out by doing ",," and skipping to the area units parameter, but got an error. I have tried various other configurations of these and get an error every time (SyntaxError, NameError or Failed to Execute Error). Examples of errors below:
arcpy.management.CalculateGeometryAttributes("Fields Watershed xc by Sub\\Fields_clip_Wat1", AREA, "ACRES_US")
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'AREA' is not defined
arcpy.management.CalculateGeometryAttributes("Fields Watershed xc by Sub\\Fields_clip_Wat1", , AREA, "ACRES_US")
File "<string>", line 1
arcpy.management.CalculateGeometryAttributes("Fields Watershed xc by Sub\\Fields_clip_Wat1", , AREA, "ACRES_US")
SyntaxError: invalid syntax
arcpy.management.CalculateGeometryAttributes("Fields Watershed xc by Sub\\Fields_clip_Wat1", AREA, "FEET_US", "ACRES_US")
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'AREA' is not defined
arcpy.management.CalculateGeometryAttributes("Fields Watershed xc by Sub\\Fields_clip_Wat1", "Area2", "FEET_US", "ACRES_US")
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\xxx\AppData\Local\Programs\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 4254, in CalculateGeometryAttributes
raise e
File "C:\Users\xxx\AppData\Local\Programs\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 4251, in CalculateGeometryAttributes
retval = convertArcObjectToPythonObject(gp.CalculateGeometryAttributes_management(*gp_fixargs((in_features, geometry_property, length_unit, area_unit, coordinate_system, coordinate_format), True)))
File "C:\Users\xxx\AppData\Local\Programs\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Traceback (most recent call last):
File "c:\users\xxx\appdata\local\programs\arcgis\pro\Resources\ArcToolbox\scripts\calculategeometry.py", line 290, in <module>
CalculateGeometry(fc, properties, lUnit, aUnit, cs, cformat)
File "c:\users\xxx\appdata\local\programs\arcgis\pro\Resources\ArcToolbox\scripts\calculategeometry.py", line 114, in CalculateGeometry
geomValue = eval(geomCalcs[calcProp[1].upper()])
KeyError: '#'
Failed to execute (CalculateGeometryAttributes).
I don't have a coherent script for this, because I usually just write the pieces I need and paste it into the python window. My txt notes below. The next to last line in the loop is the one I am asking for help with writing.
# Set workspace (where to save results)
arcpy.env.workspace = r'F:\xxx\Fields _ xc.gdb'
# list of layers to edit (drag and drop)
l = [...]
l.reverse() #reverses order so when layers are created, won't have to reorder them in side panel
#for loop - cycle through each layer, add a column, and calculate area
for i in range(len(l)):
print("adding column" + str(i))
in_layer = l[i]
arcpy.management.AddField(in_layer, "Area_Field", "DOUBLE")
print("calculating area" + str(i))
arcpy.management.CalculateGeometryAttributes(in_layer, geometry_property, {length_unit}, {area_unit}, {coordinate_system}, {coordinate_format})
print("next layer" + str(i+1))
TL;DR:
I'm a beginner with both ArcGIS Pro and Python, working on a large automation project.
How do I set up the parameters for the following in ArcGIS Pro,
arcpy.management.CalculateGeometryAttributes(in_features, geometry_property, {length_unit}, {area_unit}, {coordinate_system}, {coordinate_format})
and direct the results to a specific field in the attribute table?
in_feature: "Fields_intersect", att table area field: "Area_Field", area unit: US Acres
I am using ArcGIS Pro 3.0.3.
