Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve folium add marker cluster method #2098

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion geemap/basemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
# Custom WMS tile services.
WMS_TILES = {
"FWS NWI Wetlands": {
"url": "https://www.fws.gov/wetlands/arcgis/services/Wetlands/MapServer/WMSServer?",
"url": "https://fwspublicservices.wim.usgs.gov/wetlandsmapservice/services/Wetlands/MapServer/WMSServer?",
"layers": "1",
"name": "FWS NWI Wetlands",
"attribution": "FWS",
Expand Down
50 changes: 29 additions & 21 deletions geemap/foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,20 +1501,21 @@ def add_osm_from_bbox(

def add_points_from_xy(
self,
data,
x="longitude",
y="latitude",
popup=None,
min_width=100,
max_width=200,
layer_name="Marker Cluster",
color_column=None,
marker_colors=None,
icon_colors=["white"],
icon_names=["info"],
angle=0,
prefix="fa",
add_legend=True,
data: Union[str, pd.DataFrame],
x: Optional[str] = "longitude",
y: Optional[str] = "latitude",
popup: Optional[List] = None,
min_width: Optional[int] = 100,
max_width: Optional[int] = 200,
layer_name: Optional[str] = "Marker Cluster",
color_column: Optional[str] = None,
marker_colors: Optional[List] = None,
icon_colors: Optional[List] = ["white"],
icon_names: Optional[List] = ["info"],
angle: Optional[int] = 0,
prefix: Optional[str] = "fa",
add_legend: Optional[bool] = True,
max_cluster_radius: Optional[int] = 80,
**kwargs,
):
"""Adds a marker cluster to the map.
Expand All @@ -1530,13 +1531,20 @@ def add_points_from_xy(
color_column (str, optional): The column name for the color values. Defaults to None.
marker_colors (list, optional): A list of colors to be used for the markers. Defaults to None.
icon_colors (list, optional): A list of colors to be used for the icons. Defaults to ['white'].
icon_names (list, optional): A list of names to be used for the icons. More icons can be found at https://fontawesome.com/v4/icons or https://getbootstrap.com/docs/3.3/components/?utm_source=pocket_mylist. Defaults to ['info'].
icon_names (list, optional): A list of names to be used for the icons. More icons can be found
at https://fontawesome.com/v4/icons or https://getbootstrap.com/docs/3.3/components/?utm_source=pocket_mylist. Defaults to ['info'].
angle (int, optional): The angle of the icon. Defaults to 0.
prefix (str, optional): The prefix states the source of the icon. 'fa' for font-awesome or 'glyphicon' for bootstrap 3. Defaults to 'fa'.
add_legend (bool, optional): If True, a legend will be added to the map. Defaults to True.
max_cluster_radius (int, optional): The maximum radius that a cluster will cover from the central marker (in pixels).
**kwargs: Other keyword arguments to pass to folium.MarkerCluster(). For a list of available options,
see https://github.com/Leaflet/Leaflet.markercluster. For example, to change the cluster radius, use options={"maxClusterRadius": 50}.
"""
import pandas as pd

if "maxClusterRadius" not in kwargs:
kwargs["maxClusterRadius"] = max_cluster_radius

color_options = [
"red",
"blue",
Expand Down Expand Up @@ -1574,7 +1582,7 @@ def add_points_from_xy(
)

if color_column is not None:
items = sorted(list(set(df[color_column])))
items = list(set(df[color_column]))
else:
items = None

Expand Down Expand Up @@ -1615,16 +1623,16 @@ def add_points_from_xy(
if y not in col_names:
raise ValueError(f"y must be one of the following: {', '.join(col_names)}")

marker_cluster = plugins.MarkerCluster(name=layer_name).add_to(self)
marker_cluster = plugins.MarkerCluster(name=layer_name, **kwargs).add_to(self)

for row in df.itertuples():
for idx, row in df.iterrows():
html = ""
for p in popup:
html = html + "<b>" + p + "</b>" + ": " + str(getattr(row, p)) + "<br>"
html = html + "<b>" + p + "</b>" + ": " + str(row[p]) + "<br>"
popup_html = folium.Popup(html, min_width=min_width, max_width=max_width)

if items is not None:
index = items.index(getattr(row, color_column))
index = items.index(row[color_column])
marker_icon = folium.Icon(
color=marker_colors[index],
icon_color=icon_colors[index],
Expand All @@ -1636,7 +1644,7 @@ def add_points_from_xy(
marker_icon = None

folium.Marker(
location=[getattr(row, y), getattr(row, x)],
location=[row[y], row[x]],
popup=popup_html,
icon=marker_icon,
).add_to(marker_cluster)
Expand Down
Loading