There seems to be a bug in the _validate_function in folium\features.py
When I add an attribute called 'color' to my json data for using it afterwards in the style function the _validate_function doesn't pass:
area_json = json.load( open(area_file) )
area_json['color'] = my_color_function(area)
gj = folium.GeoJson(area_json, name = area,
style_function = lambda feature: {
'fillColor': feature['geometry']['color']
}
)
The loaded JSON file contains just the geometry: {"type": "Polygon", "coordinates": [ ... ]}
If I try to access directly to color in the style function ( pythonstyle_function = lambda feature: { 'fillColor': feature['color'] } ) the _validate_function test passes, but it will cause an error when saving the map, because folium tries to read from feature['color'] while it's in feature['geometry']['color'] after creating the folium GeoJson object.
If I just change the _validate_function for accepting both ways, e.g. like this (in a dirty way):
try:
if isinstance(func(test_feature), dict):
return
except:
pass
try:
if isinstance(func({'geometry': test_feature}), dict):
return
except:
pass
Everything works as expected.
So is there something to fix in this function or is folium just not designed for handling geojson data in the format of {"type": "Polygon", "coordinates": [ ... ]} without the brackets for feature etc around it?
folium.__version__0.8.3
Hi @PhilHam, thanks for your report. It's sort of that last thing you said. We do support loose geometries, but not geometries with custom things added to them. That's why I added the 'wont fix' label.
According to the geojson specification it's not allowed to have anything other than the geometry in the 'geometry' field. You can see that part of the spec here:
https://tools.ietf.org/html/rfc7946#section-3.2
Please change your data to follow the spec and see if that solves your issue. You can turn it into a feature with properties:
{
"type": "Feature",
"geometry": {"type": "Polygon", "coordinates": []},
"properties": {"color": "red"},
}
@PhilHamm that is pretty much what @Conengmo mentioned above. Sorry but we cannot act on non-compliant data or we would go crazy trying to cover all the possibilities out there.
Thanks for your answers!
Of course I'm not asking you for supporting non-compliant data. But as my code & data worked with a previous folium version, I wasn't sure what's supposed to be supported by folium. Changing it to the format mentioned by @Conengmo is totally fine.
Most helpful comment
Hi @PhilHam, thanks for your report. It's sort of that last thing you said. We do support loose geometries, but not geometries with custom things added to them. That's why I added the 'wont fix' label.
According to the geojson specification it's not allowed to have anything other than the geometry in the 'geometry' field. You can see that part of the spec here:
https://tools.ietf.org/html/rfc7946#section-3.2
Please change your data to follow the spec and see if that solves your issue. You can turn it into a feature with properties: