Skip to content

Bug in _validate_function #1150

Description

@PhilHamm

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?

Output of folium.__version__

0.8.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis problem or request is outside the scope of folium

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions