3

I am trying to select a polygon then it must automatically change (calculate) the status to "C". The tool seems to work in the background but nothing actually happens (the status is not updated). I have used the code from Select and copy features in ArcMap using Python add-in tool but nothing actually gets selected. If I have a feature in the layer selected already it does deselect it though.

Below is the code that i am using. Is there something that I have gotten wrong?

import arcpy
import pythonaddins

def getSearchDistanceInches(scale, selection_tolerance=3):
    """Returns the map distance in inches that corresponds to the input selection
    tolerance in pixels (default 3) at the specified map scale."""
    return scale * selection_tolerance / 96.0 # DPI used by ArcMap when reporting scale

class tlStatToC(object):
    """Implementation for PythonAddin_addin.tlMapCoods (Tool)"""
    def __init__(self):
        self.enabled = True
        self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
    def onMouseDownMap(self, x, y, button, shift):
        mxd = arcpy.mapping.MapDocument("CURRENT")
        pointGeom = arcpy.PointGeometry(arcpy.Point(x, y), mxd.activeDataFrame.spatialReference)
        searchdistance = getSearchDistanceInches(mxd.activeDataFrame.scale)
        lyr = arcpy.mapping.ListLayers(mxd)[0] # assumes you want to select features from 1st layer in TOC
        arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", pointGeom)
        arcpy.RefreshActiveView()
        arcpy.CalculateField_management(lyr,"STATUS","C")
2

You left off the search distance argument to SelectLayerByLocation. This is important -- for some reason, without it, a PointGeometry object will never intersect anything, at least in my limited testing.

Change:

arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", pointGeom)

To:

arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", pointGeom, "%d INCHES" % searchdistance)

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.