Select to view content in your preferred language

Python Script to Calculate Geometry Attributes from Selected Feature

6894
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
2 Solutions

Accepted Solutions
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

View solution in original post

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

View solution in original post

0 Kudos
29 Replies
BlakeTerhune
MVP Regular Contributor

With arcpy, you can find the layer you want by looking at the data source, validate if a feature is selected, and run calculate field on the layer (so it only calculates the selected field). You may need to use the Editor class to start and stop an edit session. Alternatively, you could put that responsibility on the user to start an edit session. Catch and handle the error that arises when not in an edit session and display a helpful message in the geoprocessing results indicating to the user what they need to do.

0 Kudos
ABishop
MVP Regular Contributor

@BlakeTerhune 

Thank you for your reply.  I am not a python programmer, but I have written python code from models and geoprocessing tasks in ArcGIS Pro.  Would you be so kind to point me to the specific tools and the order I should run them in the model?  I would then use the model to create a python script which would be executed in the add-in button "On Click" function.

Amanda Bishop, GISP
0 Kudos
BlakeTerhune
MVP Regular Contributor

I'm not sure about making a custom selection and validating it with a model, but here are the tools I would use for writing my Python script.

  1. Find the layer you want to work on using the mapping module (arcpy.mp)
  2. Validate there is a selection with the layer's getSelectionSet() method (throw Exception if exactly one record is not selected.)
  3. Start an arcpy Editor. Test without the Editor first in case it just works. I don't think you need it for calculate field.
  4. Add geometry attributes. Only the selected features will have values calculated in the added fields; all other features will have null values. If this is not what you want, maybe try Calculate Field with the shape field.
0 Kudos
ABishop
MVP Regular Contributor

@BlakeTerhune 

Thank you again for the information but its not exactly what I am trying to do with this workflow.  The mapping module requires that I have a set project for this button to work and unfortunately our users all have their own projects and edit versions.  I would basically need to set the selection request from the user and have them select the feature.  Based on their selection, the calculation of the geometry attribute would occur.  I am looking into doing a model with parameters but having difficulty with that as well.

Amanda

Amanda Bishop, GISP
0 Kudos
BlakeTerhune
MVP Regular Contributor

You can get the "current" project that is open running the script.

Guidelines for arcpy.mp—ArcGIS Pro | Documentation

aprx = arcpy.mp.ArcGISProject('current')
ABishop
MVP Regular Contributor

@BlakeTerhune 

OK... so aprx is a variable correct?  Would I need to call up a selection of the polygon feature class also?  I want to use this in the calculate geometry attributes tool as an input.

Amanda Bishop, GISP
0 Kudos
BlakeTerhune
MVP Regular Contributor

Yes, aprx is the variable name for the current ArcGISProject object in the line of code I posted. Once you have that project, you will need to identify the Map in the project you want. If you know it by name for everyone, that's best. Otherwise, you'd have to look at all the maps to find a layer with the feature class name you want. Once you find the layer (by name) in the map of the project, step 1 I described above is complete. 

0 Kudos
ABishop
MVP Regular Contributor

I want to use "current" so that when they open whatever project they have with the layer "parcel" for instance, that I can just find the selection made in the "parcel" layer and perform the calculate geometry attributes geoprocessing tool on the current selection.  This really shouldn't be this hard, but I am terrible with writing code from scratch and therefore having to do a lot of reading to figure out the correct format.

Amanda Bishop, GISP
0 Kudos
BlakeTerhune
MVP Regular Contributor

Is it safe to assume the layer will always have the same name ("parcel") or would it be better to check the data source of the layer for the feature class name?

0 Kudos