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")
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Apr 21, 2013 at 17:18

1 Answer 1

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)
answered May 23, 2013 at 5:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.