[Tutor] Creating a Polyline Feature class
bob gailer
bgailer at gmail.com
Sun Mar 4 05:49:35 CET 2012
On 3/3/2012 11:21 PM, Carie Pigeon wrote:
> I have an assignment to create a polyline feature class of rhino
> tracks from a csv file. I am getting an error that says "unbound
> method add() must be called with Array instance as first argument (got
> type instance)". this is for line 104, the line I underlined and put
> in bold below in my code. What does this mean? If there are other
> obvious code issues, please feel free to comment. I am unsure if my
> code is correct. Thank you for your time!
> Cheers,
> Carie
>
> # Reads rhino positions from an Excel-originating CSV file
>
> import arcpy
> from arcpy import env
> import fileinput
> import string
> import os
>
> env.overwriteOutput = True
>
> # Hard code file paths
> spreadsheet = "C:/WCGIS/Geog485/Lesson4/
> RhinoObservations.csv"
>
> # Sample of data
> ###Observer,X,Y,Rhino,Comments
> ##Ben,26.99391,-19.10447,Bo,
> ##Ben,27.00071,-19.1089,Tulip,Bathing
> ##Ben,26.9919,-19.10511,Bo,
> ##Ben,27.00071,-19.1059,Tulip,
> ##Ben,26.96809,-19.09578,Patches,
> ##Ben,26.97808,-19.11016,Dinky,
> ##Ben,26.99213,-19.10395,Bo,
> ##Ben,27.00083,-19.10326,Tulip,
> ##Ben,26.97038,-19.09863,Patches,Not doing much of anything
> ##Ben,26.97768,-19.11153,Dinky,
> ##Ben,26.99107,-19.10421,Bo,
> ##Ben,27.00138,-19.1021,Tulip,
> ##Ben,26.97122,-19.0991,Patches,
> ##Ben,26.97551,-19.11269,Dinky,
> ##Ben,26.9904,-19.10553,Bo,
> ##Ben,26.99893,-19.10342,Tulip,
>
> # output feature class?? feature class doesn't exist yet, so not sure
> how to do this??
> fcname = "C:/WCGIS/Geog485/Lesson4"
> outname = "C:/WCGIS/Geog485/Lesson4/Rhino.shp"
>
>
>
> # Open the CSV file & read the header line
> observations = open(spreadsheet, "r")
> headerline = observations.readline()
> fieldList = headerline.split(",")
>
> ### 0 1 2 3 4
> ### Observer,X,Y,Rhino,Comments
> # Look through the header to find "X", "Y" and the "Rhino" field indices
> rhinoIndex = fieldList.index("Rhino")
> xIndex = fieldList.index("X")
> yIndex = fieldList.index("Y")
> ## print "Rhino "+str(rhinoIndex) +" X "+str(xIndex)+" Y "+str(yIndex)
>
> # Create a list / array for RhinoTracks
> rhinoTracks = {}
>
> # create the output feature class
> arcpy.CreateFeatureclass_management ("C:/WCGIS/Geog485/Lesson4",
> "Rhino.shp", "POLYLINE")
>
> # open an insert cursor for the new feature class
> cur = arcpy.InsertCursor(outname)
>
> # Loop through the rest of the file to read in all of the rhinos
> for line in observations.readlines():
> ##Ben,26.99391,-19.10447,Bo,
> segmentedLine = line.split(",")
> rhino = segmentedLine[rhinoIndex]
> #print rhino
> # If rhino exists in dictionary - get the array from its key & add
> a point
> if rhino in rhinoTracks:
> coordArray = rhinoTracks[rhino]
> # create a new row or feature, in the feature class
>
>
> ## coordArray { [x,y] }
> lineArray = arcpy.Array()
> coord = arcpy.Point
I am making educated guesses - try coord = arcpy.Point()
> coord.X = segmentedLine[xIndex]
> coord.Y = segmentedLine[yIndex]
> ## add point to coordinate array
> coordArray.add(coord)
> #print "Rhino is "+rhino+" added point
> "+str(segmentedLine[xIndex])+" "+str(segmentedLine[yIndex])
> ## coordArray { [x,y],[x1,y1] }
>
> feat = cur.newRow()
>
> # set the geometry of the new feature to the array of points
> feat.shape = lineArray
>
> #insert the feature
> cur.insertRow(feat)
> lineArray.removeAll()
> lineArray.add(coord)
>
> # If rhino doesn't exist in dictionary - make a new array & add a
> point
> else:
>
> cur = arcpy.InsertCursor(outname)
> ## Create a coordinate array
> coordArray = arcpy.Array
try coordArray = arcpy.Array()
> ## Create a point object
> coord = arcpy.Point
also try coord = arcpy.Point()
> coord.X = segmentedLine[xIndex]
> coord.Y = segmentedLine[yIndex]
> ## add point to coordinate array
> *_coordArray.add(coord)
>
> _* feat = cur.newRow()
>
> # set the geometry of the new feature to the array of points
> feat.shape = lineArray
>
> #insert the feature
> cur.insertRow(feat)
> lineArray.removeAll()
> lineArray.add(coord)
> ## add coordinate array to my Dictionary (list of rhinos)
> rhinoTracks[rhino] = coordArray
Do you understand the difference between
coord = arcpy.Point
and
coord = arcpy.Point()
?
The first gets you a reference to the class.
The 2nd gets you an instance of the class.
Quite different.
--
Bob Gailer
919-636-4239
Chapel Hill NC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120303/c534e166/attachment-0001.html>
More information about the Tutor
mailing list