Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
    
Might be a silly question, but could you not use Union to do this in one hit? –  om_henners Jun 13 '12 at 12:37
1  
Another silly question, but did you actually try it, and if so what error or behavior did you get? –  blah238 Jun 13 '12 at 18:15

3 Answers 3

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)
share|improve this answer
2  
Agreed, this should work. In 10 they added the ability to use geometry objects in the place of feature layers for most GP tools. –  blah238 Jun 13 '12 at 17:51

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.

share|improve this answer
    
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 7:46

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)

Thanks for helping me!

share|improve this answer
2  
I am pretty sure PolyGeo's answer covered this. You should be careful to note the distinction between a feature class and a feature layer. –  blah238 Jun 16 '12 at 8:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.