AnsweredAssumed Answered

Insert cursor to iterate through dictionary

Question asked by JacobHorwitz on Oct 6, 2018
Latest reply on Oct 6, 2018 by Dan_Patterson

I'm trying to set an arcpy da insert cursor to iterate through a dictionary that adds in the rhino's name. I'm missing a code that would loop through the cursor.

 

def rhinoTracker(Rhino, X, Y, dictionary):

    # Sets the variable for arcpy.Point
    vertex = arcpy.Point(X, Y)

    # If the Rhino is in the dictionary, points are added.
    if Rhino in dictionary:
        vertexArray = dictionary[Rhino]
        vertexArray.add(vertex)
        #print str(dictionary.values())
       
    # If the Rhino is not in the dictionary, an array created.
    else:
        vertexArray = arcpy.CreateObject("Array")
        vertexArray.add(vertex)
        dictionary[Rhino] = vertexArray
        #print str(dictionary.values())
       

    #for Rhino in dictionary:
        print "This the key {0}".format(Rhino)
        print "These are {0}'s points {1}".format(Rhino, dictionary[Rhino])
        print "{0} has {1} points".format(Rhino, str(len(dictionary[Rhino])))


#--------------------------------------------------------------------------

# Imports the csv and arcpy modules.
import csv
import arcpy

# Sets up the workspace environment and overwrite output.
arcpy.env.workspace = "E:\College\Penn State\GEOG 485\Lesson 4\JHorwitz_Lesson4_Submission\Shapefiles"
arcpy.env.overwriteOutput = True

#--------------------------------------------------------------------------

# Sets a path for the folder containing the shapefile.
Path = "E:\College\Penn State\GEOG 485\Lesson 4\JHorwitz_Lesson4_Submission\Shapefiles"

# Uses the "MysteryState.shp" as the spatial reference.
spatialpath = "E:\College\Penn State\GEOG 485\Lesson 4\JHorwitz_Lesson4_Submission\Shapefiles\MysteryState.shp"

# Sets up the "RhinoObservations.csv" as the csv file.
RhinoObservation = open("E:\College\Penn State\GEOG 485\Lesson 4\JHorwitz_Lesson4_Submission\RhinoObservations.csv", "r")

# Creates the Polyline shapefile as "RhinoTrack.shp" using "MysteryState.shp" as the spatial reference and the folder to store the shapefile.
arcpy.CreateFeatureclass_management(Path, "RhinoTrack.shp", "POLYLINE", "", "DISABLED", "DISABLED", spatialpath)

# Creates a new text field for the Rhino polyline.
arcpy.AddField_management("RhinoTrack.shp", "Name", "TEXT")

# Sets up the file path to "RhinoTrack.shp"
rhinoLine = "E:\College\Penn State\GEOG 485\Lesson 4\JHorwitz_Lesson4_Submission\Shapefiles\RhinoTrack.shp"

# Sets up the spatial reference for the rhinoLine.
spatialRef = arcpy.Describe(rhinoLine).spatialReference

#--------------------------------------------------------------------------

# Sets up a csv reader and will process the header.
csvReader = csv.reader(RhinoObservation, delimiter=",")
header = csvReader.next()
xIndex = header.index("X")
yIndex = header.index("Y")
rhinoIndex = header.index("Rhino")

# Creates the rhinoList dictionary.
rhinoList = {}


# Loop through the lines in the csv file to get the coordinates and rhino name.
for row in csvReader:
    Rhino = row[rhinoIndex]
    X = row[xIndex]
    Y = row[yIndex]
    rhinoTracker(Rhino, X, Y, rhinoList)
   
    with arcpy.da.InsertCursor(rhinoLine, ("SHAPE@")) as cursor:
        for Rhino in cursor:
            polyline = arcpy.Polyline(vertexArray, spatialpath)
            cursor.insertRow((polyline,))
       
    # Updates and fills up the new text field as OpenStreetMap using a "with" loop.
    with arcpy.da.UpdateCursor("RhinoTrack.shp", "Name") as cursor:

        #Uses a "for" loop to insert each row.
        for Name in cursor:
            Name[0] = Rhino
            cursor.updateRow(Name)

Between line 79-82, I need to use the insert cursor to iterate through a dictionary and I'm not sure how.

Outcomes