rows = ap.UpdateCursor(rtsPath) for row in rows: rows2 = ap.SearchCursor(outTble, '"CoZoneName" = row.CoZoneName') buffDist = 402 #This will be meters as all my inputs are UTM frstdPrcnt = 0 lpChkVal = (rows2[2] / (rows2[2] + rows2[3] + rows[4])) minLpChkVal = lpChkVal - lpChkVal * .05 maxLpChkVal = lpChkVal + lpChkVal * .05 x = 0 while frstdPrcnt < minLpChkVal or frstdPrcnt > maxLpChkVal: ap.Buffer_Analysis(row,)
Solved! Go to Solution.
#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() 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 + '\'') while frstPrcnt < minLpChkVal or frstPrcnt > maxLpChkVal: arcpy.Buffer_analysis(row.shape, "lineBuffer", buffDist,"FULL","FLAT","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 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
There is more but I know the rest is working.
I am wondering if I am on the right track.
So I took a raster (containing values of forested, open, road, nonhabitat) and tabulated the area by county into a table (outTble). I want to find the buffer distance around line features (rtsPath) that has the same forested percent (frstPrcnt) as the county. In the while loop I will increase the buffer by 5 X the cell size until the frstdPrcnt is found then I will populate the rtsPath with the approriate buffer distance.
Wondering about my syntax and can I use the row as input into the geoprocessing tool or am I way off?
Thanks!
Aliciarows = ap.UpdateCursor(rtsPath) for row in rows: rows2 = ap.SearchCursor(outTble, '"CoZoneName" = row.CoZoneName') buffDist = 402 #This will be meters as all my inputs are UTM frstdPrcnt = 0 lpChkVal = (rows2[2] / (rows2[2] + rows2[3] + rows[4])) minLpChkVal = lpChkVal - lpChkVal * .05 maxLpChkVal = lpChkVal + lpChkVal * .05 x = 0 while frstdPrcnt < minLpChkVal or frstdPrcnt > maxLpChkVal: ap.Buffer_Analysis(row,)
#Set environment and declare varibles ... # ... arcpy.env.overwriteOutput = True ... arcpy.env.workspace = "C:/CustomTools/DeerSurveyRoutes/rtsScratch.gdb" ... arcpy.env.cellSize = rasPath ... ... 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 ... ... #Tabulate Forested/Open for county ... # ... arcpy.CheckOutExtension("Spatial") ... arcpy.env.snapRaster = rasPath ... znFld = "CoZoneName" ... clsFld = "Value" ... outTble = "CoTbleOpenFrst" ... ... arcpy.sa.TabulateArea(coPath,znFld,opnFrstdRas,clsFld,outTble,rasCellSz) ... ... #Create Cursor to iterate through rts and dictionary to hold county forested/open value ... # ... updtCur = arcpy.UpdateCursor(rtsPath) ... ... coOPDict = dict([((r.COZONENAME, f.name),r.getValue(f.name)) for f in arcpy.ListFields(outTble) for r in arcpy.SearchCursor(outTble)]) ... ... #Loop to find buffer distance of each route that will contain the same forested percent as the county return BuffDist ... # ... for row in updtCur: ... buffDist = 402 #This will be meters as all my inputs are projected to UTM ... frstPrcnt = 0 ... frstd = coOPDict[(row.getValue(CoZoneName),'VALUE_2')] ... opn = coOPDict[(row.getValue(CoZoneName),'VALUE_1')] ... nHbt = coOPDict[(row.getValue(CoZoneName),'VALUE_0')] ... lpChkVal = frstd / (frstd + opn + nHbt) ... minLpChkVal = lpChkVal - (lpChkVal * .05) ... maxLpChkVal = lpChkVal + (lpChkVal * .05) ... x = 0 ... ... ... print frstd
1) Variables are case sensitive- You have defined both a CoZoneName and a COZONENAME- Is that valid? (i.e. do you want two?)
2) In the loop, you refer to the name of the field (CoZoneName) rather than the variable assigned to it (znFld)- I think it should be frstd = coOPDict[(row.getValue(znFld),'VALUE_2')]
(I'm certainly no python expert so may be way off...but it's always good to get some fresh eyes on your code!)
#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() 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 + '\'') while frstPrcnt < minLpChkVal or frstPrcnt > maxLpChkVal: arcpy.Buffer_analysis(row.shape, "lineBuffer", buffDist,"FULL","FLAT","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 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