Select to view content in your preferred language

Calculate Geometry Attributes causing AttributeError

1832
10
Jump to solution
01-10-2024 10:13 AM
Christian_Kennedy
Occasional Contributor

Hello!

I am currently trying to create a script to automate parcel updates. I built the original script in a Notebook that works perfectly fine in Pro but now that I am trying to recreate it as a stand-alone script, I am running into some hiccups. My current sticking point is trying to calculate geometry attributes. It is coming back with the error: AttributeError: 'module' object has no attribute 'CalculateGeometryAttributes'

I have tried a couple of things based on what I have found on the web pertaining to this error. First, I tried using arcpy.CalculateGeometryAttributes_management instead of arcpy.management.CalculateGeometryAttributes but no luck.

I then tried to create a variable for the parameters since there is so much text and that also did not work.  

I am able to run the geoprocessing tool in Pro once the layers have been created with the script, the tool just isn't working in the script. 

Any thoughts? 

Here is how I have it set up in the script:

arcpy.CalculateGeometryAttributes_management(out_data_py,
"ACRE AREA",
'',
"ACRES",
'PROJCS["NAD_1983_StatePlane_Kansas_North_FIPS_1501_Feet",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",1312333.333333333],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-98.0],PARAMETER["Standard_Parallel_1",38.71666666666667],PARAMETER["Standard_Parallel_2",39.78333333333333],PARAMETER["Latitude_Of_Origin",38.33333333333334],UNIT["Foot_US",0.3048006096012192]]',
"SAME_AS_INPUT")
print("Acreage Calculated")

I am fairly new to writing Python scripts for automation so I apologize if this isn't very clear or if I haven't included enough info. It's a fairly long script (for me) so I didn't want to share a bunch of superfluous text but if it's needed, I can add more for context. Thank you for looking!

 

0 Kudos
1 Solution

Accepted Solutions
Christian_Kennedy
Occasional Contributor

I figured it out! I THOUGHT I was running the same Python environment as Pro but I was running the Python env for ArcMap! Ugggghhhhh...I had checked but was not thorough enough apparently. But it's fixed and it's working now. Good gravy, what a relief. 

View solution in original post

0 Kudos
10 Replies
DavidPike
MVP Frequent Contributor

How are you importing the modules?  Just import arcpy or are you doing anything different?

Are you running from the same Python environment as Pro?  e.g. having an old ArcMap install.

I'd recommend trying the below syntax to access it via the arcpy.management submodule rather than _management.

arcpy.management.CalculateGeometryAttributes(in_features, geometry_property, {length_unit}, {area_unit}, {coordinate_system}, {coordinate_format})

 

 

Christian_Kennedy
Occasional Contributor

Thanks, @DavidPike !

I am running from the same environment as the Pro environment...I have several modules I've imported including arcpy, datetime, time and os. I made sure to use arcpy.management.CalculateGeometryAttributes instead of arcpy.CalculateGeometryAttributes_management but I still got the same error. What's goofy is I am able to use other data management geoprocessing tools earlier in the script and they work just fine. It's just when I get to calc geometry that it bombs. So it must be a problem with the submodule and how I have some of the parameters entered. That makes the most sense to me right now. 

 

0 Kudos
DavidPike
MVP Frequent Contributor
arcpy.CalculateGeometryAttributes_management(out_data_py,
                                             [["ACRE", "AREA"]],
                                             '',
                                             "ACRES",
                                             'PROJCS["NAD_1983_StatePlane_Kansas_North_FIPS_1501_Feet",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",1312333.333333333],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-98.0],PARAMETER["Standard_Parallel_1",38.71666666666667],PARAMETER["Standard_Parallel_2",39.78333333333333],PARAMETER["Latitude_Of_Origin",38.33333333333334],UNIT["Foot_US",0.3048006096012192]]',
                                             "SAME_AS_INPUT")
print("Acreage Calculated")

yes your syntax looks different from the format shown in the help.  I've adjusted the 2nd argument for the tool.  I'd also try without specifying the input coordinate system as the last argument and see if that's causing the issue.

0 Kudos
Christian_Kennedy
Occasional Contributor

Welp, I corrected the syntax and tried it multiple times. I tried without specifying that last argument. Then I started really messing around and just throwing stuff at the wall in those last two arguments and I'm still getting the same error. I am just stumped. 

0 Kudos
RhettZufelt
MVP Notable Contributor

If I run the tool in Pro, then right click on the job in the History pane and "Copy Python Command" I came up with this, that seems to work in script.

arcpy.management.CalculateGeometryAttributes(
    in_features=out_data_py,
    geometry_property="area AREA",
    length_unit="",
    area_unit="ACRES",
    coordinate_system='PROJCS["NAD_1983_StatePlane_Kansas_North_FIPS_1501_Feet",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",1312333.333333333],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-98.0],PARAMETER["Standard_Parallel_1",38.71666666666667],PARAMETER["Standard_Parallel_2",39.78333333333333],PARAMETER["Latitude_Of_Origin",38.33333333333334],UNIT["Foot_US",0.3048006096012192]]',
    coordinate_format="SAME_AS_INPUT"
)

Does this work for you?

R_

0 Kudos
Christian_Kennedy
Occasional Contributor

Sadly, it does not. I've done the same. I've copied the python command from the history pane in pro and put it into the script and it hasn't worked yet. I tried again with the syntax from your comment and it still doesn't work 😕  Thank you for the suggestion!

0 Kudos
LeeAllen
Frequent Contributor

Have you tried adding the script to a toolbox as a script tool then put the script in a model and schedule it? Now that you can schedule any model easily, I have stopped using stand alone scripts when possible and just create a new model and schedule it. This has worked flawlessly for over a year running scripts nightly.

0 Kudos
Christian_Kennedy
Occasional Contributor

I have not yet tried that but that's a good idea. Thanks, Lee!

0 Kudos
dhaag
by
New Contributor

what specifically are you trying to do with the entire tool and what version of Pro are you on?  You could likely set up a couple attribute rules to do your calculations on the fly as anything changes in your data.

For example:

This arcade expression set up as an attribute rule will update the acres calculation any time the geometry changes.

var acres = $feature.area; //whatever the name of the field is you are wanting to calculate
//Check if the geometry of the feature has changed during the last edit
if(Geometry($feature) != Geometry($originalFeature)) {
	//if it has, calculate the new acreage
	acres = Area($feature, 'acres');
}
//update the area field with the new calculation or just return the original value
return acres;

 

and if you don't care if it calculates the area for every edit regardless of whether you touched the geometry or not then it is as simple as this:

return Area($feature, 'acres')

 

If there are other processing tools updating different fields/etc.. earlier on in the script or afterwards, it is likely some simple attribute rules could be set up for them as well.

0 Kudos