# Open CSV file containing Rhino track points
trackPointsPath = "C:\wcgis\GEOG485\Project_4\RhinoObservations.csv"
trackPoints = open(trackPointsPath, "r")
# Read first line of CSV file, strip "new line" and make Python List of Header titles
headerLine = trackPoints.readline()
headerLine = headerLine.rstrip("\n")
splitHeaderLine = headerLine.split(",")
# Get index for needed Header fields
indexPointX = splitHeaderLine.index("X")
indexPointY = splitHeaderLine.index("Y")
indexRhinoName = splitHeaderLine.index("Rhino")
# Create an empty dictionary. Each key will be Rhino Name & values points Array.
dictionaryRhinoTracks = {}
# Loop through each line in the CSV file & split with delimiter ","
for line in trackPoints.readlines():
line = line.rstrip("\n")
splitLine = line.split(",")
print splitLine
# Create variable for needed field items based on index position
pointX = splitLine[indexPointX]
pointY = splitLine[indexPointY]
rhinoName = splitLine[indexRhinoName]
# Create a Point object
currentPoint = gp.createobject("Point")
currentPoint.X = pointX
currentPoint.Y = pointY
# Check if the Dictionary contains Name, if yes pass, else createobject Array and add to the dictionary
if rhinoName in dictionaryRhinoTracks:
pass
else:
rhinoTrackArray = gp.createobject("Array")
dictionaryRhinoTracks[rhinoName] = rhinoTrackArray
# Check Keys in Dictionary and add Point Object (X & Y coordinates) to the Array
for key in dictionaryRhinoTracks:
if key == rhinoName:
rhinoTrackArray.Add(currentPoint)]
cursor = gp.InsertCursor(featureClassName)
row = cursor.NewRow()
for rhinoName in dictionaryRhinoTracks:
row.Shape = rhinoTrackArray
row.Rhino_Name = rhinoName
cursor.InsertRow(row)
# Check if the Dictionary does not contain Name, then createobject Array and add to the dictionary
if rhinoName not in dictionaryRhinoTracks:
dictionaryRhinoTracks[rhinoName] = gp.createobject("Array")
# Add Point Object (X & Y coordinates) to the Array
dictionaryRhinoTracks[rhinoName].Add(currentPoint)] #Use the dictionary!
cursor = gp.InsertCursor(featureClassName)
row = cursor.NewRow()
for rhinoName in dictionaryRhinoTracks:
row.Shape = dictionaryRhinoTracks[rhinoName] #Use the dictionary object!
row.Rhino_Name = rhinoName
cursor.InsertRow(row)
# Check if the Dictionary contains Name, if yes pass, else createobject Array and add to the dictionary
if rhinoName in dictionaryRhinoTracks:
pass
else:
rhinoTrackArray = gp.createobject("Array")
dictionaryRhinoTracks[rhinoName] = rhinoTrackArray
# Check Keys in Dictionary and add Point Object (X & Y coordinates) to the Array
for key in dictionaryRhinoTracks:
if key == rhinoName:
rhinoTrackArray.Add(currentPoint)]
cursor = gp.InsertCursor(featureClassName)
row = cursor.NewRow()
for rhinoName in dictionaryRhinoTracks:
row.Shape = rhinoTrackArray
row.Rhino_Name = rhinoName
cursor.InsertRow(row)