Skip to content

Commit

Permalink
Add support for line and circle legend items
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Nov 9, 2024
1 parent e7ab4be commit 9e36d2a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions leafmap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6789,6 +6789,7 @@ def create_legend(
draggable=True,
output=None,
style={},
shape_type="rectangle",
):
"""Create a legend in HTML format. Reference: https://bit.ly/3oV6vnH
Expand Down Expand Up @@ -7014,6 +7015,14 @@ def create_legend(
content.append(line)

legend_text = "".join(content)
if shape_type == "circle":
legend_text = legend_text.replace("width: 30px", "width: 16px")
legend_text = legend_text.replace(
"border: 1px solid #999;",
"border-radius: 50%;\n border: 1px solid #999;",
)
elif shape_type == "line":
legend_text = legend_text.replace("height: 16px", "height: 3px")

if output is not None:
with open(output, "w") as f:
Expand Down
4 changes: 4 additions & 0 deletions leafmap/foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ def add_legend(
position: Optional[str] = "bottomright",
draggable: Optional[bool] = True,
style: Optional[Dict] = {},
shape_type: Optional[str] = "rectangle",
):
"""Adds a customized legend to the map. Reference: https://bit.ly/3oV6vnH.
If you want to add multiple legends to the map, you need to set the `draggable` argument to False.
Expand Down Expand Up @@ -1260,6 +1261,8 @@ def add_legend(
'bottom': '20px',
'right': '5px'
}
shape_type (str, optional): The shape type of the legend item. It can be
either "rectangle", "line" or "circle". Defaults to "rectangle".
"""
content = create_legend(
Expand All @@ -1272,6 +1275,7 @@ def add_legend(
position,
draggable,
style=style,
shape_type=shape_type,
)
if draggable:
from branca.element import Template, MacroElement
Expand Down
10 changes: 10 additions & 0 deletions leafmap/leafmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,7 @@ def add_legend(
position: Optional[str] = "bottomright",
builtin_legend: Optional[str] = None,
layer_name: Optional[str] = None,
shape_type: Optional[str] = "rectangle",
**kwargs,
) -> None:
"""Adds a customized basemap to the map.
Expand Down Expand Up @@ -1935,6 +1936,15 @@ def add_legend(
legend_html = header + content + footer
legend_text = "".join(legend_html)

if shape_type == "circle":
legend_text = legend_text.replace("width: 30px", "width: 16px")
legend_text = legend_text.replace(
"border: 1px solid #999;",
"border-radius: 50%;\n border: 1px solid #999;",
)
elif shape_type == "line":
legend_text = legend_text.replace("height: 16px", "height: 3px")

try:
legend_output_widget = widgets.Output(
layout={
Expand Down
11 changes: 11 additions & 0 deletions leafmap/map_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def __init__(
position="bottomright",
builtin_legend=None,
add_header=True,
shape_type="rectangle",
widget_args={},
**kwargs,
):
Expand All @@ -187,6 +188,7 @@ def __init__(
to the map. Defaults to None.
add_header (bool, optional): Whether the legend can be closed or
not. Defaults to True.
shape_type (str, optional): The shape type of the legend item.
widget_args (dict, optional): Additional arguments passed to the
widget_template() function. Defaults to {}.
Expand Down Expand Up @@ -273,6 +275,15 @@ def __init__(

legend_html = header + content + footer
legend_text = "".join(legend_html)

if shape_type == "circle":
legend_text = legend_text.replace("width: 30px", "width: 16px")
legend_text = legend_text.replace(
"border: 1px solid #999;",
"border-radius: 50%;\n border: 1px solid #999;",
)
elif shape_type == "line":
legend_text = legend_text.replace("height: 16px", "height: 3px")
legend_output = ipywidgets.Output(layout=Legend.__create_layout(**kwargs))
legend_widget = ipywidgets.HTML(value=legend_text)

Expand Down
11 changes: 11 additions & 0 deletions leafmap/maplibregl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2513,6 +2513,7 @@ def add_legend(
bg_color: str = "white",
position: str = "bottom-right",
builtin_legend: Optional[str] = None,
shape_type: str = "rectangle",
**kwargs: Union[str, int, float],
) -> None:
"""
Expand All @@ -2534,6 +2535,7 @@ def add_legend(
position (str, optional): The position of the legend on the map. Can be one of "top-left",
"top-right", "bottom-left", "bottom-right". Defaults to "bottom-right".
builtin_legend (Optional[str], optional): The name of a built-in legend to use. Defaults to None.
shape_type (str, optional): The shape type of the legend items. Can be one of "rectangle", "circle", or "line".
**kwargs: Additional keyword arguments for future use.
Returns:
Expand Down Expand Up @@ -2648,6 +2650,15 @@ def add_legend(
legend_html = header + content + footer
legend_text = "".join(legend_html)

if shape_type == "circle":
legend_text = legend_text.replace("width: 30px", "width: 16px")
legend_text = legend_text.replace(
"border: 1px solid #999;",
"border-radius: 50%;\n border: 1px solid #999;",
)
elif shape_type == "line":
legend_text = legend_text.replace("height: 16px", "height: 3px")

self.add_html(
legend_text,
fontsize=fontsize,
Expand Down

0 comments on commit 9e36d2a

Please sign in to comment.