Hello All:
I am a little stuck on my code. I am new to python and am learning as I go, but have usually been able to figure it out till now. The code was working up till I put the dictionary part in and now this is where its failing. When I run the program, it only goes through once and doesn't appear to be storing the data. I have found people in the discussions have had the same problem, but they never said what helped them resolve the issue. Below is a breakdown of the objective of the program:
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.
Sample 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,
Appreciate any help and guidance anyone has to offer.
<SPAN class="comment token">#Import a csv file into ArcMap and plot the points for each Rhino</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> csv
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\GEOG485\\Lesson4\\Project4"</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>
targetFolder <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\GEOG485\\Lesson4\\Project4"</SPAN>
rhinoShape <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\GEOG485\\Lesson4\\Project4\\Rhino.shp"</SPAN>
rhinoFile <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\GEOG485\\Lesson4\\Project4\\RhinoObservations.csv"</SPAN>
spatialRef <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>rhinoShape<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>spatialReference
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#Open CSV file and read the headers </SPAN>
trackPoints <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN>rhinoFile<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"r"</SPAN><SPAN class="punctuation token">)</SPAN>
textLine <SPAN class="operator token">=</SPAN> trackPoints<SPAN class="punctuation token">.</SPAN>readline<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
headerLine <SPAN class="operator token">=</SPAN> textLine<SPAN class="punctuation token">.</SPAN>strip<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN>
pairList <SPAN class="operator token">=</SPAN> headerLine<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">","</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> pairList
polylineArray <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Get index for header fields </SPAN>
xIndex <SPAN class="operator token">=</SPAN> pairList<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> pairList<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> pairList<SPAN class="punctuation token">.</SPAN>index<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Rhino"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> rhinoIndex
<SPAN class="comment token">#Created an empty dictionary </SPAN>
dictionaryRhinoTracks <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">for</SPAN> line <SPAN class="keyword token">in</SPAN> trackPoints<SPAN class="punctuation token">.</SPAN>readlines<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
line <SPAN class="operator token">=</SPAN> line<SPAN class="punctuation token">.</SPAN>rstrip<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN>
splitLine <SPAN class="operator token">=</SPAN> line<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">","</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> splitLine
<SPAN class="comment token">#Created variable for needed field items based on index location </SPAN>
pointX <SPAN class="operator token">=</SPAN> splitLine<SPAN class="punctuation token">[</SPAN>xIndex<SPAN class="punctuation token">]</SPAN>
pointY <SPAN class="operator token">=</SPAN> splitLine<SPAN class="punctuation token">[</SPAN>yIndex<SPAN class="punctuation token">]</SPAN>
rhinoName <SPAN class="operator token">=</SPAN> splitLine<SPAN class="punctuation token">[</SPAN>rhinoIndex<SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#Create a point object </SPAN>
currentPoint<SPAN class="punctuation token">.</SPAN>x <SPAN class="operator token">=</SPAN> pointX
currentPoint<SPAN class="punctuation token">.</SPAN>y <SPAN class="operator token">=</SPAN> pointY
currentPoint <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>currentPoint<SPAN class="punctuation token">.</SPAN>x<SPAN class="punctuation token">,</SPAN> currentPoint<SPAN class="punctuation token">.</SPAN>y<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> currentPoint
<SPAN class="comment token">#Check if the dictionary does not contain name </SPAN>
<SPAN class="keyword token">if</SPAN> rhinoName <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> dictionaryRhinoTracks<SPAN class="punctuation token">:</SPAN>
rhinoTrackArray <SPAN class="operator token">=</SPAN> polylineArray<SPAN class="punctuation token">.</SPAN>Add<SPAN class="punctuation token">(</SPAN>currentPoint<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Add the point object to the Array dictionaryRhinoTracks[rhinoName].Add(currentPoint) </SPAN>
cursor <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>InsertCursor<SPAN class="punctuation token">(</SPAN>rhinoShape<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>
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="keyword token">for</SPAN> rhinoName <SPAN class="keyword token">in</SPAN> dictionaryRhinoTracks<SPAN class="punctuation token">:</SPAN>
row<SPAN class="punctuation token">.</SPAN>Shape <SPAN class="operator token">=</SPAN> dictionaryRhinoTracks<SPAN class="punctuation token">[</SPAN>rhinoName<SPAN class="punctuation token">]</SPAN>
row<SPAN class="punctuation token">.</SPAN>Rhino_Name <SPAN class="operator token">=</SPAN> rhinoName
cursor<SPAN class="punctuation token">.</SPAN>InsertRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"error again"</SPAN><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>