After opening shapefile I want to use all of its attributes (geometry column, crs, etc), but want to delete the contents of the OrderedDict in the 'properties' key. I want to append new data to it, and write it back to a shapefile. The standard syntax for deleting keys from Python dicts like so

D = {'spam': 2, 'ham': 1, 'eggs': 3}
('eggs', 3)
{'ham': 1, 'spam': 2}

does not apply to the fiona collections object: How can I manipulate a fiona.collection object? Or are those immutable?

  • I think of the fiona collection as a reflection of the underlying data file. So copy the parts you want to a new file.. – BradHards Aug 28 '16 at 5:30

Simply use del

import fiona
shape = fiona.open("ashape.shp")
one = shape.next()
print one
{'geometry': {'type': 'Point', 'coordinates': (-0.7989756722151089, 0.5492957746478874)}, 
'type': 'Feature', 'id': '0', 
'properties': OrderedDict([(u'id', 0), (u'test', 5), (u'hello', u'you')])}
# delete one "field"
del one['properties']['hello']
print one
{'geometry': {'type': 'Point', 'coordinates': (-0.7989756722151089, 0.5492957746478874)}, 
 'type': 'Feature', 'id': '0', 
 'properties': OrderedDict([(u'id', 0), (u'test', 5)])}
# add a new "field"
one['properties']['bb']= "hello"
print one
{'geometry': {'type': 'Point', 'coordinates': (-0.58898847631242, 0.17797695262484003)}, 
'type': 'Feature', 'id': '1', 
'properties': OrderedDict([(u'id', 1), (u'test', 5), ('bb', 'hello')])}

If you want to delete all the key/values

 print one['properties']
 one['properties']['test'] = 1
 OrderedDict([('test', 1)])

But be careful, if you modify the properties, you need to modify also the schema of the original shapefile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.