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.

[BUG] folium.GeoJson() with style_function changes input data

See original GitHub issue

Please add a code sample or a nbviewer link, copy-pastable if possible

Example data:


pistes = {"features":[{'geometry': {'coordinates': [[13.7750875, 47.3894653],
     [13.7745458, 47.3897487],
     [13.7744245, 47.3899024],
     [13.7743022, 47.3901266],
     [13.7742754, 47.3904393],
     [13.7745857, 47.3913728],
     [13.7747696, 47.3922164]],
    'type': 'LineString'},
   'properties': {'difficulty': 'easy'},
   'type': 'Feature'},
  {'geometry': {'coordinates': [[13.7748277, 47.3951473],
     [13.7745382, 47.3955505],
     [13.7740868, 47.3961718],
     [13.7738771, 47.3965048],
     [13.7739069, 47.3967489],
     [13.77409, 47.3970407],
     [13.7744923, 47.3973698],
     [13.7748859, 47.3977331],
     [13.7753385, 47.3980947],
     [13.7756779, 47.3983777],
     [13.7758294, 47.3985963],
     [13.7758483, 47.3987251],
     [13.7758303, 47.3988537]],
    'type': 'LineString'},
   'properties': {'difficulty': 'advanced'},
   'type': 'Feature'},
  {'geometry': {'coordinates': [[13.7750875, 47.3894653],
     [13.7752798, 47.38999],
     [13.7753808, 47.3905846],
     [13.7754171, 47.391452],
     [13.7754382, 47.3919579],
     [13.7751964, 47.3927863],
     [13.7759708, 47.3937253],
     [13.7764045, 47.3944936],
     [13.7766345, 47.3947044],
     [13.7774691, 47.3955802],
     [13.7775424, 47.3958695],
     [13.7773201, 47.3963544],
     [13.7770295, 47.3965058],
     [13.777536, 47.3973757]],
    'type': 'LineString'},
   'properties': {'difficulty': 'intermediate'},
   'type': 'Feature'}]}

Example:


m = folium.Map(location=[47.4, 13.78],tiles='OpenStreetMap',zoom_start=14)

diff_to_colour = {'easy':'blue', 'intermediate':'red','advanced':'black'}
def piste_style_function(feature):
    return {'opacity': 1, 
            'weight': 1, 
            'color': diff_to_colour[feature['properties']['difficulty']]}

folium.GeoJson(pistes, style_function=piste_style_function).add_to(m)
m

Then, in a separate notebook cell (after map rendered):

pistes

{'features': [{'geometry': {'coordinates': [[13.7750875, 47.3894653],
     [13.7745458, 47.3897487],
     [13.7744245, 47.3899024],
     [13.7743022, 47.3901266],
     [13.7742754, 47.3904393],
     [13.7745857, 47.3913728],
     [13.7747696, 47.3922164]],
    'type': 'LineString'},
   'properties': {'difficulty': 'easy',
    'highlight': {},
    'style': {'color': 'blue', 'opacity': 1, 'weight': 1}},
   'type': 'Feature'},
  {'geometry': {'coordinat 
...

Notice the additional style property.

Problem description

The style function should NOT edit the data in place. This become problematic in the following cases:

  • You want to re-use a pre-filtered collection of features
  • You want to use several layers (where you tweak styles)

Expected Output

Unchanged pistes

Output of folium.__version__

‘0.5.0’

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
arkanoid87commented, Apr 8, 2020

been wasting hours on this and still I’ve not solved the problem

I’m adding more layers using a loop function over the target_col variable

            style_function=lambda feature: {
                'fillOpacity': 0.5,
                'weight': 0.5,
                'color': "#8b0000" if feature['properties'][target_col] > -1 else "#808080",
                'fillColor': colorscale(feature['properties'][target_col]) if feature['properties'][target_col] > -1 else "#d4d4d4"
            },

I always get TypeError: '>' not supported between instances of 'NoneType' and 'int' on m.save(…) on the second feature of the second layer added.

Everything works if I loop over the target_col only once, it always returns error on second layer even if I set global constant geodataframe in input, even when passing a fresh copied database in input.

Very frustrating

0reactions
Conengmocommented, Apr 8, 2020

Hi @arkanoid87 , this issue you are replying to is no longer relevant, we no longer change the input data in recent folium versions. I suggest you double check you input data. If you really think the issue lies with folium, please open a new ticket and provide a minimal, self-contained example that will help us debug the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python folium parsing error oppening json - Stack Overflow
Try using with following change: fgp.add_child(folium.GeoJson(data=open('World_Population.json').read(), style_function = lambda x: ...
Read more >
Folium draw and highlight polylines with distinct colours from ...
1. Changing the values of the parameters withing both style_function and highlight_function (optional if highlight needed in folium.features.
Read more >
Folium 0.12.1 documentation
You can pass a custom tileset to Folium by passing a Leaflet-style URL to the tiles parameter: ... Convert an unknown data input...
Read more >
Shallow Thoughts : : mapping
Tags: mapping, GIS, fire, data, open data [ 10:46 May 13, 2022 More mapping ... folium.GeoJson(jsonfile, style_function=style_fcn).add_to(m).
Read more >
foliumap module - leafmap
DataFrame): File path or HTTP URL to the input file or a list of data points in ... ipyleaflet and folium. if "style_function"...
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