[GIS] Automating a Python expression to calculate acres

arcgis-10.2arcgis-desktoparcpymodelbuilderpython-addin

I'm not new to GIS but very, very new to using Python and using scripts. I successfully use a Python expression !shape.area@acres! to update a field with acres in a feature class.

What my users would like is a tool they can put on their toolbar to see the calculated acres when they select a polygon feature. We had this tool in 9.3 but obviously doesn't work in 10. I am using Desktop 10.2 Advanced.

Is there some way to create this as a button so that users can add this "tool." I'm just not sure if I should be looking at ModelBuilder, geoprocessing tools, or add-ins? I just don't want my user to have to load this expression every time they want to use it.

Best Answer

I created a simple script tool using python which does not update an existing field but adds a new field and calculates the acreage. You could just as easily ask the user for the existing field name and update it (using a second parameter in the geoprocessing tool dialog box).

The python file looks like this (Creating a new Python script):

import arcpy

# Get the feature name to work with
in_featureclass = arcpy.GetParameterAsText(0)

# Set local variables
field_Name = "ACRES"
field_Type = "DOUBLE"
field_Precision = 10 # total number of digits stored
field_Scale = 8 # number of decimal places

arcpy.AddField_management(in_featureclass, field_Name, field_Type, field_Precision, field_Scale)

arcpy.CalculateField_management(in_featureclass, field_Name, '!SHAPE.area@ACRES!', "PYTHON_9.3")

Once you save this python file, create a new toolbox.tbx, right-click it > Add... > Script. Give it name, desc, etc. and CHECK the box to 'store relative path names.' Add the python file you created above on the next screen. On the final screen, choose your parameter 'name' and 'data type' and edit parameters if needed. I chose 'Feature Class' for data type which is 'Required' and filtered by 'Feature Class.'

Once the script tool is created, right-click on it > Import Script. You should be able to share your Toolbox.tbx with your co-worker(s) who can run the tool using their own data.

Related Question