Select to view content in your preferred language

Python Script to Calculate Geometry Attributes from Selected Feature

6897
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

I did try exporting it several times.  That logical operator is only meant for model builder.  I think what I am going to do at this point is just build the model to select all parcel features that contain a null value and then use that selection to calculate the geometry attributes.

Thank you for all your help.  Sorry this one wasn't solved. 😕

Amanda Bishop, GISP
ABishop
MVP Regular Contributor

I already did that several times.  I am going to just eliminate that logical operation from the model in order to export it as a script.  Its not optimal though.  Unfortunately this wasn't solved.  

Amanda Bishop, GISP
0 Kudos
BlakeTerhune
MVP Regular Contributor

The logic could easily be added back in manually in the Python script.

0 Kudos
ABishop
MVP Regular Contributor

how?

Amanda Bishop, GISP
0 Kudos
BlakeTerhune
MVP Regular Contributor

Well, post the full script you export that actually runs and describe the logical operation you'd like it to have. We'll come up with a solution together.

0 Kudos
ABishop
MVP Regular Contributor

I posted it a couple times already in this thread.

Basically what I want to do is if the user has a parcel selected, then if they click the button that I want to create in ArcGIS Pro SDK, it will calculate the geometry attributes of that parcel.  The field name is "GIS_ACRES".

Amanda Bishop, GISP
0 Kudos
BlakeTerhune
MVP Regular Contributor

Oh, sorry, I didn't realize there was not a version that worked.

0 Kudos
ABishop
MVP Regular Contributor

Yes, there was a version from the model that works when running the model manually.  None of the script versions worked because of that logical operator "IfSelectionExists".  Apparently that logical operator only works with model builder. *Shrug*

Amanda Bishop, GISP
0 Kudos
ABishop
MVP Regular Contributor

Here is the script from the model.  I didn't ever have a script that ran properly. Only the model ran properly.

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2021-09-17 14:39:12
"""
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_2_ = "parcel"

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

        # Process: If Selection Exists (If Selection Exists) ()
        True_69, False_70 = #  NOT  IMPLEMENTED(in_layer_or_view=parcel_2_, selection_condition="EXISTS", count=0, count_min=0, count_max=0)

        # Process: Calculate Geometry Attributes (Calculate Geometry Attributes) (management)
        if True_69:
            parcel_3_ = arcpy.management.CalculateGeometryAttributes(in_features=parcel_2_, 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

Am I correct in my understanding that this just selects features where GIS_ACRES IS NULL, then calculates the geometry (acres) into the GIS_ACRES field? Try this version of the script to do the same thing. It looks like it is referencing a local temp workspace so you might need up update scratchWorkspace

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2021-09-17 14:39:12
"""
import arcpy

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_2_ = "parcel"

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

        # Process: Calculate Geometry Attributes (Calculate Geometry Attributes) (management)
        if count:
            arcpy.management.CalculateGeometryAttributes(in_features=parcel_2_, 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()

 

0 Kudos