Looked more closely at problem on how to add data to a shapefile using fiona. Docs for fiona very unclear on how to do this. I know how to add a schema after that looks very unwieldy and can not figure things out. So has anyone just done a join (out side of fiona) and added data to shape file using fiona. A reasonably large file not just one record. I know how to join data just start from there. You have a shape file get the data for the record attributes join it then what? Seems easy. Really just use the bottom part for historical reference.
OK coped shape and added new schema with ordereddict and successfully (I think). Then using append in fiona I just want to add data using ordereddict or something like that to properties data. See code below. The variables are matched up but keep getting error. TypeError: string indices must be integers, not str
I don't know how it wants the data input or something else is the problem.
Below is the code and the ordereddicts I am using. I use the write funtion to add new Schema first then append data to add variables to shapefile. I think that was the way to do it.
bx = 0
with fiona.open(
'/*/adata.shp',
'w',
driver = source_driver,
crs = source_crs,
schema={'geometry': 'Polygon','properties': wschema,}) as c:
print len(c)
print c.schema
close()
0
{'geometry': 'Polygon', 'properties': OrderedDict([(u'scalerank', 'int:4'), (u'featurecla', 'str:30'), (u'labelrank', 'float:16.6'), (u'sovereignt', 'str:254'), (u'sov_a3', 'str:254'), (u'adm0_dif', 'float:16.6'), (u'level', 'float:16.6'), (u'type', 'str:254'), (u'admin', 'str:254'), (u'adm0_a3', 'str:254'), (u'geou_dif', 'float:16.6'), (u'geounit', 'str:254'), (u'gu_a3', 'str:254'), (u'su_dif', 'float:16.6'), (u'subunit', 'str:254'), (u'su_a3', 'str:254'), (u'brk_diff', 'float:16.6'), (u'name', 'str:254'), (u'name_long', 'str:254'), (u'brk_a3', 'str:254'), (u'brk_name', 'str:254'), (u'brk_group', 'str:254'), (u'abbrev', 'str:254'), (u'postal', 'str:254'), (u'formal_en', 'str:254'), (u'formal_fr', 'str:254'), (u'note_adm0', 'str:254'), (u'note_brk', 'str:254'), (u'name_sort', 'str:254'), (u'name_alt', 'str:254'), (u'mapcolor7', 'float:16.6'), (u'mapcolor8', 'float:16.6'), (u'mapcolor9', 'float:16.6'), (u'mapcolor13', 'float:16.6'), (u'pop_est', 'float:16.6'), (u'gdp_md_est', 'float:16.6'), (u'pop_year', 'float:16.6'), (u'lastcensus', 'float:16.6'), (u'gdp_year', 'float:16.6'), (u'economy', 'str:254'), (u'income_grp', 'str:254'), (u'wikipedia', 'float:16.6'), (u'fips_10', 'str:254'), (u'iso_a2', 'str:254'), (u'iso_a3', 'str:254'), (u'iso_n3', 'str:254'), (u'un_a3', 'str:254'), (u'wb_a2', 'str:254'), (u'wb_a3', 'str:254'), (u'woe_id', 'float:16.6'), (u'adm0_a3_is', 'str:254'), (u'adm0_a3_us', 'str:254'), (u'adm0_a3_un', 'float:16.6'), (u'adm0_a3_wb', 'float:16.6'), (u'continent', 'str:254'), (u'region_un', 'str:254'), (u'subregion', 'str:254'), (u'region_wb', 'str:254'), (u'name_len', 'float:16.6'), (u'long_len', 'float:16.6'), (u'abbrev_len', 'float:16.6'), (u'tiny', 'float:16.6'), (u'homepart', 'float:16.6'), ('Country', 'str:254'), ('Index', 'str:38'), ('A2', 'str:254'), ('A3', 'str:254'), ('Code', 'str:38'), ('1', 'str:38'), ('730', 'str:38'), ('1000', 'str:38'), ('1150', 'str:38'), ('1280', 'str:38'), ('1300', 'str:38'), ('1348', 'str:38'), ('1400', 'str:38'), ('1450', 'str:38'), ('1500', 'str:38'), ('1550', 'str:38'), ('1570', 'str:38'), ('1600', 'str:38'), ('1650', 'str:38'), ('1700', 'str:38'), ('1720', 'str:38'), ('1750', 'str:38'), ('1775', 'str:38'), ('1800', 'str:38'), ('1820', 'str:38'), ('1850', 'str:38'), ('1870', 'str:38'), ('1890', 'str:38'), ('1913', 'str:38'), ('1929', 'str:38'), ('1950', 'str:38'), ('1960', 'str:38'), ('1973', 'str:38'), ('1980', 'str:38'), ('1990', 'str:38'), ('2000', 'str:38'), ('2008', 'str:38')])}
with fiona.open('/*/adata.shp', 'a') as cwt:
kr = gh[0]
print kr
cwt.write({'geometry':'Polygon','properties': kr,})
close()
TypeError Traceback (most recent call last) in () 2 kr = gh[0] 3 print kr ----> 4 cwt.write({'geometry':'Polygon','properties': kr,}) 5 close()
/home/matthew/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/fiona/collection.pyc in write(self, record) 209 def write(self, record): 210 """Stages a record for writing to disk.""" --> 211 self.writerecords([record]) 212 213 def validate_record(self, record):
/home/matthew/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/fiona/collection.pyc in writerecords(self, records) 203 if self.mode not in ('a', 'w'): 204 raise IOError("collection not open for writing") --> 205 self.session.writerecs(records, self) 206 self._len = self.session.get_length() 207 self._bounds = self.session.get_extent()
/home/matthew/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/fiona/ogrext.so in fiona.ogrext.WritingSession.writerecs (src/fiona/ogrext.c:15787)()
/home/matthew/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/fiona/ogrext.so in fiona.ogrext.WritingSession.writerecs.validate_geometry_type (src/fiona/ogrext.c:15230)()
TypeError: string indices must be integers, not str
OrderedDict([(u'scalerank', 1), (u'featurecla', u'Admin-0 map subunit'), (u'labelrank', 3.0), (u'sovereignt', u'Afghanistan'), (u'sov_a3', u'AFG'), (u'adm0_dif', 0.0), (u'level', 2.0), (u'type', u'Sovereign country'), (u'admin', u'Afghanistan'), (u'adm0_a3', u'AFG'), (u'geou_dif', 0.0), (u'geounit', u'Afghanistan'), (u'gu_a3', u'AFG'), (u'su_dif', 0.0), (u'subunit', u'Afghanistan'), (u'su_a3', u'AFG'), (u'brk_diff', 0.0), (u'name', u'Afghanistan'), (u'name_long', u'Afghanistan'), (u'brk_a3', u'AFG'), (u'brk_name', u'Afghanistan'), (u'brk_group', None), (u'abbrev', u'Afg.'), (u'postal', u'AF'), (u'formal_en', u'Islamic State of Afghanistan'), (u'formal_fr', None), (u'note_adm0', None), (u'note_brk', None), (u'name_sort', u'Afghanistan'), (u'name_alt', None), (u'mapcolor7', 5.0), (u'mapcolor8', 6.0), (u'mapcolor9', 8.0), (u'mapcolor13', 7.0), (u'pop_est', 28400000.0), (u'gdp_md_est', 22270.0), (u'pop_year', -99.0), (u'lastcensus', 1979.0), (u'gdp_year', -99.0), (u'economy', u'7. Least developed region'), (u'income_grp', u'5. Low income'), (u'wikipedia', -99.0), (u'fips_10', None), (u'iso_a2', u'AF'), (u'iso_a3', u'AFG'), (u'iso_n3', 4), (u'un_a3', u'004'), (u'wb_a2', u'AF'), (u'wb_a3', u'AFG'), (u'woe_id', -99.0), (u'adm0_a3_is', u'AFG'), (u'adm0_a3_us', u'AFG'), (u'adm0_a3_un', -99.0), (u'adm0_a3_wb', -99.0), (u'continent', u'Asia'), (u'region_un', u'Asia'), (u'subregion', u'Southern Asia'), (u'region_wb', u'South Asia'), (u'name_len', 11.0), (u'long_len', 11.0), (u'abbrev_len', 4.0), (u'tiny', -99.0), (u'homepart', 1.0), ('Country', 'Afghanistan'), ('Index', 104L), ('A2', 'AF'), ('A3', 'AFG'), ('Code', 4), ('1', nan), ('730', nan), ('1000', nan), ('1150', nan), ('1280', nan), ('1300', nan), ('1348', nan), ('1400', nan), ('1450', nan), ('1500', nan), ('1550', nan), ('1570', nan), ('1600', nan), ('1650', nan), ('1700', nan), ('1720', nan), ('1750', nan), ('1775', nan), ('1800', nan), ('1820', nan), ('1850', nan), ('1870', nan), ('1890', nan), ('1913', nan), ('1929', nan), ('1950', 644.7561631573), ('1960', 739.4106486121), ('1973', 684.0618506358), ('1980', 689.9746687251), ('1990', 604.0490304301), ('2000', 565.2309015098), ('2008', 868.8580032192)])