Select to view content in your preferred language

Square Buffer Script not working

688
3
06-07-2010 11:38 PM
KevinCressy
Emerging Contributor
Hello all,

I am running ArcGIS Descktop 9.3.1 ArcView licence with no extensions.

I am trying to run the python script "Example: Using SearchCursor and InputCursor to create square buffers"  located in the ArcGIS 9.2 online help (see: http://webhelp.esri.com/arcgisdesktop/9.2/index.cfm?TopicName=Writing_geometries) however the script is not working when I run in PythonWin.

- I have modified the script to reference the 9.3 geoprocessing script by changing the line: gp = arcgisscripting.create() to gp = arcgisscripting.create(9.3)
- my input file is a points file
- the arguments I am using are: C:\A5VRM\trees.shp C:\A5VRM\trees1.shp 5 TRUE
- The scrip I am using is given at the end of this email.

The error I get in PythonWin is:

Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\A5VRM\SquareBuffer.py", line 27, in <module>
    field = fields.Next()
AttributeError: 'list' object has no attribute 'Next'

Any idea why this may not be working?

Many thanks,

Kevin

# Import native arcgisscripting module
import arcgisscripting, sys, os
# Create the geoprocessor object
gp = arcgisscripting.create(9.3)

gp.OverWriteOutput = 1

inPoints = sys.argv[1] #Input point feature class
outPolys = sys.argv[2] #Output polygon feature class
bufDist = sys.argv[3] #Buffer distance
keepFields = sys.argv[4] #Boolean type: Maintain fields and field values of the input in the output

# Prepare the output based on whether field and field values are desired in the output
if keepFields:
    # Create empty output polygon feature class that includes fields of the input
    gp.CreateFeatureClass(os.path.dirname(outPolys), os.path.basename(outPolys), "POLYGON",                           inPoints, "", "", inPoints)

    # Create a short-list of fields to ignore when moving fields values from input to output
    ignoreFields = []
    # Use Describe properties to identify the ShapeFieldName and OIDFieldName
    desc = gp.Describe(inPoints)
    ignoreFields.append(desc.ShapeFieldName)
    ignoreFields.append(desc.OIDFieldName)

    # Create a list of fields to use when moving field values from input to output
    fields = gp.ListFields(inPoints)
    field = fields.Next()
    fieldList = []
    while field:
        if field.Name not in ignoreFields:
            fieldList.append(field.Name)
        field = fields.Next()
else:
    # Create empty output polygon feature class without fields of the input
    gp.CreateFeatureClass(os.path.dirname(outPolys), os.path.basename(outPolys), "POLYGON",                           "", "", "", inPoints)

# Open searchcursor
inRows = gp.SearchCursor(inPoints)
inRow = inRows.Next()

# Open insertcursor
outRows = gp.InsertCursor(outPolys)

# Create point and array objects
pntObj = gp.CreateObject("Point")
arrayObj = gp.CreateObject("Array")

while inRow: # One output feature for each input point feature
    inShape = inRow.Shape
    pnt = inShape.GetPart(0)
    # Need 5 vertices for square buffer: upper-right, upper-left, lower-left,
    # lower-right, upper-right. Add and subtract distance from coordinates of
    # input point as appropriate.
    for vertex in [0,1,2,3,4]:
        pntObj.id = vertex
        if vertex in [0,3,4]:
            pntObj.x = pnt.x + bufDist
        else:
            pntObj.x = pnt.x - bufDist
        if vertex in [0,1,5]:
            pntObj.y = pnt.y + bufDist
        else:
            pntObj.y = pnt.y - bufDist
        arrayObj.add(pntObj)

    # Create new row for output feature
    feat = outRows.NewRow()

    # Shift atttributes from input to output
    if keepFields:
        for fieldName in fieldList:
            feat.SetValue(fieldName, inRow.GetValue(fieldName))

    # Assign array of points to output feature
    feat.Shape = arrayObj
    # Insert the feature
    outRows.InsertRow(feat)

    # Clear array of points
    arrayObj.RemoveAll()

    # Get next feature in searchcursor
    inRow = inRows.Next()

# Delete inputcursor
del outRows
0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus
things changed between 9.2 and 9.3, specifically many "list***" methods now return python lists rather than enumerations.  The nice thing is if you change the geoprocessor creation back to
arcgisscripting.create()  #9.2 version
it will run as if 9.2 was used...not much lost either, give it a try it will save rewriting sections of the code
0 Kudos
KevinCressy
Emerging Contributor
I have had some very useful help from Peter McDaid at ESRI UK who has advised that the problem is coming from the input for the bufDist being interpreted as a String rather than a number.

Peter advised to change the line:

bufDist = sys.argv[3] #Buffer distance

to

bufDist = float(sys.argv[3]) #Buffer distance

I did this and it worked.

Thanks Peter!
0 Kudos
AndrewHansford
Frequent Contributor
Hi there,

Was this script only for Points or could it be applied to a line feature.

Could you also post the full script please?

Regards

Andrew
0 Kudos