Skip to content

Commit

Permalink
Fix add_geojson style callback bug (#898)
Browse files Browse the repository at this point in the history
* Fix add_geojson style callback bug

* Fix folium style callback bug
  • Loading branch information
giswqs authored Sep 22, 2024
1 parent f5cab53 commit 01f9055
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
9 changes: 5 additions & 4 deletions leafmap/foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,14 +1449,18 @@ def add_geojson(
raise Exception(e)

# interchangeable parameters between ipyleaflet and folium.

if "style_callback" in kwargs:
kwargs["style_function"] = kwargs.pop("style_callback")

style_dict = {}
if "style_function" not in kwargs:
if "style" in kwargs:
style_dict = kwargs["style"]
if isinstance(kwargs["style"], dict) and len(kwargs["style"]) > 0:
kwargs["style_function"] = lambda x: style_dict
kwargs.pop("style")
else:
elif "fill_colors" not in kwargs:
style_dict = {
# "stroke": True,
"color": "#3388ff",
Expand All @@ -1470,9 +1474,6 @@ def add_geojson(
}
kwargs["style_function"] = lambda x: style_dict

if "style_callback" in kwargs:
kwargs.pop("style_callback")

if "hover_style" in kwargs:
kwargs.pop("hover_style")

Expand Down
17 changes: 7 additions & 10 deletions leafmap/leafmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2630,10 +2630,10 @@ def add_geojson(
import random
import requests

style_callback_only = False
# style_callback_only = False

if fill_colors is not None:
style_callback_only = True
# if fill_colors is not None:
# style_callback_only = True

try:
if isinstance(in_geojson, str):
Expand Down Expand Up @@ -2670,7 +2670,7 @@ def add_geojson(

geom_type = get_geometry_type(data)

if not style:
if style is None and (style_callback is None):
style = {
# "stroke": True,
"color": "#3388ff",
Expand Down Expand Up @@ -2779,23 +2779,20 @@ def update_html(feature, fields=fields, **kwargs):
if fill_colors is not None:
style_callback = random_color

if style_callback_only:
if style_callback is None:
geojson = ipyleaflet.GeoJSON(
data=data,
style=style,
hover_style=hover_style,
style_callback=style_callback,
name=layer_name,
)
else:
kwargs = {}
if style_callback is not None:
kwargs["style_callback"] = style_callback
geojson = ipyleaflet.GeoJSON(
data=data,
style=style,
hover_style=hover_style,
name=layer_name,
**kwargs,
style_callback=style_callback,
)

if info_mode == "on_hover":
Expand Down

0 comments on commit 01f9055

Please sign in to comment.