10

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
user8175
  • 111
  • 1
  • 5

3 Answers3

13

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)
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
2

This can be done, but as far as I know, only through the use of Feature Layers w/arcpy. Essentially you'll create a where clause and update it with the OID (or other uniquely identifying field) and loop through the SelectLayerByLocation.

Check out this tutorial from the PSU master's program-- it covers pretty much exactly what you're after if I'm not mistaken.

No reference to geometry is needed; the spatial query uses the geometry based on what type of query you define (i.e. completely contains, shares a border, etc.). If you need to reference the geometry of these points, once they're determined to be within the buffer append them (OID) to a python list and work with the original data set using a where clause.

Roy
  • 3,958
  • 4
  • 30
  • 54
  • I found the solution to the problem. I had to give a feature_layer as a first argument instead of string refering to the class_layer. – user8175 Jun 16 '12 at 07:46
1

I found the solution to the problem. I had to provide a feature_layer as a first argument instead of string refering to the class_layer."

    # Make a feature layer from the feature class
    class_layer = arcpy.env.workspace + "." + "connections"
    arcpy.MakeFeatureLayer_management(class_layer, "feature_layer")

    # Do selection using feature_layer 
    arcpy.SelectLayerByLocation_management("feature_layer", "WITHIN", p_polygon)

    # get result
    connections_found_in_polygon = arcpy.SearchCursor("feature_layer")
    self._update_connections(connections_found_in_polygon)
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
user8175
  • 111
  • 1
  • 5