AnsweredAssumed Answered

Iterate features and select by location

Question asked by jeff.thomasilm on Nov 13, 2018
Latest reply on Nov 14, 2018 by jborgion

I've seen many similar questions but somehow never found an answer that I can understand and apply to my problem. I have a layer with Census tracts. I have a layer with parcels. I need to iterate through each tract, select the parcels falling inside, do some things to the parcels (for the sake of testing, I'm trying to copy the parcels in each tract into a new feature class named with the tract number), then repeat for the next tract. The loop works, insofar as it runs through each tract and creates a new feature class that is named with the tract number. But rather than each feature class containing only the parcels falling inside the respective tract, each resulting feature class includes all parcels falling inside all tracts. I'm clearly missing some important construct in my loop. Please help!

 

with arcpy.da.SearchCursor("tracts", "TRACT_NUMBER") as cursor:
  for row in cursor:
    arcpy.SelectLayerByLocation_management("parcels","HAVE_THEIR_CENTER_IN","tracts")
    arcpy.CopyFeatures_management("parcels", "parcels_"+"{}".format(row[0]))

Outcomes