Select to view content in your preferred language

assignment

1900
3
11-27-2011 06:58 AM
Stephaniejohnson2
Emerging Contributor
Parsing rhinoceros sightings

Challenges

The rhinos in the spreadsheet appear in no guaranteed order, and not all the rhinos appear at the beginning of the spreadsheet. As I parse each line, I must determine which rhino the reading belongs to and update that rhino's polyline track accordingly. I am not allowed to sort the Rhino column in Excel before I export to the CSV file. My script must be "smart" enough to work with an unsorted spreadsheet in the order that the records appear.
I do not immediately know how many rhinos are in the file or even what their names are. Although I could visually comb the spreadsheet for this information and hard-code each rhino's name, your script is required to handle all the rhino names programmatically. The idea is that I should be able to run this script on a different file, possibly containing more rhinos, without having to make many manual adjustments.
If I do things right, my polylines should look like this (points are included only for reference):

I'm having the most problems with the dictionaries

part of my code so far but no luck:





rhinoName = {('Rhino'):(Rhino, array)}: 
    for each Rhino in rhinoname:
        dictionary[rhinoName].append([latValue, lonValueIndex])

    if rhinoName not in dictionary:
        dictionary[rhinoName] = []
       
    else:
        dictionary[rhinoName]= ([latValue, lonValue])
Tags (2)
0 Kudos
3 Replies
CariePigeon
Deactivated User
I have this same assignment and am receiving an error saying "unbound method add() must be called with Array instance as first parameter" (got type instance instead).  I am not sure what this means.  Here is my code below.  The error is for the second time I called "coordArray.add(coord)".  Also, if there are any other obvious problems with my code (although this is just a scratch draft so far) feel free to comment.  I have yet to have the script deal with the spatial reference and the text "name" field as required by the assignment.  Thank you for your help!

# Reads rhino positions from an Excel-originating CSV file
#  and writes them to a pre-existing shapefile
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
0 Kudos
JohnMaddox
New Contributor
Were you able to figure it out?
0 Kudos
JasonScheirer
Esri Alum
Things like coordArray = arcpy.Array should really be coordArray = arcpy.Array(), note the parens at the end.
0 Kudos