Can I use row object to select features for geoprocessing

481
5
Jump to solution
10-12-2013 04:50 PM
AliciaMein
New Contributor III
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!
Alicia

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,) 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
AliciaMein
New Contributor III
This is how I got it to work.
I can use the row as an argument, at least in a search cursor.  I dont think it will work in any of the others because the lock the table, feature class or whatever for use.  At least that is my take.  Please someone correct me if I am wrong!
#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 

View solution in original post

0 Kudos
5 Replies
TimBarnes
Occasional Contributor III
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!
Alicia

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,) 


My first instinct is to say 'no' as a row object isn't a feature as such.

What you could do is use the attributes of the current row as a dynamic selection query in a makeFeatureLayer command and then buffer that?
thisFID = row.getValue('FID')
ap.MakeFeatureLayer_management(rtsPath, "selectedLine", "[FID] = thisFID")
ap.Buffer_Analysis ("selectedLine,.....) etc

?
I'm sure there is a more efficient way though!
0 Kudos
AliciaMein
New Contributor III
Thanks, I am anxious to get there.  I have changed my code to get rid of the second cursor, couldn't figure out how to make that work and after reading decided it may have to much overhead.  I went to a dict instead of the search cursor.
I almost have this code working and then I will be able to try to process the routes.

I now getting an error in the for loop.
Runtime error <type 'exceptions.NameError'>: name 'CoZoneName' is not defined
Can you tell me what I am doing wrong now?
Thanks,
Alicia
#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

0 Kudos
TimBarnes
Occasional Contributor III
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!)
0 Kudos
AliciaMein
New Contributor III
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!)


Thanks, this was perfect.  about the two different CoZoneName and COZONENAME, my output table comes back all caps and I can not access the zariable in the dict without using all caps, so that is why you see it that way.  Tonight I am trying to get the buffers done.  I wll let you know what works.  I was hoping not to use a layer because I dont understand the behavior that well.
We will see.
Thanks Again, I really appreciate you reply.  Look to see what I figure out. 🙂
Thanks
Alicia
0 Kudos
AliciaMein
New Contributor III
This is how I got it to work.
I can use the row as an argument, at least in a search cursor.  I dont think it will work in any of the others because the lock the table, feature class or whatever for use.  At least that is my take.  Please someone correct me if I am wrong!
#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 
0 Kudos