I am attempting to use fiona and xlrd to convert a complicated excel spreadsheet to a shapefile (point). I am trying to figure out how, during the .write
call, to send the list of coordinates I've pulled out of the spreadsheet and use them to draw the geometry. Here's an abbreviation of what I've got so far:
shapefile_dir = r'C:\Temp'
point_shapefile = os.path.join(shapefile_dir, '{}_export_p.shp'.format(datetime.now().strftime('%Y-%m-%d %H:%m')))
schema_properties = {
'guid': 'str:50'
'dateTime': 'str:16'
}
point_schema = {'geometry': 'Point', 'properties': schema_properties}
with fiona.open(point_shapefile, 'w', crs=from_epsg(4326), driver='ESRI Shapefile', schema=point_schema) as layer:
with xlrd.open_workbook(wkbk) as wb:
cs = wb.sheet_by_name('Master')
keys = [cs.cell(0, col_index).value for col_index in xrange(cs.ncols)]
dict_list = []
for row_index in xrange(1, cs.nrows):
d = {keys[col_index]: cs.cell(row_index, col_index).value for col_index in xrange(cs.ncols)}
dict_list.append(d)
for idict in dict_list:
try:
guid = idict['A']
dateTime = idict['DATE']
declat = idict['DECLAT']
declon = idict['DECLON']
try:
layer.write({'geometry': (declat, declon), 'properties': {
'guid': guid,
'dateTime': dateTime
except: #except clause for writing the geometry and attributes
except: #except clause for getting values out of dictionary list
I'm a little overboard with xlrd because I need this to scale to read a workbook with n number of sheets.
But currently I'm getting an error
tuple indices must be integers, not str
Which of course means I'm missing something very basic.
My values are being fetched from the excel spreadsheet correctly, so I think I'm almost there. But I'm not sure I'm thinking through this procedure correctly. Should I be stashing the xlrd
operations inside of the fiona
write loop? How do I get from that list of XYs, cast them to float
and then write them as a new row in the shapefile?
I reviewed Fiona - Preffered method for defining a schema
but I guess I'm not clear on how to adapt some of the methods in there to my own data.
declat
anddeclon
though.