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.
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">rhinoTracker</SPAN><SPAN class="punctuation token">(</SPAN>Rhino<SPAN class="punctuation token">,</SPAN> X<SPAN class="punctuation token">,</SPAN> Y<SPAN class="punctuation token">,</SPAN> dictionary<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Sets the variable for arcpy.Point</SPAN>
vertex <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>X<SPAN class="punctuation token">,</SPAN> Y<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># If the Rhino is in the dictionary, points are added.</SPAN>
<SPAN class="keyword token">if</SPAN> Rhino <SPAN class="keyword token">in</SPAN> dictionary<SPAN class="punctuation token">:</SPAN>
vertexArray <SPAN class="operator token">=</SPAN> dictionary<SPAN class="punctuation token">[</SPAN>Rhino<SPAN class="punctuation token">]</SPAN>
vertexArray<SPAN class="punctuation token">.</SPAN>add<SPAN class="punctuation token">(</SPAN>vertex<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#print str(dictionary.values())</SPAN>
<SPAN class="comment token"># If the Rhino is not in the dictionary, an array created.</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
vertexArray <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CreateObject<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Array"</SPAN><SPAN class="punctuation token">)</SPAN>
vertexArray<SPAN class="punctuation token">.</SPAN>add<SPAN class="punctuation token">(</SPAN>vertex<SPAN class="punctuation token">)</SPAN>
dictionary<SPAN class="punctuation token">[</SPAN>Rhino<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> vertexArray
<SPAN class="comment token">#print str(dictionary.values())</SPAN>
<SPAN class="comment token">#for Rhino in dictionary:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"This the key {0}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>Rhino<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"These are {0}'s points {1}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>Rhino<SPAN class="punctuation token">,</SPAN> dictionary<SPAN class="punctuation token">[</SPAN>Rhino<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"{0} has {1} points"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>Rhino<SPAN class="punctuation token">,</SPAN> str<SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>dictionary<SPAN class="punctuation token">[</SPAN>Rhino<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#--------------------------------------------------------------------------</SPAN>
<SPAN class="comment token"># Imports the csv and arcpy modules.</SPAN>
<SPAN class="keyword token">import</SPAN> csv
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="comment token"># Sets up the workspace environment and overwrite output.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> <SPAN class="string token">"E:\College\Penn State\GEOG 485\Lesson 4\JHorwitz_Lesson4_Submission\Shapefiles"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token">#--------------------------------------------------------------------------</SPAN>
<SPAN class="comment token"># Sets a path for the folder containing the shapefile.</SPAN>
Path <SPAN class="operator token">=</SPAN> <SPAN class="string token">"E:\College\Penn State\GEOG 485\Lesson 4\JHorwitz_Lesson4_Submission\Shapefiles"</SPAN>
<SPAN class="comment token"># Uses the "MysteryState.shp" as the spatial reference.</SPAN>
spatialpath <SPAN class="operator token">=</SPAN> <SPAN class="string token">"E:\College\Penn State\GEOG 485\Lesson 4\JHorwitz_Lesson4_Submission\Shapefiles\MysteryState.shp"</SPAN>
<SPAN class="comment token"># Sets up the "RhinoObservations.csv" as the csv file.</SPAN>
RhinoObservation <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"E:\College\Penn State\GEOG 485\Lesson 4\JHorwitz_Lesson4_Submission\RhinoObservations.csv"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"r"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Creates the Polyline shapefile as "RhinoTrack.shp" using "MysteryState.shp" as the spatial reference and the folder to store the shapefile.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CreateFeatureclass_management<SPAN class="punctuation token">(</SPAN>Path<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"RhinoTrack.shp"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"POLYLINE"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"DISABLED"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"DISABLED"</SPAN><SPAN class="punctuation token">,</SPAN> spatialpath<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Creates a new text field for the Rhino polyline.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"RhinoTrack.shp"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Name"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"TEXT"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Sets up the file path to "RhinoTrack.shp"</SPAN>
rhinoLine <SPAN class="operator token">=</SPAN> <SPAN class="string token">"E:\College\Penn State\GEOG 485\Lesson 4\JHorwitz_Lesson4_Submission\Shapefiles\RhinoTrack.shp"</SPAN>
<SPAN class="comment token"># Sets up the spatial reference for the rhinoLine.</SPAN>
spatialRef <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>rhinoLine<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>spatialReference
<SPAN class="comment token">#--------------------------------------------------------------------------</SPAN>
<SPAN class="comment token"># Sets up a csv reader and will process the header.</SPAN>
csvReader <SPAN class="operator token">=</SPAN> csv<SPAN class="punctuation token">.</SPAN>reader<SPAN class="punctuation token">(</SPAN>RhinoObservation<SPAN class="punctuation token">,</SPAN> delimiter<SPAN class="operator token">=</SPAN><SPAN class="string token">","</SPAN><SPAN class="punctuation token">)</SPAN>
header <SPAN class="operator token">=</SPAN> csvReader<SPAN class="punctuation token">.</SPAN>next<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
xIndex <SPAN class="operator token">=</SPAN> header<SPAN class="punctuation token">.</SPAN>index<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"X"</SPAN><SPAN class="punctuation token">)</SPAN>
yIndex <SPAN class="operator token">=</SPAN> header<SPAN class="punctuation token">.</SPAN>index<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Y"</SPAN><SPAN class="punctuation token">)</SPAN>
rhinoIndex <SPAN class="operator token">=</SPAN> header<SPAN class="punctuation token">.</SPAN>index<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Rhino"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Creates the rhinoList dictionary.</SPAN>
rhinoList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN>
<SPAN class="comment token"># Loop through the lines in the csv file to get the coordinates and rhino name.</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> csvReader<SPAN class="punctuation token">:</SPAN>
Rhino <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN>rhinoIndex<SPAN class="punctuation token">]</SPAN>
X <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN>xIndex<SPAN class="punctuation token">]</SPAN>
Y <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN>yIndex<SPAN class="punctuation token">]</SPAN>
rhinoTracker<SPAN class="punctuation token">(</SPAN>Rhino<SPAN class="punctuation token">,</SPAN> X<SPAN class="punctuation token">,</SPAN> Y<SPAN class="punctuation token">,</SPAN> rhinoList<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>InsertCursor<SPAN class="punctuation token">(</SPAN>rhinoLine<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"SHAPE@"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> Rhino <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
polyline <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Polyline<SPAN class="punctuation token">(</SPAN>vertexArray<SPAN class="punctuation token">,</SPAN> spatialpath<SPAN class="punctuation token">)</SPAN>
cursor<SPAN class="punctuation token">.</SPAN>insertRow<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>polyline<SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Updates and fills up the new text field as OpenStreetMap using a "with" loop.</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"RhinoTrack.shp"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Name"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#Uses a "for" loop to insert each row.</SPAN>
<SPAN class="keyword token">for</SPAN> Name <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
Name<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> Rhino
cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>Name<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Between line 79-82, I need to use the insert cursor to iterate through a dictionary and I'm not sure how.