import arcpy from arcpy import env import pythonaddins import os import subprocess class ToolClass2(object): """Implementation for ArcGISAddins_addin.tool (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): # Local variables: arcpy.env.workspace = "C:/temp/python/test.gdb" Parcels = "C:/temp/python/test.gdb/Parcels" #message = "Your mouse clicked:" + str(x) + ", " + str(y) #pythonaddins.MessageBox(message, "My Coordinates") mxd = arcpy.mapping.MapDocument("current") pointGeom = arcpy.PointGeometry(arcpy.Point(x, y), mxd.activeDataFrame.spatialReference) df = mxd.activeDataFrame searchdistance = getSearchDistanceInches(df.scale, 3) #lyr = arcpy.mapping.ListLayers(mxd)[0].name # assumes you want to select features from 1st layer in TOC # Make a layer from the feature class arcpy.MakeFeatureLayer_management(Parcels, "feature_layer") arcpy.SaveToLayerFile_management("feature_layer", "C:/temp/python/test.gdg/Parcels.lyr") addlayer = arcpy.mapping.Layer("C:/temp/python/Parcels.lyr") arcpy.mapping.AddLayer(df, addlayer, "BOTTOM") arcpy.SelectLayerByLocation_management("feature_layer", "INTERSECT", pointGeom, "%d INCHES" % searchdistance,"NEW_SELECTION") arcpy.RefreshActiveView() #arcpy.Delete_management("feature_layer")
Solved! Go to Solution.
def onMouseDownMap(self, x, y, button, shift): # set up environment arcpy.env.workspace = "C:/temp/python/test.gdb" Parcels = "C:/temp/python/test.gdb/Parcels" mxd = arcpy.mapping.MapDocument("current") df = mxd.activeDataFrame # build point geometry, run your getSearchDistanceInches function pointGeom = arcpy.PointGeometry(arcpy.Point(x, y), mxd.activeDataFrame.spatialReference) searchdistance = getSearchDistanceInches(df.scale, 3) # make an arcpy.mapping layer obj and do the selection by location plyr = arcpy.mapping.Layer(Parcels) arcpy.SelectLayerByLocation_management(plyr, "INTERSECT", pointGeom, "%d INCHES" % searchdistance, "NEW_SELECTION") # all done, now add our layer to the map arcpy.mapping.AddLayer(df, plyr, "BOTTOM") arcpy.RefreshActiveView()
def onMouseDownMap(self, x, y, button, shift): # set up environment arcpy.env.workspace = "C:/temp/python/test.gdb" Parcels = "C:/temp/python/test.gdb/Parcels" mxd = arcpy.mapping.MapDocument("current") df = mxd.activeDataFrame # build point geometry, run your getSearchDistanceInches function pointGeom = arcpy.PointGeometry(arcpy.Point(x, y), mxd.activeDataFrame.spatialReference) searchdistance = getSearchDistanceInches(df.scale, 3) # make an arcpy.mapping layer obj and do the selection by location plyr = arcpy.mapping.Layer(Parcels) arcpy.SelectLayerByLocation_management(plyr, "INTERSECT", pointGeom, "%d INCHES" % searchdistance, "NEW_SELECTION") # all done, now add our layer to the map arcpy.mapping.AddLayer(df, plyr, "BOTTOM") arcpy.RefreshActiveView()
You are making it way too hard on yourself. Let's simplify the code a little:def onMouseDownMap(self, x, y, button, shift): # set up environment arcpy.env.workspace = "C:/temp/python/test.gdb" Parcels = "C:/temp/python/test.gdb/Parcels" mxd = arcpy.mapping.MapDocument("current") df = mxd.activeDataFrame # build point geometry, run your getSearchDistanceInches function pointGeom = arcpy.PointGeometry(arcpy.Point(x, y), mxd.activeDataFrame.spatialReference) searchdistance = getSearchDistanceInches(df.scale, 3) # make an arcpy.mapping layer obj and do the selection by location plyr = arcpy.mapping.Layer(Parcels) arcpy.SelectLayerByLocation_management(plyr, "INTERSECT", pointGeom, "%d INCHES" % searchdistance, "NEW_SELECTION") # all done, now add our layer to the map arcpy.mapping.AddLayer(df, plyr, "BOTTOM") arcpy.RefreshActiveView()
There are 2 kinds of feature layers used in arcpy code. The type made by MakeFeatureLayer_management is a geoprocessing layer and does not work with some parts arcpy.mapping, like AddLayer. The other kind is an arcpy.mapping layer object, which can be a feature layer or a number of other kinds of layers. If it is feature layer, then it will work with at least some of the geoprocessing tools like SelectLayerByLocation that expect a feature layer. If this seems confusing, don't feel bad. It confuses me too.
Here's some more confusing stuff: You can create an arcpy.mapping.Layer object from a saved .lyr file, like you were doing. Now, the documentation doesn't tell you this, but you can also make a layer object from a feature class on a disk, from another layer in the toc, or even from a geoprocessing layer made with MakeFeatureLayer_management. So all the work you were doing to make a .lyr file was unnecessary. Just make an arcpy.mapping layer straight from the feature class, select against that layer, and add it to the map.
You were raising an error in your code so that's why it seemed nothing was happening. The addin environment is really tough to debug in, since you don't get to see the traceback and usually don't even know an error has occurred. So I get my code running first by executing it in the Python window. Then, when everything is running correctly, I stick the code into a method or function in the addin.
good luck,
Mike
I'd add that once you have the feature selected, an easy way to grab the attribute value is with arcpy.da.SearchCursor:
line_id = arcpy.da.SearchCursor(plyr, "LINE_ID").next()[0]