Skip to content

Commit 6c0c20e

Browse files
authored
Merge pull request #1935 from python-visualization/fix_geo_interface_styling
fix __geo_interface__ styling
2 parents 4b860c0 + b81eada commit 6c0c20e

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ exclude: examples/data/|.*\.css|.*\.json|.*\.geojson|.*\.html
22

33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: v4.5.0
5+
rev: v4.6.0
66
hooks:
77
- id: trailing-whitespace
88
- id: check-ast
@@ -15,12 +15,12 @@ repos:
1515
files: requirements-dev.txt
1616

1717
- repo: https://github.com/astral-sh/ruff-pre-commit
18-
rev: v0.3.4
18+
rev: v0.4.1
1919
hooks:
2020
- id: ruff
2121

2222
- repo: https://github.com/psf/black
23-
rev: 24.3.0
23+
rev: 24.4.0
2424
hooks:
2525
- id: black
2626
language_version: python3

docs/user_guide/geojson/geojson.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,23 @@ colormap.add_to(m)
237237
m
238238
```
239239

240+
>
241+
> **Caveat**
242+
>
243+
> When using `style_function` in a loop you may encounter Python's 'Late Binding Closure' gotcha!
244+
> See https://docs.python-guide.org/writing/gotchas/#late-binding-closures for more info.
245+
> There are a few ways around it from using a GeoPandas object instead,
246+
> to "hacking" your `style_function` to force early closure, like:
247+
> ```python
248+
> for geom, my_style in zip(geoms, my_styles):
249+
> style = my_style
250+
> style_function = lambda x, style=style: style
251+
> folium.GeoJson(
252+
> data=geom,
253+
> style_function=style_function,
254+
> ).add_to(m)
255+
> ```
256+
240257
### Highlight function
241258
242259
The `GeoJson` class provides a `highlight_function` argument, which works similarly

folium/features.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,10 @@ class GeoJson(Layer):
643643
.done({{ this.get_name() }}_add);
644644
{%- endif %}
645645
646+
{%- if not this.style %}
647+
{{this.get_name()}}.setStyle(function(feature) {return feature.properties.style;});
648+
{%- endif %}
649+
646650
{% endmacro %}
647651
"""
648652
) # noqa

0 commit comments

Comments
 (0)