Per other users suggestion, you should use Excel to Table GP tool with SearchCursor to construct the dictionary in python. Here is a sample code:
sample data in an excel file:
col1 col2 col3
---------------------
val1 val2 val3
val4 val5 val6
import arcpy
inputExcel = r"D:\Test.xls"
sheetName = "Sheet1"
memoryTable = "in_memory" + "\\" + "memoryTable"
#Make sure the memory is empty
arcpy.Delete_management(memoryTable)
arcpy.ExcelToTable_conversion(inputExcel, memoryTable,sheetName )
d = {}
fieldIndices = {}
fields = arcpy.ListFields(memoryTable)
for i,field in enumerate(fields):
d[field.name]=[]
fieldIndices[i] = field.name
with arcpy.da.SearchCursor(memoryTable, '*') as cursor:
for row in cursor:
for i in range(0,len(row)):
d[fieldIndices[i]].append(row[i])
del cursor
And the output python dictionary:
>>> d
{u'ObjectID': [1, 2],
u'col1': [u'val1', u'val4'],
u'col2': [u'val2', u'val5'],
u'col3': [u'val3', u'val6']}
I hope it helps
At first pass, I see two potential issues you might want to examine.
1) The rhino dictionary contains a list of what?
This line, coordPair = [coordX, coordY]
, says you want a list stored under each item in the rhino dictionary. I bet that works, but I tend to think of pairs as tuples. So try changing the line to:
coordPair = (coordX, coordY)
This says, here's a tuple of X and Y, but we really want the dictionary to store a list, so let's change this bit as well:
def rhinoTrackFinder(rhino, coordX, coordY, dictionary):
if rhino not in dictionary:
dictionary[rhino] = []
dictionary[rhino].append((coordX, coordY))
Note, I removed the coordPair assignment and just append the tuple directly.
So now rhinoTrackFinder will create a dictionary, keyed by rhino, containing a list of tuples.
rhinoTrackDictionary['bob'] might contain: (1,1), (2,4), (-3,7). These tuples should make it easier to read later.
2) What are lon and lat being assigned to?
Further along, you have lon = CoordX
and lat = CoordY
. Python loves global variables, so from what I can tell, you are just setting lon and lat to the last row values from the csvReader, CoordX = row [XCoordIndex]
and CoordY = row [YCoordIndex]
. That's probably not right.
If we use that tuple stuff above, try:
for rhino in rhinoTrackDictionary:
for coord in rhinoTrackDictionary[rhino]:
lon = coord[0]
lat = coord[1]
print rhino, lon, lat
...
We're going from rhino to rhino, and looking at the list of coordinates for that rhino. Then we go coordinate to coordinate.
For each coordinate, looks like you're adding it to the VertexArray, which then you use to create your polyline. That should all work.
Best Answer
You can use a set: