From 148530e96e5b4c10466e94816e001cd43b887751 Mon Sep 17 00:00:00 2001 From: saheelsapovadia Date: Tue, 9 Sep 2025 16:43:29 +0000 Subject: [PATCH 1/4] example added for on_each_feature use --- docs/user_guide/geojson.rst | 1 + .../geojson_advanced_on_each_feature.md | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 docs/user_guide/geojson/geojson_advanced_on_each_feature.md diff --git a/docs/user_guide/geojson.rst b/docs/user_guide/geojson.rst index 704dc2e4b1..44dfe284bd 100644 --- a/docs/user_guide/geojson.rst +++ b/docs/user_guide/geojson.rst @@ -8,5 +8,6 @@ GeoJSON and choropleth geojson/choropleth geojson/geojson_marker geojson/geojson_popup_and_tooltip + geojson/geojson_advanced_on_each_feature geojson/geopandas_and_geo_interface geojson/smoothing diff --git a/docs/user_guide/geojson/geojson_advanced_on_each_feature.md b/docs/user_guide/geojson/geojson_advanced_on_each_feature.md new file mode 100644 index 0000000000..4854715883 --- /dev/null +++ b/docs/user_guide/geojson/geojson_advanced_on_each_feature.md @@ -0,0 +1,77 @@ +```{code-cell} ipython3 +--- +nbsphinx: hidden +--- +import folium +``` + +# Advanced GeoJSON Customization with on_each_feature + +The `on_each_feature` parameter in `folium.GeoJson` provides powerful customization capabilities by allowing you to execute JavaScript code for each feature in your GeoJSON data. This is particularly useful for: + +- Custom tooltip and popup handling for complex geometries like MultiPoint +- Adding custom event listeners +- Implementing advanced styling logic +- Working with geometry types that need special handling + +## Understanding on_each_feature + +The `on_each_feature` parameter accepts a `folium.utilities.JsCode` object containing JavaScript code that will be executed for each feature. The JavaScript function receives two parameters: +- `feature`: The GeoJSON feature object +- `layer`: The Leaflet layer object representing the feature + +## Basic Example + +```{code-cell} ipython3 +import folium +from folium.utilities import JsCode +import json + +# Simple point data +geojson_data = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {"name": "Location 1", "value": 100}, + "geometry": {"type": "Point", "coordinates": [77.5946, 12.9716]} # Central Bangalore + }, + { + "type": "Feature", + "properties": {"name": "Location 2", "value": 200}, + "geometry": {"type": "Point", "coordinates": [77.6200, 12.9800]} # Northeast Bangalore + } + ] +} + +# Custom JavaScript to add popups +on_each_feature = JsCode(""" +function(feature, layer) { + layer.bindTooltip( + '' + feature.properties.name + '
' + + 'Value: ' + feature.properties.value + ); + + layer.bindPopup( + '' + ' This is popup ' + '
' + + '' + feature.properties.name + '
' + + 'Value: ' + feature.properties.value + ); +} +""") + +m = folium.Map(location=[12.9716, 77.5946], zoom_start=10) + +folium.GeoJson( + geojson_data, + on_each_feature=on_each_feature +).add_to(m) + +m +``` + +The `on_each_feature` parameter provides the flexibility needed to handle complex GeoJSON scenarios that the standard tooltip and popup classes cannot address, particularly for MultiPoint geometries and advanced interactive features. + +## References + +- [Leaflet GeoJSON Tutorial](https://leafletjs.com/examples/geojson/) - Comprehensive guide to using GeoJSON with Leaflet, including the `onEachFeature` option that inspired folium's `on_each_feature` parameter. From 9de3279f882503e6fff10e87e50dcd1eba16d08f Mon Sep 17 00:00:00 2001 From: saheelsapovadia Date: Wed, 10 Sep 2025 14:05:47 +0000 Subject: [PATCH 2/4] doc link added to pop and tooltip page --- docs/user_guide/geojson/geojson_popup_and_tooltip.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/user_guide/geojson/geojson_popup_and_tooltip.md b/docs/user_guide/geojson/geojson_popup_and_tooltip.md index 5a2154c3c3..dcb494ad03 100644 --- a/docs/user_guide/geojson/geojson_popup_and_tooltip.md +++ b/docs/user_guide/geojson/geojson_popup_and_tooltip.md @@ -7,6 +7,9 @@ import folium # GeoJSON popup and tooltip +The standard `GeoJsonPopup` and `GeoJsonTooltip` classes work well with most geometry types, but have limitations with MultiPoint and other complex multigeometries. For these cases, consider using the `on_each_feature` parameter for custom handling. See the [doc](https://python-visualization.github.io/folium/latest/user_guide/geojson/geojson_advanced_on_each_feature.html) for more information. + + ```{code-cell} ipython3 import pandas as pd From 69cc5ece4b00cfff89270e71cb82d1d027580bc5 Mon Sep 17 00:00:00 2001 From: saheelsapovadia Date: Wed, 10 Sep 2025 14:09:33 +0000 Subject: [PATCH 3/4] changed the placement --- docs/user_guide/geojson/geojson_popup_and_tooltip.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/user_guide/geojson/geojson_popup_and_tooltip.md b/docs/user_guide/geojson/geojson_popup_and_tooltip.md index dcb494ad03..dd14d84143 100644 --- a/docs/user_guide/geojson/geojson_popup_and_tooltip.md +++ b/docs/user_guide/geojson/geojson_popup_and_tooltip.md @@ -7,9 +7,6 @@ import folium # GeoJSON popup and tooltip -The standard `GeoJsonPopup` and `GeoJsonTooltip` classes work well with most geometry types, but have limitations with MultiPoint and other complex multigeometries. For these cases, consider using the `on_each_feature` parameter for custom handling. See the [doc](https://python-visualization.github.io/folium/latest/user_guide/geojson/geojson_advanced_on_each_feature.html) for more information. - - ```{code-cell} ipython3 import pandas as pd @@ -140,3 +137,6 @@ colormap.add_to(m) m ``` + + +The standard `GeoJsonPopup` and `GeoJsonTooltip` classes work well with most geometry types, but have limitations with MultiPoint and other complex multigeometries. For these cases, consider using the `on_each_feature` parameter for custom handling. See the [doc](https://python-visualization.github.io/folium/latest/user_guide/geojson/geojson_advanced_on_each_feature.html) for more information. From 077244b6d427424fa88c46ef7f2101f2829ad0f9 Mon Sep 17 00:00:00 2001 From: Hans Then Date: Wed, 10 Sep 2025 18:47:30 +0200 Subject: [PATCH 4/4] Update geojson_popup_and_tooltip.md Make link relative --- docs/user_guide/geojson/geojson_popup_and_tooltip.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user_guide/geojson/geojson_popup_and_tooltip.md b/docs/user_guide/geojson/geojson_popup_and_tooltip.md index dd14d84143..2f67c85e61 100644 --- a/docs/user_guide/geojson/geojson_popup_and_tooltip.md +++ b/docs/user_guide/geojson/geojson_popup_and_tooltip.md @@ -139,4 +139,4 @@ m ``` -The standard `GeoJsonPopup` and `GeoJsonTooltip` classes work well with most geometry types, but have limitations with MultiPoint and other complex multigeometries. For these cases, consider using the `on_each_feature` parameter for custom handling. See the [doc](https://python-visualization.github.io/folium/latest/user_guide/geojson/geojson_advanced_on_each_feature.html) for more information. +The standard `GeoJsonPopup` and `GeoJsonTooltip` classes work well with most geometry types, but have limitations with MultiPoint and other complex multigeometries. For these cases, consider using the `on_each_feature` parameter for custom handling. See the [doc](geojson_advanced_on_each_feature.html) for more information.