Python Script to Calculate Geometry Attributes from Selected Feature

4743
29
Jump to solution
09-14-2021 06:32 AM
ABishop
MVP Regular Contributor

Hello All,

I am running ArcGIS Pro 2.8.  I am also experimenting with Visual Studio to build an add-in with a button.  I want the button to run a python script which calculates the geometry attributes (acres) of a selected polygon and then I plan to make the add-in button available to our users in the office.  I was going to build a model in ArcGIS Pro which uses the "Calculate Geometry Attributes" tool to do this, but I quickly realized a few issues: #1 use the feature that is hard coded to my profile path and #2 not sure it will allow me to add a parameter to require the use to select the feature they want calculated due to the the issue with #3 the data source is a polygon stored in an SDE which is versioned.  Any suggestions?

Amanda Bishop, GISP
0 Kudos
29 Replies
ABishop
MVP Regular Contributor

Probably check the data source of the layer for the feature class name.

Amanda Bishop, GISP
0 Kudos
BlakeTerhune
MVP Regular Contributor

Is there any chance the same parcel feature class would be added as a layer to the map more than once? Or would the parcel layer be present in more than one map in the project?

0 Kudos
ABishop
MVP Regular Contributor

Sorry I just sent you a message and then I saw your question.  Yes, there is a chance.  Each user has control over their own project, and without me looking at their projects individually, I can't say for sure.

Amanda Bishop, GISP
0 Kudos
ABishop
MVP Regular Contributor

OK... So after I sent you that last message with the python code from the ModelBuilder, I ran the model itself while in the project with the feature class selected and a "null" in the GIS_ACRES field.... and it ran but then I got a warning.  I am assuming it is because the GIS_ACRES field only accepts so many characters and I really do need the acreage calculation to be two decimal points.  Is there a way to control this?  (see pics attached for the warning and the calculated value in the selected feature)

Amanda Bishop, GISP
0 Kudos
ABishop
MVP Regular Contributor

@BlakeTerhune 

Again... thank you so much for your assistance!

OK.. so I did some more research and I found out that there is a logical function in the ModelBuilder.  Its called "If Selection Exists".  To test this, I built a model with "Select by Attributes" (where "GIS_ACRES IS NULL") then used the logical function "If Selection Exists" to check if a selection is made on the "parcel" feature class and if the logical function returns true, then the "Calculate Geometry Attributes" occurs on the "GIS_ACRES" field.  To make sure this worked on the particular parcel i had selected, I made the GIS_ACRES field "null".  I exported the script from the model and ran it inside the project I had open with the parcel feature class selected and the script returned an error:

File "<string>", line 6
def # NOT IMPLEMENTED# Function Body not implemented
^
SyntaxError: invalid syntax

HELLLLPPPPPP! 😥  See python code window for the model builder code that I used. 

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2021-09-16 13:37:29
"""
import arcpy
def #  NOT  IMPLEMENTED# Function Body not implemented

def GeometryCalc():  # GeometryCalc

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = False

    arcpy.ImportToolbox(r"c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Data Management Tools.tbx")
    # Model Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"C:\Users\abishop\AppData\Local\Temp\ArcGISProTemp10544\1ba0f558-c873-4c6c-be6d-dc08393016c0\Default.gdb", workspace=r"C:\Users\abishop\AppData\Local\Temp\ArcGISProTemp10544\1ba0f558-c873-4c6c-be6d-dc08393016c0\Default.gdb"):
        parcel = "I:\\users\\abishop\\DATA\\CadastreCopy.gdb\\parcel"

        # Process: Select Layer By Attribute (Select Layer By Attribute) (management)
        parcel_Layer, Count = arcpy.management.SelectLayerByAttribute(in_layer_or_view=parcel, selection_type="NEW_SELECTION", where_clause="GIS_ACRES IS NULL", invert_where_clause="")

        # Process: If Selection Exists (If Selection Exists) ()
        True_48, False_49 = #  NOT  IMPLEMENTED(in_layer_or_view=parcel_Layer, selection_condition="EXISTS", count=0, count_min=0, count_max=0)

        # Process: Calculate Geometry Attributes (Calculate Geometry Attributes) (management)
        if True_48:
            parcel_Layer_2_ = arcpy.management.CalculateGeometryAttributes(in_features=parcel_Layer, geometry_property=[["GIS_ACRES", "AREA"]], length_unit="", area_unit="ACRES", coordinate_system="PROJCS[\"NAD_1983_StatePlane_Florida_West_FIPS_0902_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[\"Transverse_Mercator\"],PARAMETER[\"False_Easting\",656166.6666666665],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",-82.0],PARAMETER[\"Scale_Factor\",0.9999411764705882],PARAMETER[\"Latitude_Of_Origin\",24.33333333333333],UNIT[\"Foot_US\",0.3048006096012192]]", coordinate_format="SAME_AS_INPUT")[0]

if __name__ == '__main__':
    GeometryCalc()

 

 

 

Amanda Bishop, GISP
0 Kudos
BlakeTerhune
MVP Regular Contributor

Sorry, I didn't get a message and haven't had a chance to look closely at the screenshots. But regarding that error, simply deleting line 6 in that code you posted will resolve the syntax error.

0 Kudos
ABishop
MVP Regular Contributor

I tried that and it didn't work.

Amanda Bishop, GISP
0 Kudos
BlakeTerhune
MVP Regular Contributor

Didn't work as in you got an error or it did something unexpected? If an error, include the entire error message with traceback.

0 Kudos
ABishop
MVP Regular Contributor

See python window for code I ran without the lines you said to remove and the error message.

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2021-09-17 08:27:10
"""
import arcpy

def GeometryCalc():  # GeometryCalc

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = True

    arcpy.ImportToolbox(r"c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Data Management Tools.tbx")
    # Model Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"C:\Users\abishop\AppData\Local\Temp\ArcGISProTemp10544\1ba0f558-c873-4c6c-be6d-dc08393016c0\Default.gdb", workspace=r"C:\Users\abishop\AppData\Local\Temp\ArcGISProTemp10544\1ba0f558-c873-4c6c-be6d-dc08393016c0\Default.gdb"):
        parcel = "I:\\users\\abishop\\DATA\\CadastreCopy.gdb\\parcel"

        # Process: Select Layer By Attribute (Select Layer By Attribute) (management)
        parcel_Layer, Count = arcpy.management.SelectLayerByAttribute(in_layer_or_view=parcel, selection_type="NEW_SELECTION", where_clause="GIS_ACRES IS NULL", invert_where_clause="")

        # Process: Calculate Geometry Attributes (Calculate Geometry Attributes) (management)
        if True_48:
            parcel_Layer_2_ = arcpy.management.CalculateGeometryAttributes(in_features=parcel_Layer, geometry_property=[["GIS_ACRES", "AREA"]], length_unit="", area_unit="ACRES", coordinate_system="PROJCS[\"NAD_1983_StatePlane_Florida_West_FIPS_0902_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[\"Transverse_Mercator\"],PARAMETER[\"False_Easting\",656166.6666666665],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",-82.0],PARAMETER[\"Scale_Factor\",0.9999411764705882],PARAMETER[\"Latitude_Of_Origin\",24.33333333333333],UNIT[\"Foot_US\",0.3048006096012192]]", coordinate_format="SAME_AS_INPUT")[0]

if __name__ == '__main__':
    GeometryCalc()
Traceback (most recent call last):
  File "<string>", line 25, in <module>
  File "<string>", line 21, in GeometryCalc
NameError: name 'True_48' is not defined
Amanda Bishop, GISP
0 Kudos
BlakeTerhune
MVP Regular Contributor

I think as @DarrenWiens2 mentioned in the other thread, something appears to have gotten messed up when exporting this script from the model. Maybe try exporting it again.