[GIS] Does ArcPy have spatial search function for geometry

arcpy

My problem:
Using ArcPy I want to loop over buffers and select geometry_features inside each buffer and do something (update) only the objects found within that specific buffer.
The code below explains what I want to do (more or less):

def _update_connections_inside_buffers(self):
    buffers = arcpy.SearchCursor(self.__buffer_class_name)
    in_layer = "connections"
    for i_buffer in buffers:
        shape = i_buffer.shape
        # can not use a geometry to do a selection.. very inconvenient!!
        connections = arcpy.SelectLayerByLocation_management(in_layer, "WITHIN", shape) 
        self._update_connections(connections)

However: this will not work because SelectLayerByLocation_management() does not accept a geometry, "shape" as an argument but expects a feature_class_name (layer name). Is there a arcpy method that can do a search using a spatial predicate. I could not find how to do this from the ESRI manual.

Best Answer

I'm confident something like this can be done because we use the code below in one of our training courses. If it appears not to be working, then I suspect that you have either not defined the layer object by using a layer in the Table of Contents of ArcMap, or by using MakeFeatureLayer outside of ArcMap.

Or, more likely I think it is the "connections = " next to SelectLayerByLocation that is giving you your problem because you are setting that to a Result object and not extracting anything from it before passing it back in.

import arcpy
schoolsLayer = "Schools"
suburbsLayer = "Suburbs"
# get an update cursor as we will be changing values
rows = arcpy.UpdateCursor(suburbsLayer)
# loop through each suburb in the layer
for row in rows:
    polygon = row.SHAPE
    arcpy.SelectLayerByLocation_management(schoolsLayer,"INTERSECT",polygon)
Related Question