Im Trying to use a loop to create seperate buffers for each row from attribute values

2547
8
Jump to solution
09-08-2012 03:30 PM
OLANIYANOLAKUNLE
Occasional Contributor II
Im Trying to use a loop to create seperate buffers for each of the row from attribute values of a field in a  feature using searchcursor, but its not working the code i used is below;

mxd = arcpy.mapping.MapDocument("Current") df = arcpy.mapping.ListDataFrames(mxd)[0] fc = "Parcels" field = "Plot_No" cursor = arcpy.SearchCursor(fc) for row in cursor:     val = row.getValue(field) for val in fc:     outFC = str(val) + "_Buffer" + ".shp"     arcpy.Buffer_analysis(str(val),outFC,"15 meters","FULL","ROUND","NONE")


The error message

Runtime error  Traceback (most recent call last):   File "<string>", line 3, in <module>   File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\analysis.py", line 687, in Buffer     raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Features: Dataset K does not exist or is not supported Failed to execute (Buffer).  


Please what im i doing wrong? Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
OLANIYANOLAKUNLE
Occasional Contributor II
Thanks i found a solution for my issues

See code Below;

mxd = arcpy.mapping.MapDocument("Current")         df = arcpy.mapping.ListDataFrames(mxd)[0]         fc = 'Database Connections/TDPMode.sde/KWAGISMAIN.DBO.Parcels'         field = "OBJECTID"         field1 = "LGA"         field2 = "District"         field3 = "Block_No"         field4 = "Plot_No"         rows = arcpy.SearchCursor(fc)         row = rows.next()         while row:             val = row.getValue(field)             val1 = row.getValue(field1)             val2 = row.getValue(field2)             val3 = row.getValue(field3)             val4 = row.getValue(field4)             whereClause = '"OBJECTID"' + " = '" + str(val) + "'"             outName = "Block_" + str(val3) + "_Plot_" + str(val4) + "_" + str(val2) + "_Area_of_" + str(val1) + "_LGA" + ".pdf"             path = "C:/Users/Administrator/Documents/ArcGIS/Default.gdb/"             arcpy.SelectLayerByAttribute_management("Parcels", "NEW_SELECTION", whereClause)             arcpy.Buffer_analysis ("Parcels", "DataDrivenPage_Buffer", "14 meters", "FULL", "ROUND", "NONE")             layer_list = "Lines","Parcels","Access_Road_Graphics","Points","Lines_Split"             for layer in layer_list:                 arcpy.SelectLayerByLocation_management(layer, "COMPLETELY_WITHIN", "DataDrivenPage_Buffer", "", "NEW_SELECTION")             exportLayer = "Points", "Lines", "Parcels","Lines_Split","Access_Road_Graphics"             for layer in exportLayer:                 outFC = path + layer + "_New"                  arcpy.Clip_analysis(layer,"DataDrivenPage_Buffer",outFC)                 arcpy.SelectLayerByAttribute_management(layer, "CLEAR_SELECTION")             mxd = arcpy.mapping.MapDocument("current")             lyr = arcpy.mapping.ListLayers(mxd, "Lines_New")[0]             for lblClass in lyr.labelClasses:                 lblClass.SQLQuery = '"ParcelID"' + "=" + str(val)                  arcpy.RefreshActiveView()             arcpy.SelectLayerByAttribute_management("DataDrivenPage_Buffer", "NEW_SELECTION", "OBJECTID = 1")             df.zoomToSelectedFeatures()             mxd = arcpy.mapping.MapDocument("Current")             for df in arcpy.mapping.ListDataFrames(mxd):                 df.rotation = 0             if df.scale <= 400:                  df.scale = 500             if df.scale > 400 and df.scale < 1000:                 df.scale = 1000                 else:                  df.scale = 2000             arcpy.mapping.ExportToPDF(mxd,r"C:\STATE\\Batch\\" + "TDP_For_" + outName)             row = rows.next()


I think i'm improving.....:D

View solution in original post

0 Kudos
8 Replies
FabianBlau
Occasional Contributor II
I guess you missunderstood the for-loop.
The second loop is outside the first.
You overwrite the val-variable in the loop.
fc holds only the name of the featureclass. Its a string/text.
for val in fc gives you the characters of the fc-name.
Try this (I did not test it):
fc = "Parcels"
field = "Plot_No"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    feat = row.getValue(shapefieldname) # this gives you the geometry
    val = row.getValue(field) 
    outFC = str(val) + "_Buffer" + ".shp" # there should be no Null-values in "Plot_No", the values have to be unique
    arcpy.Buffer_analysis(feat , outFC, "15 meters", "FULL", "ROUND", "NONE")
0 Kudos
markdenil
Occasional Contributor III
It may be simpler to run Buffer using your buffer_field
then using a search cursor to compile a list of buffer field values

loop through the list to select and export the buffers with each value
(make Layer then copy features)

That may be simpler than passing each feature individually to Buffer....
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Thanks for your response, but after trying the code it returns only 1 feature? and i want it to return a 15meters buffer for all the records

>>> fc = "Parcels"
>>> field = "Plot_No"
>>> shapefieldname = "Shape"
>>> cursor = arcpy.SearchCursor(fc)
>>> for row in cursor:
...     feat = row.getValue(shapefieldname)
...     val = row.getValue(field)
...     outFC = str(val) + "_Buffer" + ".shp"
...     
>>> arcpy.Buffer_analysis(feat , outFC, "15 meters", "FULL", "ROUND", "NONE")
<Result 'C:\\Users\\Administrator\\Documents\\ArcGIS\\8_Buffer.shp'>


What is wrong?
0 Kudos
FabianBlau
Occasional Contributor II
move the buffer-function into the loop.
The result will be a shapefile for every single row.
If your Plot_No-Field contains non-unique values you should mention marks answer.
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
move the buffer-function into the loop.
The result will be a shapefile for every single row.
If your Plot_No-Field contains non-unique values you should mention marks answer.


Thanks that did it!
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
@ fabl and mdenil, thanks for your response...
I want to use the code you suggested to create a list of geoprocessing operations on each of the rows in a feature. I would start with the first row and generate a final pdf output, then move to the next row till the searchcursor is through with all the rows; im thinking along the line of the code you suggested but im not sure if it would take each of the rows one at a time, below is an example of what i want


fc = "Parcels"
field = "Plot_No"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    feat = row.getValue(shapefieldname) # this gives you the geometry
    val = row.getValue(field) 
    outFC = str(val) + "_Buffer" + ".shp" # there should be no Null-values in "Plot_No", the values have to be unique
    arcpy.Buffer_analysis(feat , outFC, "15 meters", "FULL", "ROUND", "NONE")
    dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight,     df.extent.upperLeft]),df.spatialReference)
    arcpy.Clip_analysis(lyr,dfAsFeature,outFC) 
    arcpy.mapping.ExportToPDF(mxd,r"C:\KWARA_STATE\\" + outName + "\\" + outName1 + "\\" + "TDP_For_" + outName2) 


fc = "Parcels"
field = "Plot_No"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    feat = row.getValue(shapefieldname) # this gives you the geometry
    val = row.getValue(field) 
    outFC = str(val) + "_Buffer" + ".shp" # there should be no Null-values in "Plot_No", the values have to be unique
    arcpy.Buffer_analysis(feat , outFC, "15 meters", "FULL", "ROUND", "NONE")
    dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight,     df.extent.upperLeft]),df.spatialReference)
    arcpy.Clip_analysis(lyr,dfAsFeature,outFC) 
    arcpy.mapping.ExportToPDF(mxd,r"C:\KWARA_STATE\\" + outName + "\\" + outName1 + "\\" + "TDP_For_" + outName2)


Im not particularly sure if i have to repeat each of the geoprocessing operations for each row, the problem is how do i determine the no of rows? the no of rows would not be fix it can change. Your suggestions would be highly appreciated.
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Morning, @ fabl and mdenil im still waiting for any possibility from my last request. Thank you
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Thanks i found a solution for my issues

See code Below;

mxd = arcpy.mapping.MapDocument("Current")         df = arcpy.mapping.ListDataFrames(mxd)[0]         fc = 'Database Connections/TDPMode.sde/KWAGISMAIN.DBO.Parcels'         field = "OBJECTID"         field1 = "LGA"         field2 = "District"         field3 = "Block_No"         field4 = "Plot_No"         rows = arcpy.SearchCursor(fc)         row = rows.next()         while row:             val = row.getValue(field)             val1 = row.getValue(field1)             val2 = row.getValue(field2)             val3 = row.getValue(field3)             val4 = row.getValue(field4)             whereClause = '"OBJECTID"' + " = '" + str(val) + "'"             outName = "Block_" + str(val3) + "_Plot_" + str(val4) + "_" + str(val2) + "_Area_of_" + str(val1) + "_LGA" + ".pdf"             path = "C:/Users/Administrator/Documents/ArcGIS/Default.gdb/"             arcpy.SelectLayerByAttribute_management("Parcels", "NEW_SELECTION", whereClause)             arcpy.Buffer_analysis ("Parcels", "DataDrivenPage_Buffer", "14 meters", "FULL", "ROUND", "NONE")             layer_list = "Lines","Parcels","Access_Road_Graphics","Points","Lines_Split"             for layer in layer_list:                 arcpy.SelectLayerByLocation_management(layer, "COMPLETELY_WITHIN", "DataDrivenPage_Buffer", "", "NEW_SELECTION")             exportLayer = "Points", "Lines", "Parcels","Lines_Split","Access_Road_Graphics"             for layer in exportLayer:                 outFC = path + layer + "_New"                  arcpy.Clip_analysis(layer,"DataDrivenPage_Buffer",outFC)                 arcpy.SelectLayerByAttribute_management(layer, "CLEAR_SELECTION")             mxd = arcpy.mapping.MapDocument("current")             lyr = arcpy.mapping.ListLayers(mxd, "Lines_New")[0]             for lblClass in lyr.labelClasses:                 lblClass.SQLQuery = '"ParcelID"' + "=" + str(val)                  arcpy.RefreshActiveView()             arcpy.SelectLayerByAttribute_management("DataDrivenPage_Buffer", "NEW_SELECTION", "OBJECTID = 1")             df.zoomToSelectedFeatures()             mxd = arcpy.mapping.MapDocument("Current")             for df in arcpy.mapping.ListDataFrames(mxd):                 df.rotation = 0             if df.scale <= 400:                  df.scale = 500             if df.scale > 400 and df.scale < 1000:                 df.scale = 1000                 else:                  df.scale = 2000             arcpy.mapping.ExportToPDF(mxd,r"C:\STATE\\Batch\\" + "TDP_For_" + outName)             row = rows.next()


I think i'm improving.....:D
0 Kudos