Using shapely's dissolve
method I created new geometries from two existing shapefiles. Now I would like to save those new geometries and their properties with Fiona.
I have tried to write the properties in Dict as well as OrderdDict format, but still keep receiving Type error: unhashable type: 'Dict'
or Type error: unhashable type: 'collections.OrderedDict'
.
I consulted several sources, including:
- Using Fiona to write a new shapefile from scratch
- Fiona - Preffered method for defining a schema
- Fiona Manual: https://fiona.readthedocs.io/en/latest/manual.html
1 Data structure
for poly_element in difference_geometry:
diff_feature = {
"geometry" : {
'type': 'Polygon',
'coordinates': poly_element,
},
1a - OrderedDict
# ordered dictionary
"properties" : OrderedDict([
("area_code", difference_area_code),
("area_name", difference_area_name),
("area_type", difference_area_type),
]),
}
shapely_geometries.append(diff_feature)
1b - Dict
# dictionary
"properties" : {
"area_code" : difference_area_code,
"area_name" : difference_area_name,
"area_type" : difference_area_type,
},
shapely_geometries.append(diff_feature)
2 Output parameters
stat_area_crs = from_epsg(28992)
output_driver = "ESRI Shapefile"
2a - OrderedDict
stat_area_schema = {
'geometry' : 'Polygon',
'properties' : OrderedDict([
('area_code', 'str'),
('area_name', 'str'),
('area_type', 'str')
])
}
2b - Dict
stat_area_schema = {
'geometry' : 'Polygon',
'properties' : {
'area_code' : 'str',
'area_name' : 'int',
'area_type' : 'str'
}
}
3 Write output to shapefile
with fiona.open(
'new_stat_areas.shp',
'w',
driver = output_driver,
crs = stat_area_crs,
schema = stat_area_schema) as c:
for feature in shapely_geometries:
c.write({
'geometry': mapping(feature['geometry']['coordinates']),
'properties': {feature['properties']}
})
4 Error message
4a - OrderedDict:
Traceback (most recent call last):
File "dataIntersections.py", line 191, in <module>
'properties': {feature['properties']}
TypeError: unhashable type: 'collections.OrderedDict
4b - Dict:
Traceback (most recent call last):
File "dataIntersections.py", line 190, in <module>
'properties': {feature['properties']}
TypeError: unhashable type: 'dict'
5 Extra info
When replacing the c.write
part for this print
statement:
pprint.pprint({'geometry': mapping(feature['geometry']['coordinates']),
'properties': feature['properties']})
The script returns this (cropped) information:
5a - OrderedDict
{'geometry': {'coordinates': (((144200.28399658203, 460830.77520751953),
# (...)
(150070.26199916803, 454926.5999999999),
(150039.87305716722, 454945.22763747524))),
'type': 'Polygon'},
'properties': OrderedDict([('area_code', 'GM0355'),
('area_name', 'Zeist'),
('area_type', 'Rural')])}
5b - Dict
{'geometry': {'coordinates': (((153474.06604003906, 469029.67779541016),
# (...)
(153473.87103271484, 469029.7180175781),
(153474.06604003906, 469029.67779541016)),),
'type': 'Polygon'},
'properties': {'area_code': 'GM0307',
'area_name': 'Amersfoort',
'area_type': 'Rural'}}
I would like to know what goes wrong and how to solve it!