Limit spatial extent of arcpy cursor
How do I limit spatial extent of features that is returned by arcpy.da.SearchCursor? Can I use arcpy.env.extent? Or somehow come up with sql statement to specify spatial relationship? Or do I have to arcpy.SelectLayerByLocation_management to do intersection of my area of interest.
EDIT: Here is what I want to accomplish. Want to visit features from a feature class within a set extent. I was hoping that second use of arcpy.da.SearchCursor respects arcpy.env.extent, but apparently it ignores it.
How can I do this (visit spatial subset of features)? I want to have method with decent efficiency (e.g. avoid copying of features).
import arcpy def proc(): # I/O files fo = open('c:/users/yosuke/documents/log.txt','w') fc = arcpy.CopyFeatures_management('c:/Users/Yosuke/documents/ArcGIS/polytical_boundaries.mdb/tl_2010_us_state10', r'in_memoryfc') # grab everything, works fine arcpy.env.extent = 'MAXOF' n = int(arcpy.GetCount_management(fc).getOutput(0)) m = len(list(arcpy.da.SearchCursor(fc, ('[email protected]',)))) extent = arcpy.Describe(fc).extent # got 52 from both, as expected fo.write('n: %s ' % n) fo.write('m: %s ' % m) fo.write('extent: %s ' % extent) fo.write(' ') # want to grab only western USA arcpy.env.extent = arcpy.Extent(-120, 0, -90, 80) # this counts 27 western states, I suppose. n = int(arcpy.GetCount_management(fc).getOutput(0)) # this still marches through all states, I want this to be spatial subset of USA m = len(list(arcpy.da.SearchCursor(fc, ('[email protected]',)))) extent = arcpy.Describe(fc).extent fo.write('n: %s ' % n) fo.write('m: %s ' % m) fo.write('extent: %s ' % extent) fo.write(' ') # For now I am making copy of feature class # turned out that this is not very bad for my application, other processing # tasks I am applying on cursor rows are more computationally expensive. # I still want to know if this is standard way to accomplish this kind of # tasks (subset cursor by extent) fc2 = arcpy.CopyFeatures_management(r'in_memoryfc', r'in_memoryfc2') n = int(arcpy.GetCount_management(fc2).getOutput(0)) m = len(list(arcpy.da.SearchCursor(fc2, ('[email protected]',)))) extent = arcpy.Describe(fc2).extent fo.write('n: %s ' % n) fo.write('m: %s ' % m) fo.write('extent: %s ' % extent) fo.write(' ')
From what I have tested the extent does not limit the search cursors spatially… When using tools like "GetCount_Management" etc. it is limited but it appears that Cursors do not follow the extent
I haven't tried this out so I may be incorrect but I think the search cursor will honor the env.extent based on these notes in ArcGIS help.
extent (Read and Write)
Tools that honor the Output Extent environment will only process features or rasters that fall within the extent specified in this setting.
Note: Cursors honor layer/table view definition queries and selections. The cursor object only contains the rows that would be used by any geoprocessing tool during an operation.
I'm taking that last sentence to mean if a geoprocessing tool will only process features that fall into the defined extent than cursors will have the same behavior.