4

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.

  • 1
    What version of ArcGIS for Desktop are you using? If it is earlier then 10.1 then your options are fewer. Please use the edit button to revise your Question with this important detail. Also, is the requirement simply to update every feature in a polygon feature class by choosing a layer? Or something else? – PolyGeo Oct 30 '13 at 9:23
  • Sorry...also new to stackexchange. I am using Desktop 10.2 Advanced. 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. – bhoobler Nov 5 '13 at 17:19
4

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.

  • 1
    You could also modify the code work with the Python Add-In Wizard by Creating a Python add-in button. This process will be more involved, but is interesting, and is another possibility. – Adam Oct 30 '13 at 20:42
  • Try running the script through the Python window in either ArcCatalog or ArcMap. The only thing that needs to be changed is the location of your shapefile or FC at line 4: in_featureclass = r'C:\gisworkspace\PythonTesting\Tracts.shp' – Adam Nov 5 '13 at 17:51
  • If this works (which it should), the problem may be in the setup of your script tool and parameter. – Adam Nov 5 '13 at 18:02
  • 1
    amarinel's script runs beautifully. User must make sure you do not copy/paste however as that introduces error into the script (ellipses). I have not tried to make this an add-in tool but I might. The only other thing left undone would be to run a script that overwrites the existing column for updates. The workaround is to delete the "acres" column and start over. I'm just not dealing with intuitive GIS users that would understand that. Thanks again for helping me solve the acre code! – bhoobler Nov 6 '13 at 1:14
  • 1
    You can just check if the field already exists. If it does, only the calculation is done, if it doesn't the field will be added and the calculation will be done. I rewrote @amarinel's script a little bit: gist.github.com/ustroetz/7340870 – ustroetz Nov 6 '13 at 17:51
3

I would use the Modelbuilder for that. You can use the Calculate Value tool to use your expression.

When you are done with the Model you can make a tool out of it. This is a good tutorial on how to do that.

  • 1
    I'm keen to see more details of the asker's requirements because I don't think this will do what I think he/she may be after. – PolyGeo Oct 30 '13 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.