From 9e4baad13425f60e3e0aa73faaa0cefdf5773851 Mon Sep 17 00:00:00 2001
From: Florian Knappers <73856313+JJFlorian@users.noreply.github.com>
Date: Wed, 15 Nov 2023 15:37:04 +0100
Subject: [PATCH 1/3] added add_wms_legend method to map class
---
leafmap/foliumap.py | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/leafmap/foliumap.py b/leafmap/foliumap.py
index ad84808252..732f6f3dd3 100644
--- a/leafmap/foliumap.py
+++ b/leafmap/foliumap.py
@@ -416,6 +416,41 @@ def add_wms_layer(
).add_to(self)
except Exception as e:
raise Exception(e)
+
+ def add_wms_legend(
+ self,
+ url,
+ ):
+ """Add a WMS legend based on an image URL
+
+ Args:
+ url (str): URL of the WMS legend image.
+ """
+ from branca.element import Figure, MacroElement, Element
+
+ # Check if the map is a Folium Map instance
+ if not isinstance(self, Map):
+ raise ValueError("The self argument must be an instance of folium.Map.")
+
+ # HTML template for the legend
+ legend_html = f"""
+ {{% macro html(this, kwargs) %}}
+
+
+

+
+ {{% endmacro %}}
+ """
+
+ # Create an Element with the HTML and add it to the map
+ macro = MacroElement()
+ macro._template = Template(legend_html)
+
+ self.get_root().add_child(macro)
def add_tile_layer(
self,
From d5e085ce4d65312c9defeb5e8cb90546f3ab5020 Mon Sep 17 00:00:00 2001
From: Florian Knappers <73856313+JJFlorian@users.noreply.github.com>
Date: Wed, 15 Nov 2023 15:41:47 +0100
Subject: [PATCH 2/3] added test_add_wms_legend unit test
---
tests/test_foliumap.py | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/tests/test_foliumap.py b/tests/test_foliumap.py
index 6d6647628d..20b4663c76 100644
--- a/tests/test_foliumap.py
+++ b/tests/test_foliumap.py
@@ -153,6 +153,16 @@ def test_add_legend(self):
out_str = m.to_html()
assert "NLCD" in out_str
+ def test_add_wms_legend(self):
+ """Check add of wms legend based on url"""
+ m = leafmap.Map()
+ url="https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?REQUEST=GetLegendGraphic&FORMAT=image/png&LAYER=NLCD_2016_Land_Cover_L48"
+ m.add_wms_legend(url=url)
+ out_str = m.to_html()
+
+ assert "GetLegendGraphic" in out_str
+
+
# def test_add_marker_cluster(self):
# """Check marker cluster"""
# with self.assertRaises(NotImplementedError):
From 81fe1bf8aba3431ba5630fc8fbb0f5d4d075e200 Mon Sep 17 00:00:00 2001
From: Florian Knappers <73856313+JJFlorian@users.noreply.github.com>
Date: Wed, 15 Nov 2023 15:47:09 +0100
Subject: [PATCH 3/3] updated stringdoc with url format if using wms legend
---
leafmap/foliumap.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/leafmap/foliumap.py b/leafmap/foliumap.py
index 732f6f3dd3..b7fa5f4700 100644
--- a/leafmap/foliumap.py
+++ b/leafmap/foliumap.py
@@ -424,7 +424,7 @@ def add_wms_legend(
"""Add a WMS legend based on an image URL
Args:
- url (str): URL of the WMS legend image.
+ url (str): URL of the WMS legend image. Should have this format if using wms legend: {geoserver}/wms?REQUEST=GetLegendGraphic&FORMAT=image/png&LAYER={layer}
"""
from branca.element import Figure, MacroElement, Element