GeoJSON feature ID creator fails when properties contain list/dict
See original GitHub issueThis 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 dict
s or list
s.
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:
- Created 4 years ago
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
True! Sorry, I pasted a small part of a full routing request and must’ve forgotten to paste the
type
, I edited the issue.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.