Extent env. ignored by Select Layer by Attribute

539
4
Jump to solution
12-12-2013 03:44 AM
StormwaterWater_Resources
New Contributor III
I tried refreshing an old post (Select Layer by Attribute with Extent environment parameter) on this topic, but it doesn't seem to be attracting any attention.  I'm trying to restrict a Select Layer by Attribute to my display extent.  This is clearly outlined on the help page (3rd bullet pt).  No matter how I use the tool (stand-alone, in a model, via Python) it always selects everything.

Since I want to select everything in the current display extent I'm using this SQL expression:   "OID" = "OID"

This seems like such a handy feature that I find it hard to believe others haven't had this problem, so I'm wondering if it's me.

10.1, Win 7 64

PS: I'm curious if others have this problem, but I'll green check anyone who gives me an alternate "Select All in Display" tool/method. (obviously this is going to be geo-processed so "use the mouse" will not get a green check  🙂
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
One thing you could do is create a polygon from the data frame's extent, and then perform a Select Layer by Location to select all features that intersect the polygon.  Ex:

import arcpy from arcpy import env from arcpy import mapping  mxd = mapping.MapDocument("CURRENT") df = mapping.ListDataFrames(mxd)[0]  #create polygon from data frame extent XMAX = df.extent.XMax XMIN = df.extent.XMin YMAX = df.extent.YMax YMIN = df.extent.YMin pnt1 = arcpy.Point(XMIN, YMIN) pnt2 = arcpy.Point(XMIN, YMAX) pnt3 = arcpy.Point(XMAX, YMAX) pnt4 = arcpy.Point(XMAX, YMIN) array = arcpy.Array() array.add(pnt1) array.add(pnt2) array.add(pnt3) array.add(pnt4) array.add(pnt1) polygon = arcpy.Polygon(array)  #select features that intersect polygon arcpy.SelectLayerByLocation_management("Airports", "INTERSECT", polygon)

View solution in original post

0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
One thing you could do is create a polygon from the data frame's extent, and then perform a Select Layer by Location to select all features that intersect the polygon.  Ex:

import arcpy from arcpy import env from arcpy import mapping  mxd = mapping.MapDocument("CURRENT") df = mapping.ListDataFrames(mxd)[0]  #create polygon from data frame extent XMAX = df.extent.XMax XMIN = df.extent.XMin YMAX = df.extent.YMax YMIN = df.extent.YMin pnt1 = arcpy.Point(XMIN, YMIN) pnt2 = arcpy.Point(XMIN, YMAX) pnt3 = arcpy.Point(XMAX, YMAX) pnt4 = arcpy.Point(XMAX, YMIN) array = arcpy.Array() array.add(pnt1) array.add(pnt2) array.add(pnt3) array.add(pnt4) array.add(pnt1) polygon = arcpy.Polygon(array)  #select features that intersect polygon arcpy.SelectLayerByLocation_management("Airports", "INTERSECT", polygon)
0 Kudos
WilliamCraft
MVP Regular Contributor
Quite similar to what Jake proposed, you may be able to grab the bounding box of the dataframe's extent this way:

map_document = arcpy.mapping.MapDocument("current")
dataframe = arcpy.mapping.ListDataFrames(map_document, map_document.activeView)[0]
extent = dataframe.extent

You can then pass the extent's bounding box as parameters to the next geoprocessing tool in your Python script.
0 Kudos
StormwaterWater_Resources
New Contributor III
Thank you both!

I gave the answer to Jake since that solution actually did the selection.

I had been thinking that Jake's solution required creating a feature class (or at least a feature) somewhere, but then I realized that this uses an on-the-fly geometry as discussed at the top of the Geometry class help page

I'd read William's post first, and so researched the extent method. Based on that I tried this:

mxd = arcpy.mapping.MapDocument("CURRENT")
df  = arcpy.mapping.ListDataFrames(mxd)[0]

extent = df.extent
grids = arcpy.da.SearchCursor("Clip_Grid",("SHAPE@","PageName"))
for grid in grids:
    if (extent.contains(grid[0])) or (extent.overlaps(grid[0])) or (extent.within(grid[0])):
        print grid[1]


And that successfully prints out each PageName value in the equivalent of an intersect.  As a side point, why is there no simple "intersects" method?

PS: Oh, there's still the question of whether or not the extent environment parameter for the Select Layer by Attribute tool is actually broken.  Jake?  Based on your logo, can you answer that just for curiosity?  (either way though, thank you for the work-around).
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I believe that is a typo within the help.  If you look under 'Environments' on the help page, EXTENT is not listed.
0 Kudos