From 05c17f2cbd2f7eff18e02593d38b47df5e39f6b8 Mon Sep 17 00:00:00 2001 From: Filipe Fernandes Date: Fri, 19 Apr 2024 19:20:34 +0200 Subject: [PATCH 1/3] fix __geo_interface__ styling --- folium/features.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/folium/features.py b/folium/features.py index 875ab319a..bca5c6d70 100644 --- a/folium/features.py +++ b/folium/features.py @@ -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 From 085d893ead1f89ff4361891ca8bd61f90eee3651 Mon Sep 17 00:00:00 2001 From: Filipe Fernandes Date: Fri, 19 Apr 2024 21:04:37 +0200 Subject: [PATCH 2/3] note on late binding closures --- docs/user_guide/geojson/geojson.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/user_guide/geojson/geojson.md b/docs/user_guide/geojson/geojson.md index f8da655c6..7a8c0d78c 100644 --- a/docs/user_guide/geojson/geojson.md +++ b/docs/user_guide/geojson/geojson.md @@ -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 From b81eadab7c16b9458bde088ca90d25ec90afe4a6 Mon Sep 17 00:00:00 2001 From: Filipe Fernandes Date: Fri, 19 Apr 2024 21:04:47 +0200 Subject: [PATCH 3/3] update pre-commits --- .pre-commit-config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6203f41c3..4ae265e9e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 @@ -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