2

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:

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!

3
  • is there a reason you're using OrderedDict? A normal python dict (like you have Geometry) should be OK. See this answer for a good explanation of the error message
    – Steven Kay
    Jan 26, 2019 at 15:58
  • @StevenKay, I've tried both. But I actually just followed the Fiona manual (see: link above). The example in 1.6.3 "Writing new files from scratch" uses an OrderedDict. I will add the dict 'route' in my question above
    – Graven
    Jan 27, 2019 at 8:41
  • @StevenKay I didn't mention it, but I read that topic: it was the reason why I tried the normal Dict, which should be hashable, however, as I added above, I still receive an error message that tells me that the 'Dict' is unhashable. And that's were I was left in confusion. I guess both 'routes' should be ok?
    – Graven
    Jan 27, 2019 at 9:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.