arcpy.mapping SelectLayerByLocation_management

2552
1
Jump to solution
04-12-2013 10:36 AM
JamesCrandall
MVP Frequent Contributor
Okay, where is the example to select feature by an extent?

The select with a point geometry was easily found, but come'on --- the select by rectangle tool is already a default tool!  I'd think there'd be an example of how this tool selects features since it's such a common tool!

Any help?

I have this so far which doesn't select anything:

 def onRectangle(self, rectangle_geometry):          mxd = arcpy.mapping.MapDocument("CURRENT")          ext = rectangle_geometry         #Creates a polygon object         a = arcpy.Array()         a.add(ext.lowerLeft)         a.add(ext.lowerRight)         a.add(ext.upperRight)         a.add(ext.upperLeft)         a.add(ext.lowerLeft)         thepoly = arcpy.Polygon(a)          for lyr in arcpy.mapping.ListLayers(mxd):             if lyr.name=="mylayer":                 dlyr = lyr                 arcpy.SelectLayerByLocation_management(dlyr, "INTERSECT", thePoly, "", "NEW_SELECTION")           arcpy.RefreshActiveView()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor
updated with correction (lower case in "thePoly") and array revamped.  Still doesn't select a dang thing.

edit: yes, it does work 🙂

That only took about 1hr of google searching!  lol.  Have a great weekend.

def onRectangle(self, rectangle_geometry):          mxd = arcpy.mapping.MapDocument("CURRENT")         df = arcpy.mapping.ListDataFrames(mxd)[0]         ext = rectangle_geometry         thepoly = arcpy.Polygon(arcpy.Array([ext.lowerLeft, ext.lowerRight, ext.upperRight,  ext.upperLeft]),df.spatialReference)          for lyr in arcpy.mapping.ListLayers(mxd):             arcpy.AddMessage(lyr.name + " first layer found")             if lyr.name=="mylayer":                 dlyr = lyr                 arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", thepoly, "", "NEW_SELECTION")           arcpy.RefreshActiveView()

View solution in original post

0 Kudos
1 Reply
JamesCrandall
MVP Frequent Contributor
updated with correction (lower case in "thePoly") and array revamped.  Still doesn't select a dang thing.

edit: yes, it does work 🙂

That only took about 1hr of google searching!  lol.  Have a great weekend.

def onRectangle(self, rectangle_geometry):          mxd = arcpy.mapping.MapDocument("CURRENT")         df = arcpy.mapping.ListDataFrames(mxd)[0]         ext = rectangle_geometry         thepoly = arcpy.Polygon(arcpy.Array([ext.lowerLeft, ext.lowerRight, ext.upperRight,  ext.upperLeft]),df.spatialReference)          for lyr in arcpy.mapping.ListLayers(mxd):             arcpy.AddMessage(lyr.name + " first layer found")             if lyr.name=="mylayer":                 dlyr = lyr                 arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", thepoly, "", "NEW_SELECTION")           arcpy.RefreshActiveView()
0 Kudos