I'm getting the following error when trying to add a property to a .shp file using fiona in python:
Traceback (most recent call last):
File "<ipython-input-38-5f04c3b56601>", line 17, in <module>
j=j+1
File "/Users/alex/anaconda/lib/python2.7/site-packages/fiona/collection.py", line 402, in __exit__
self.close()
File "/Users/alex/anaconda/lib/python2.7/site-packages/fiona/collection.py", line 386, in close
self.flush()
File "/Users/alex/anaconda/lib/python2.7/site-packages/fiona/collection.py", line 376, in flush
self.session.sync(self)
File "fiona/ogrext.pyx", line 939, in fiona.ogrext.WritingSession.sync (fiona/ogrext.c:15649)
RuntimeError: Failed to sync to disk
Here is my code – I'm adding a dummy property 'jazz' for now but I have many properties with both float and string types that I want to add once I figure out how to get it to work…
import random
#
import fiona
with fiona.open("data/subplace_maps/SP_SA_2011.shp") as input:
schema2 = input.schema.copy()
schema2['properties']['jazz'.decode('utf-8')] = 'str:50'
temp = input.crs
with fiona.open('data/subplace_maps/output.shp', 'w', crs=input.crs, driver=input.driver, schema=schema2) as sink:
# iterate over shapefile shapes
j = 0
for elem in input:
# update progress
if(j % 500 == 0):
print j,'/',len(input)
#
ja = ('jazz' + str(random.random())).decode('utf-8')
elem['properties'].update(jazz=ja)
#
sink.write(elem)
j=j+1
print 'done'
Best Answer
Fiona works with Python dictionaries (schema, features) and and more specifically Ordered Dictionaries
When you modify the schema of your shapefile you simply add a new key:value to the dictionary
And when you want to add/modify a value in the
jazz
field you simply add/modify the value of the key[jazz]
(=elem['properties']['jazz']
)Control (all is dictionaries)