AnsweredAssumed Answered

Script stops unexpectedly.

Question asked by armein01 on Oct 18, 2013
Latest reply on Oct 28, 2013 by armein01
#Script that computes the Percent forsted area from a landcover raster and #county zone data.  Then finds the buffer distance required around sampling #routes, so the buffer frstd percent matches the county frstd percent.  #BufferDist attribute of the route feature class are changed to reflect the #values found by the script.  !! The BufferDist attribute will be changed. #Set environment and declare varibles # arcpy.env.overwriteOutput = True arcpy.env.workspace = "C:/CustomTools/DeerSurveyRoutes/rtsScratch.gdb"  coPath = "C:/CustomTools/DeerSurveyRoutes/RtsAnlysVectors.gdb/County" rasPath = "C:/CustomTools/DeerSurveyRoutes/RtsAnlysRasters.gdb/WVUReclass" opnFrstdRas = arcpy.Raster("C:/CustomTools/DeerSurveyRoutes/RtsAnlysRasters.gdb/WVUReclass") rasCellSz = (opnFrstdRas.meanCellHeight + opnFrstdRas.meanCellWidth) / 2 rtsPath = "C:/CustomTools/DeerSurveyRoutes/RtsAnlysVectors.gdb/SmplRts2012Edited4CountyAnalysis"  #Set environemt and create variables for County area tabulation # arcpy.CheckOutExtension("Spatial") arcpy.env.snapRaster = rasPath znFld = "CoZoneName" clsFld = "Value" outTble = "CoTbleOpenFrst"  # Tabulate area of each class in raster and set up dictionary to hold values in table. # arcpy.sa.TabulateArea(coPath,znFld,opnFrstdRas,clsFld,outTble,rasCellSz) coOFDict = dict([((r.COZONENAME, f.name), r.getValue(f.name)) for f in arcpy.ListFields(outTble) for r in arcpy.SearchCursor(outTble)]) del r, f  #Create dictionary of Rts Containing the CoZoneName, RouteID and BufferDist rtDict = dict([((r.RouteID, f.name), r.getValue(f.name)) for f in arcpy.ListFields(rtsPath) for r in arcpy.SearchCursor(rtsPath)]) del r, f  #Loop to find buffer distance of each route that will contain the same forested percent as the county  return BuffDist # buffCur = arcpy.SearchCursor(rtsPath) #row = buffCur.next()  (not needed) for row in buffCur:     buffDist = 402     frstPrcnt = 0     frstd = coOFDict[(row.getValue(znFld),'VALUE_2')]     opn = coOFDict[(row.getValue(znFld),'VALUE_1')]     nHbt = coOFDict[(row.getValue(znFld),'VALUE_0')]     lpChkVal = frstd / (frstd + opn + nHbt)     minLpChkVal = lpChkVal - (lpChkVal * .05)     maxLpChkVal = lpChkVal + (lpChkVal * .05)     x = 0      #Set up feature layer for Buffer analysis     #thisRtID = row.getValue('RouteID')     #arcpy.MakeFeatureLayer_management(rtsPath, "selectedLine", '"RouteID" = \'' + thisRtID + '\'')  (good where clause example - do not delete)      while frstPrcnt < minLpChkVal or frstPrcnt > maxLpChkVal:              arcpy.Buffer_analysis(row.shape, "lineBuffer", buffDist,"FULL","ROUND","ALL")         arcpy.sa.TabulateArea("lineBuffer","OBJECTID",rasPath,clsFld,"BufferOFTble",rasCellSz)                  rtOFDict = dict([((r.OBJECTID, f.name), r.getValue(f.name)) for f in arcpy.ListFields("BufferOFTble") for r in arcpy.SearchCursor("BufferOFTble")])         del r, f         rtFrstd = rtOFDict[(1,'VALUE_2')]         rtOpn = rtOFDict[(1,'VALUE_1')]         rtNHbt = rtOFDict[(1,'VALUE_0')]         frstPrcnt = rtFrstd / (rtFrstd + rtOpn + rtNHbt)         newBuffVal = buffDist         x += 1         if x == 30:             rtDict[(row.getValue('RouteID'), 'BufferDist')] = 1             break         buffDist = buffDist + (x * 5 * rasCellSz)     rtDict[(row.getValue('RouteID'), 'BufferDist')] = newBuffVal  #Create Cursor to iterate through rts and dictionary to update buffer distance. # updtCur = arcpy.UpdateCursor(rtsPath)  for rows in updtCur:     dist = rtDict[(rows.getValue('RouteID'), 'BufferDist')]     rows.setValue('BufferDist', dist)     updtCur.updateRow(rows) del rows, updtCur, row, buffCur


I suspect that this sort of create, delete and overwrite is not optimal.  I am learning and would appreciate understanding what is going on.
I also realize that I might be better of passing the entire feature class and working through buffering all features instead of just one, but right now I just want to understand some concepts.
I have a feature class containing only 140ish features.  I hoped this would not be to large a burden for my computer.
I developed and tested with two routes just to make sure everything was claculated and completed.  When I took the script and ran it against a file geodatabase on a network the script immediately returned :

Runtime error <type 'exceptions.NameError'>: name 'r' is not defined

This is the variable used in the dictionary to populate the key.

When I ran the script from my C again it processed fairly quickly and returned the same error after processing 130ish records.

Is Why to broad a question? 

I have already changed the end type in the buffer tool from flat to round, because the buffers where eating themselves from the inside out.  I am going to change variable r to unique values in each dict and Id will also implement error checking.  But as is, does anybody have any idea why the script doesnt recognize r?
I appreciate all the help I have recieved.  Any comments are welcome.
Thanks,
Alicia

Outcomes