[Tutor] Creating a Polyline Feature class

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] Creating a Polyline Feature class

Carie Pigeon
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
        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
        ## Create a point object
        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

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Creating a Polyline Feature class

Bob Gailer
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

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Creating a Polyline Feature class

Dave Angel-3
In reply to this post by Carie Pigeon
I see Bob Gailer has responded to your message while I was still trying
to make sense of it.  His "guesses" are probably right on.

I have no knowledge of arcpy, so I can't directly answer your query.  
But I may be able to help you formulate a question that will solicit
some answers.


On 03/03/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
> <SNIP>
>         coordArray.add(coord)

There's no underline or bold here;  this mailing list is a text based
one, and attributes are dropped for most of us.
If you want to tag a particular line, use a comment in the code, or put
a row of dashes after it, or something.

If you import a non-standard library, then give a web link so that
interested people could actually look it up.  In addition, tell the
version that you're using.

Similarly, you need to supply the python version, and probably the OS
environment Windows 94, Atari 4.3, ...)

When giving error messages, supply the whole stacktrace, not just the
abbreviated error.  The stacktrace would have shown the line that
actually triggered the error.  And in more complex code, it also would
have shown the functions that called the one that had the error.

As you surmise, there seem to be a number of problems with the code.  
But judicious use of print statements could have probably given you a
lot of clues as to what was wrong.



--

DaveA

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Creating a Polyline Feature class

Bob Gailer
In reply to this post by Bob Gailer
Please always reply-all so a copy goes to the list. I' copying the list
on this reply.
Also we like it better when you put your responses in the body of the
email close to what they apply to rather than at top.
Also remove irrelevant text to keep the emails short and to the point.

On 3/4/2012 5:39 PM, Carie Pigeon wrote:

> Ok, cool, that makes sense!  Thank you for pointing that out.  I ran
> it with that correction...but now I am getting another error...
> "Traceback (most recent call last):
>   File
> "C:\Python26\ArcGIS10.0\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 312, in RunScript
>     exec codeObject in __main__.__dict__
>   File "C:\WCGIS\Geog485\Lesson4\Lesson4.py", line 109, in <module>
>     feat.shape = arcpy.Array()
>   File "C:\Program Files
> (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 35, in
> __setattr__
>     return setattr(self._arc_object, attr, ao)
> RuntimeError: ERROR 999999: Error executing function."
>
> Any ideas?  I'm feeling a little lost at this point.

Diagnosing that requires a knowledge of ArcGIS which I lack.

--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor