I'm trying to loop through a set of lines and select points within .25 miles of them. I just need the count, so I'm trying to put the results into a list I can print out. The relevant code is below.
When I execute the code, it will loop through twice and then print the list called data. There are actually 35 rows it should loop through. If I comment out everything but printing out the name of the route (first line of the loop), it will successfully execute as I intended.
I found a similar question here: Select Layer By Attribute using loop. However, this question simply ended with the poster figuring out that their cursor was tied up. Not sure exactly what that means or how to resolve it.
Why would the loop execute less times than there are rows, but not produce an error?
fc = r'PATH TO FILE'
inlyr = r'PATH TO FILE'
data = {}
field = 'ROUTE'
arcpy.MakeFeatureLayer_management(fc, 'route')
route = 'route'
arcpy.MakeFeatureLayer_management(inlyr, 'addrs')
addrs = 'addrs'
cursor = arcpy.SearchCursor(fc)
for row in cursor:
print "ROUTE = \'{}\'".format(row.getValue(field))
arcpy.SelectLayerByAttribute_management(route, "NEW_SELECTION", "ROUTE = \'{}\'".format(row.getValue(field)))
arcpy.SelectLayerByLocation_management(addrs, 'WITHIN_A_DISTANCE', route, '.25 Miles')
num = arcpy.GetCount_management(addrs)
data.update({row.getValue(field):num})
del num
del cursor
print data
PATH TO FILE
, but to be sure, it's not actually a layer, is it?num = arcpy.GetCount_management(addrs)
will return a result object. You may instead wantnum = arcpy.GetCount_management(addrs).getOutput (0)
arcpy.da.SearchCursor
(which has a completely different syntax and use paradigm).