I have multiple lines (lines) intersecting points (centroids) from which I want to extract landuse values and then write the extracted values to the lines as 'land1' and 'land2' values. I need to iterate through the line feature class in order to transfer the appropriate landuse values. My line layer is made up of 10's thousands of lines so the problem I have is that the select by location operation runs out of memory. I'm using ArcMap 10.0. I have tried processing the dataset in chunks but the problem persists. Why does my 'chunk' approach not work and how can I manage the memory use to avoid this sort of error?
Here is my code:
import arcpy
myfield = "LANDUSE"
obsList = []
obs = arcpy.SearchCursor("lines")
for row in obs:
myid = row.OBJECTID
obsList.append(myid)
del row
del obs
chunk = []
mycount = 0
for x in range(0,len(obsList),1000):
mycount = mycount+1
chunk = obsList[x:x+1000]
chunkmax = chunk[len(chunk)-1]
chunkmin = chunk[0]
WhereClause = """"OBJECTID">={0} and "OBJECTID"<={1}""".format(chunkmin,chunkmax)
sle = arcpy.SelectLayerByAttribute_management("lines", "NEW_SELECTION",WhereClause)
cur = arcpy.UpdateCursor(sle)
for row in cur:
counter = 0
geometry = row.getValue("SHAPE")
fcs = arcpy.SelectLayerByLocation_management("centroids","INTERSECT",geometry)
for feat in arcpy.SearchCursor(fcs):
outval = str(feat.getValue(myfield))
if counter == 0:
row.LAND1 = outval
upcur.updateRow(row)
if counter == 1:
row.LAND2 = outval
upcur.updateRow(row)
counter = counter +1
del row
del cur
del sle