Buffering roads. Too many to do all at once so I'm breaking them into groups using forest district boundaries.
Iterating through the districts file, selecting a district, then intersecting to select a group of roads, buffering and dissolving, and then appending to output files.
Getting an error on the second time through the loop to do with the selection of the district.
The first iteration works fine, the second gives me the "invalid expression" error.
I don't understand why it should work the first time through (creates the buffers and saves them fine) and then it decides it doesn't like
the expression to select the district anymore?
My code (no formatting, problem line is highlighted in yellow):
wss = gp.searchcursor(fdistricts)
ws = wss.Next()
fdistrictscount = 1
while ws:
districtID = ws.getvalue('ORG_UNIT')
# Get the unique ID # for the current woodlot.
print 'Iteration # '+str(fdistrictscount)
print ' selecting the forest district'
selectFdistrict = gp.SelectLayerByAttribute(fdistricts, "NEW_SELECTION", '\"ORG_UNIT\" = \'' + str(districtID) + '\'')
print ' selecting all major roads near woodlots within the '+districtID+' district'
majorRoadSelect = gp.SelectLayerByLocation(majorRoads, "INTERSECT", fdistricts)
# Create the buffer polygons around the roads.
print ' creating buffer around the selected major roads'
bufferPoly = gp.Buffer_Analysis(majorRoadSelect, proj_main+tempRoadBuff, "1 Kilometers", "FULL", "ROUND")
print ' dissolving buffer lines based on ROAD_NAME_ID'
tempDissolve = gp.dissolve(bufferPoly, proj_main+tempBuffDissolve, "ROAD_NAM_5")
if fdistrictscount == 1:
print ' saving the buffer of the selected major roads as a new shapefile'
gp.CopyFeatures(bufferPoly, proj_main+output1)
gp.MakeFeatureLayer(proj_main+output1+'.shp', output)
print ' saving the dissolve of the buffers as a new shapefile'
gp.CopyFeatures(tempDissolve, proj_main+output2)
gp.MakeFeatureLayer(proj_main+output2+'.shp', dissolve)
# If not the first iteration then don't create a new road select layer, append to the existing road select layer.
else:
print ' appending the buffer information to the output file'
gp.append(bufferPoly, output, "NO_TEST")
print ' appending the dissolve information to the output file'
gp.append(tempDissolve, dissolve, "NO_TEST")
# Step up the count and return for the next record in the forest district layer and select the
# next group of woodlots.
fdistrictscount = fdistrictscount + 1
print ' Moving on'
print ' '
ws = wss.Next()