ArcPy & SelectLayerByLocation Performance

943
6
07-21-2011 03:17 PM
GraemeBrowning
Occasional Contributor III
I keep thinking that I must be missing something, but there does not seem to be a tool in ArcGIS 10 to select features (in particular polygons) from a layer at a point (X,Y) location via ArcPy. The parameters for such a tool would just be a layer name and an XY location.

At the moment I workaround this by creating a point featureclass containing the point and performing a SelectLayerByLocation on it. However, when the polygon feature class is in Oracle (accessed via ArcSDE 9.x) and contains 3.5 million polygons the time taken to make the selection can be more than 5 mins when I think a second or two (with less code) would be more appropriate. The feature class has a spatial index and I've tried using arcpy.env.extent (which SelectLayerByLocation appears to ignore) to restrict the geographic area accessed but the results always seem to be the same.

Is there a quicker way to do this using ArcGIS Desktop 10 and ArcPy?

PS I've made related posts to this on Stack Exchange and ArcGIS Ideas with no quick solutions so am beginning to think that I'm not actually missing anything.
Tags (2)
0 Kudos
6 Replies
BradPosthumus
Occasional Contributor II
What exactly do you need? Just the attribute information from the selected (identified) polygon? Will your script run within or outside of an ArcMap session? Is it based on an MXD?
0 Kudos
JasonScheirer
Occasional Contributor III
You can pass in a PointGeometry as an input, like so:


SelectLayerByLocation(in_layer=arcpy.PointGeometry(arcpy.Point(x, y)), select_features="mylayer")
0 Kudos
ChrisSnyder
Regular Contributor III
A speedier work around might be to buffer your point a bit and then use the buffer extent as the analysis extent to make an in_memory copy (CopyFeatures tool) of your SDE data, and then do a SelectByLocation on the smaller and local in_memory dataset. That way you are sort of making the SelectByLocation tool honor the analysis extent environment, which it would not normally do. BTW: Any features that overlap with the analysis extent will be copied with the CopyFeatures tool.

I sure wish the SelectByLocation tool and the cursors methods honored the analysis extent...
0 Kudos
PaulBillock
New Contributor II
I'm not sure this arcpy function works properly, or at least when it is exported from model builder to Python.  I exported a model which is supposed to select points within a polygon and it gives me an error that the layer is not a raster layer and not a mosaic and it fails.

additional:  it works perfectly in model builder.
0 Kudos
GraemeBrowning
Occasional Contributor III
Hi Paul

There is no guarantee that a model will just work when exported to Python script - see http://blogs.esri.com/dev/blogs/geoprocessing/archive/2011/06/24/exportmodeltopy.aspx

If you need help debugging why your exported model gives that error perhaps post some code.

- Graeme
0 Kudos
GraemeBrowning
Occasional Contributor III
Hi Thomas

You seem to be mixing up how to refer to layers vs datasets.

When you say: r'someLayer', it should just be someLayer (if you have already made a reference to it using ListLayers), or 'someLayer' (if there is a layer in your TOC called someLayer).

The 'r' only gets (at least by me) when dealing with backslashes in dataset paths.

- Graeme
0 Kudos