Skip to content

Commit

Permalink
Merge pull request #1935 from python-visualization/fix_geo_interface_…
Browse files Browse the repository at this point in the history
…styling

fix __geo_interface__ styling
  • Loading branch information
ocefpaf authored Apr 23, 2024
2 parents 4b860c0 + b81eada commit 6c0c20e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ exclude: examples/data/|.*\.css|.*\.json|.*\.geojson|.*\.html

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: check-ast
Expand All @@ -15,12 +15,12 @@ repos:
files: requirements-dev.txt

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
rev: v0.4.1
hooks:
- id: ruff

- repo: https://github.com/psf/black
rev: 24.3.0
rev: 24.4.0
hooks:
- id: black
language_version: python3
Expand Down
17 changes: 17 additions & 0 deletions docs/user_guide/geojson/geojson.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,23 @@ colormap.add_to(m)
m
```

>
> **Caveat**
>
> When using `style_function` in a loop you may encounter Python's 'Late Binding Closure' gotcha!
> See https://docs.python-guide.org/writing/gotchas/#late-binding-closures for more info.
> There are a few ways around it from using a GeoPandas object instead,
> to "hacking" your `style_function` to force early closure, like:
> ```python
> for geom, my_style in zip(geoms, my_styles):
> style = my_style
> style_function = lambda x, style=style: style
> folium.GeoJson(
> data=geom,
> style_function=style_function,
> ).add_to(m)
> ```
### Highlight function
The `GeoJson` class provides a `highlight_function` argument, which works similarly
Expand Down
4 changes: 4 additions & 0 deletions folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,10 @@ class GeoJson(Layer):
.done({{ this.get_name() }}_add);
{%- endif %}
{%- if not this.style %}
{{this.get_name()}}.setStyle(function(feature) {return feature.properties.style;});
{%- endif %}
{% endmacro %}
"""
) # noqa
Expand Down

0 comments on commit 6c0c20e

Please sign in to comment.