3

I'm looking to create a simple button that calculates acres on selected polygons and can't quite find what I'm looking for. I read through this thread: automating a python script..., but found it wasn't quite what I am looking for. My Python skills are very near nil, I'm using ArcGIS 10.1 SP1 for Desktop (Build (3143).

I'm aware of !SHAPE.area@ACRES!, but need help getting into button form that resides on my toolbar.

EDIT: I've pieced some code together from several examples here & around the net. When I add the button to ArcGIS, highlight a polygon and click, nothing happens. Nothing. I'm obviously something and hope it's just one big hump to get me there. Below is the code thus far.

import arcpy
import pythonaddins

class CalculateSelectedAcreage(object):
    """Implementation for AcreageAddInTest_addin.CalcAcresSelected (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        # Implementation of OnClick method of Button's class
        # Get the current map document and the first data frame.
        mxd = arcpy.mapping.MapDocument('current')
        df = arcpy.mapping.ListDataFrames(mxd)[0]

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

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

        fieldList = arcpy.ListFields(in_featureclass, field_Name)
        fieldCount = len(fieldList)
        if fieldCount == 1:
            # field already exists, as it does in DI Landtracs
            arcpy.calculateField_management(in_featureclass, field_Name, 'float(!SHAPE.area@ACRES!)', "PYTHON_9.3")                                         
  • possible duplicate of Automating a Python expression to calculate acres – Jason Scheirer Nov 8 '13 at 15:56
  • @JasonScheirer, that thread is close, but not quite what I'm looking for. I want to be able to click a button to calculate acres and fill an existing field ("Area"), rather than automatically in a new field. – spark Nov 8 '13 at 22:15
  • @spark I've been wanting to do this too. In 9.3.1 I had a tool that did this well, but have not found a good alternate. I'll work on something and hopefully write an answer. – Elliott Dec 19 '13 at 4:05
  • Have you considered using arcpy.da.UpdateCursor? I've used this very successfully with addins. – Barbarossa Jan 5 '14 at 5:34
  • @Elliott yes we had a similar tool in 9.3.1 and lost it with the upgrade to 10.1 – spark Jan 6 '14 at 13:10
2

Getting a script to a button is pretty simple.

Go to the menu Customize --> Customize Mode --> Commands (tab)

Once on the Commands tab, scroll to the bottom of "Categories" and choose:

[Geoprocessing Tools]

and click on "Add Tools..."

Find the Script Tool you want to add.

The tool should be in the "Commands:" window, just drag it onto a toolbar and voila! You have it in a button form.

...

However, there are other things that you should clarify. How you see the Acres depends on the code in you Script Tool. Do you have a Tool yet? Or do you need help with that as well?

  • I don't yet have a script that can work (near nil on Python experience) but want to figure it out. – spark Nov 8 '13 at 21:39
1

Try using the Python Addin Toolbar features from ESRI ESRI's Tool Bar Documentation

Then almost any geoprocessing task that you can perform using the GUI can be performed using the the arcpy site package. All of the ESRI documentation for geoprocessing tools has examples of python code for that process or tool. ESRI Field Calculate Documentation

Here is a snippet of my code for a tool bar which uses a combo box to select the file that will have polygon values changed and A button that when clicked will change the value of the selected polygon. Make sure when declaring the variable that references the selected file from the combo box that you declare it as a "global" variable.

class LFM_8600(object):
"""Implementation for LFM_8600.button_8600 (Button)"""
def __init__(self):
    self.enabled = False
    self.checked = False
def onClick(self):
    inFeatures=l
    expression=8600
    arcpy.CalculateField_management(inFeatures, 4000, expression, "PYTHON")
    arcpy.CalculateField_management(inFeatures, "DOM40_CLASS", expression, "PYTHON")
    arcpy.CalculateField_management(inFeatures,"DOM60_CLASS",expression, "PYTHON")
    arcpy.CalculateField_management(inFeatures, "CCV_CLASS", expression, "PYTHON")
    arcpy.CalculateField_management(inFeatures, "TSZ_CLASS", expression, "PYTHON")

class ListLayers(object):
    """Implementation for NEZ_EDITS_addin.list_layers (ComboBox)"""
    def __init__(self):
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWW'
    def onSelChange(self, selection):
        global l
        l=arcpy.mapping.ListLayers(self.mxd, selection)[0]
        arcpy.RefreshActiveView()

    def onFocus(self, focused):
        if focused:
            self.mxd = arcpy.mapping.MapDocument('current')
            layers = arcpy.mapping.ListLayers(self.mxd)
            self.items = []
            for layer in layers:
                self.items.append(layer.name)
1

Your code that you have pieced together looks like it was written for a script tool which you run as a geoprocessing tool not as a python addin. I say this as you are using GetParameterAsText() as the method to pass in the FeatureClass name.

I'm pretty sure this is your problem.

Try hard-wiring it with the FeatureClass name to prove this is the problem. You then need a way of getting a handle on the layer you want to update.

As of 10.1 there is a way of get a handle on 1 and only 1 selected layer in the Table of Contents. Have a look at this thread this will get you going.

  • Thanks @Hornbydd. I'm not sure how to do that, but I will start working on passing the FeatureClass name into the code. – spark Nov 26 '13 at 16: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.