question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GeoJSON feature ID creator fails when properties contain list/dict

See original GitHub issue

This code throws a TypeError: (edit: add geometry type to feature)

import folium
json = {
    "type": "FeatureCollection",
    "features": [
        {
            "bbox": [
                8.546326,
                47.417879,
                8.548813,
                47.420362
            ],
            "type": "Feature",
            "properties": {
                "summary": {
                    "distance": 343.2,
                    "duration": 50.5
                },
                "way_points": [
                    0,
                    15
                ]
            },
            "geometry": {
                "coordinates": [
                    [
                        8.548813,
                        47.420362
                    ],
                    [
                        8.548795,
                        47.420345
                    ],
                    [
                        8.548621,
                        47.420167
                    ]
                ],
                "type": "LineString"
            }
        }
    ]
}

m = folium.Map(location=[47.417879, 8.546326], zoom_start=13)

folium.features.GeoJson(json, style_function=lambda x: {'opacity': 1.0}).add_to(m)
m

Problem description

If a style_function is passed, folium tries to find an ID for the GeoJSON features. When no id field is present (not required in GeoJSONs AFAIK), it will try to pick a field from properties by checking if the values of the property fields are unique. However, the current implementation with set to check uniqueness fails when the property field values are dicts or lists.

Quick Fix

It works with a try/except around the the appropriate lines:

try:
    if len(set(feat['properties'][key] for feat in features)) == n:
        return 'feature.properties.{}'.format(key)
except TypeError:
    continue

I’d be happy with this if was my library, as far as I can see nothing else could raise a TypeError here. What do you think?

Output of 0.9.0

    530         for key in feature.get('properties', []):
    531             # try:
--> 532             if len(set(feat['properties'][key] for feat in features)) == n:
    533                 return 'feature.properties.{}'.format(key)
    534             # except TypeError:

TypeError: unhashable type: 'dict'

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
nilsnoldecommented, May 25, 2019

True! Sorry, I pasted a small part of a full routing request and must’ve forgotten to paste the type, I edited the issue.

1reaction
Conengmocommented, May 25, 2019

Thanks for your excellent bug report. This is indeed an issue. Your workaround seems fine, but I’ll look into it a bit, see if we can do something a bit more explicit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unsupported GeoJSON type - GIS Stack Exchange
In creating your GeoJSON, you forgot to add type property for your feature: "type" : "Feature" , so it should be:
Read more >
arcgis.gis module | ArcGIS API for Python
This parameter allows the desired item id to be specified during creation which can be useful for cloning and automated content creation scenarios....
Read more >
PDPYRAS: PagerDuty Python REST API Sessions
To use, simply pass in a list of objects or references (dictionaries having a structure according to the API schema reference for that...
Read more >
Labelbox Python API reference — Python SDK reference 3.33 ...
JSON object containing the Labelbox data row ID for the annotation. status. Indicates SUCCESS or FAILURE. errors. An array of error messages included...
Read more >
Adding a source in mapbox throws error "Input data given to ...
map.addSource('some id', { type: 'geojson', data: { "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": {} ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found