Skip to main content
Add docs, make code functional
Source Link

All you need to do is use a Cursor spatial_filter:

The issue with your code is that the select by attributes does nothing to a Cursor. Selecting a layer modifies the OID set of the layer object and the cursor only looks at the raw SQL table.

If you want to filter a Cursor, you need to use spatial filters and where clauses:

Cursor Spatial Filter documentation

Cursor Where Clause documentation

Here's some updated code from my unedited answer that solves exactly the problem you had. The making a layer and copying features can be run in a separate operation to the flagging, so I made it into a function.

import arcpy
import os
     
def flag_parcel(parcel_path: os.PathLike, buffer: arcpy.Polyline):
    desc = arcpy.Describe(parcel_path)
    if 'BufferParcels' in [f.name for f in desc.fields]:
        arcpy.management.AddFieldDeleteField(resParcels,"BufferParcels"parcel_path,"SHORT" 'BufferParcels')
    
    # Create the field and set it to 0 (default)
    arcpy.management.MakeFeatureLayerAddField(resParcelsparcel_path,"layerParcels" 'BufferParcels', 'SHORT')
    
    # Set the BufferParcels field to 1 for all parcels that intersect the buffer
    with arcpy.da.UpdateCursor(resParcelsparcel_path, ['BufferParcels'], spatial_filter=bigBufferspatial_filter=buffer) as cursor:
        for row in cursor:
            row = dict(zip(cursor.fields, row))
            row['BufferParcels'] = 1
 
## This is really not needed because the default value of a newcursor.updateRow(list(row.values()))
 SHORT field is 0
withdef main():
    resParcels = r"<path>"
    bigBuffer: arcpy.daPolygon = arcpy.UpdateCursorPolygon()
    flag_parcel(resParcels, ['BufferParcels']bigBuffer)
    
    # If you have multiple buffers, where_clause='BufferParcelsyou <>can 1')use asthe cursorfollowing code to flag parcels for each buffer
    # bigBuffers: List[arcpy.Polygon] = [arcpy.Polygon(), arcpy.Polygon(), ...]
    # for rowbuffer in cursorbigBuffers:
    #    row = dict(zipflag_parcel(cursor.fieldsresParcels, row)buffer)
    #  
   row['BufferParcels'] # Or Merge the buffers into a single buffer
    # bigBuffer: arcpy.Polygon = 0[bigBuffers[0].union(buf) for buf in bigBuffers[1:]][0]
    # flag_parcel(resParcels, bigBuffer)

if __name__ == "__main__":
    main(

All you need to do is use a Cursor spatial_filter

arcpy.management.AddField(resParcels,"BufferParcels","SHORT")
arcpy.management.MakeFeatureLayer(resParcels,"layerParcels")

with arcpy.da.UpdateCursor(resParcels, ['BufferParcels'], spatial_filter=bigBuffer) as cursor:
    for row in cursor:
        row = dict(zip(cursor.fields, row))
        row['BufferParcels'] = 1
 
## This is really not needed because the default value of a new SHORT field is 0
with  arcpy.da.UpdateCursor(resParcels, ['BufferParcels'], where_clause='BufferParcels <> 1') as cursor:
    for row in cursor:
        row = dict(zip(cursor.fields, row))
        row['BufferParcels'] = 0

All you need to do is use a Cursor spatial_filter

All you need to do is use a Cursor spatial_filter:

The issue with your code is that the select by attributes does nothing to a Cursor. Selecting a layer modifies the OID set of the layer object and the cursor only looks at the raw SQL table.

If you want to filter a Cursor, you need to use spatial filters and where clauses:

Cursor Spatial Filter documentation

Cursor Where Clause documentation

Here's some updated code from my unedited answer that solves exactly the problem you had. The making a layer and copying features can be run in a separate operation to the flagging, so I made it into a function.

import arcpy
import os
     
def flag_parcel(parcel_path: os.PathLike, buffer: arcpy.Polyline):
    desc = arcpy.Describe(parcel_path)
    if 'BufferParcels' in [f.name for f in desc.fields]:
        arcpy.management.DeleteField(parcel_path, 'BufferParcels')
    
    # Create the field and set it to 0 (default)
    arcpy.management.AddField(parcel_path, 'BufferParcels', 'SHORT')
    
    # Set the BufferParcels field to 1 for all parcels that intersect the buffer
    with arcpy.da.UpdateCursor(parcel_path, ['BufferParcels'], spatial_filter=buffer) as cursor:
        for row in cursor:
            row = dict(zip(cursor.fields, row))
            row['BufferParcels'] = 1
            cursor.updateRow(list(row.values()))
    
def main():
    resParcels = r"<path>"
    bigBuffer: arcpy.Polygon = arcpy.Polygon()
    flag_parcel(resParcels, bigBuffer)
    
    # If you have multiple buffers, you can use the following code to flag parcels for each buffer
    # bigBuffers: List[arcpy.Polygon] = [arcpy.Polygon(), arcpy.Polygon(), ...]
    # for buffer in bigBuffers:
    #     flag_parcel(resParcels, buffer)
    #  
    # Or Merge the buffers into a single buffer
    # bigBuffer: arcpy.Polygon = [bigBuffers[0].union(buf) for buf in bigBuffers[1:]][0]
    # flag_parcel(resParcels, bigBuffer)

if __name__ == "__main__":
    main(
Source Link

arcpy.management.AddField(resParcels,"BufferParcels","SHORT")
arcpy.management.MakeFeatureLayer(resParcels,"layerParcels")

with arcpy.da.UpdateCursor(resParcels, ['BufferParcels'], spatial_filter=bigBuffer) as cursor:
    for row in cursor:
        row = dict(zip(cursor.fields, row))
        row['BufferParcels'] = 1

## This is really not needed because the default value of a new SHORT field is 0
with  arcpy.da.UpdateCursor(resParcels, ['BufferParcels'], where_clause='BufferParcels <> 1') as cursor:
    for row in cursor:
        row = dict(zip(cursor.fields, row))
        row['BufferParcels'] = 0

All you need to do is use a Cursor spatial_filter