I'm trying to add a tool to an add-in toobar to select features from an existing feature class and copy them across to another feature class. I need two tools, one for a point and one for a rectangle. Add-in tools only handle rectangles, so I'm trying to generate the point coordinate off the onMouseDown event and simply use the X and Y feedback I get. The problem is that I can't seem to pass these coordinates to anything usefull to extract the features. I've tried using Environment extents and then doing a simple CopyFeatures, but that's not working as it copies the entire fc and doesn't seem to honour the environment settings I set.
Can I pass coordinates to the Select by Location tool somehow or is there another way of passing coordinates to something to extract by that extent?
This is what I have now:
import arcpy
import pythonaddins
arcpy.overWriteOutput = True
# Replace this with the SDE layer once it's available.
global ELAtemplate, fc
ELAtemplate = r'C:\Data\nsw_map_units.shp'
fc = ""
class DefineUnitsbyPoint(object):
"""Implementation for DefineUnitsbyPoint.tool (Tool)"""
def __init__(self):
self.enabled = True
self.shape = "Rectangle" # Use onMouseDown to get initial extent of the rectangle.
def onMouseDownMap(self, x, y, button, shift):
# fc can be assigned from a combo box selection in another class
global ELAtemplate, fc
if fc == "":
pythonaddins.MessageBox('Choose a layer to select from.', 'Choose a Layer', 0)
else:
mxd = arcpy.mapping.MapDocument("CURRENT")
pointGeom = arcpy.PointGeometry(arcpy.Point(x,y), mxd.activeDataFrame.spatialReference)
arcpy.SelectLayerByLocation_management(ELAtemplate, "INTERSECT", pointGeom, "", "ADD_TO_SELECTION")
arcpy.CopyFeatures(ELAtemplate, fc)
fc
set? It's not set to anything but an empty string in this code. Also the input toSelectLayerByLocation
must be a reference to a Layer object or the name of a feature layer in the current map document.ELAtemplate
is a string pointing to a shapefile.fc
is set from a combo box selection. So I've made that global.ELAtemplate
is a shapefile here, but I've tried running this in the Python window as a layer, shapefile or dragged from the TOC and none of them work.pointGeom
:arcpy.MakeFeatureLayer_management("nsw_map_units","unit_layer")
wherensw_map_units
comes from the TOC. I then use that to runarcpy.SelectLayerByLocation_management('unit_layer',"INTERSECT",pointGeom,"#","ADD_TO_SELECTION")
, but I get no selection. I've even tried to physically create a point layer.pointGeom
doesn't actually intersect anything inunit_layer
. Perhaps you need to just refresh the view?