Skip to content

Commit

Permalink
Improve the maplibre module
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Nov 27, 2024
1 parent fc1085a commit 8224a53
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions leafmap/maplibregl.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@ def set_zoom(self, zoom: Optional[int] = None) -> None:
"""
self.add_call("setZoom", zoom)

def fit_bounds(self, bounds: List[Tuple[float, float]]) -> None:
def fit_bounds(
self, bounds: List[Tuple[float, float]], options: Dict = None
) -> None:
"""
Adjusts the viewport of the map to fit the specified geographical bounds
in the format of [[lon_min, lat_min], [lon_max, lat_max]] or
Expand All @@ -562,16 +564,22 @@ def fit_bounds(self, bounds: List[Tuple[float, float]]) -> None:
should be visible in the viewport. Each point is a list of two
numbers representing the longitude and latitude. For example,
[[32.958984, -5.353521],[43.50585, 5.615985]]
options (dict, optional): Additional options for fitting the bounds.
See https://maplibre.org/maplibre-gl-js/docs/API/type-aliases/FitBoundsOptions/.
Returns:
None
"""

if options is None:
options = {}

if isinstance(bounds, list):
if len(bounds) == 4 and all(isinstance(i, (int, float)) for i in bounds):
bounds = [[bounds[0], bounds[1]], [bounds[2], bounds[3]]]

self.add_call("fitBounds", bounds)
options["animate"] = options.get("animate", True)
self.add_call("fitBounds", bounds, options)

def add_basemap(
self,
Expand Down Expand Up @@ -681,6 +689,7 @@ def add_geojson(
visible: bool = True,
before_id: Optional[str] = None,
source_args: Dict = {},
fit_bounds_options: Dict = None,
**kwargs: Any,
) -> None:
"""
Expand Down Expand Up @@ -712,6 +721,9 @@ def add_geojson(
the new layer should be inserted.
source_args (dict, optional): Additional keyword arguments that are
passed to the GeoJSONSource class.
fit_bounds_options (dict, optional): Additional options for fitting the bounds.
See https://maplibre.org/maplibre-gl-js/docs/API/type-aliases/FitBoundsOptions
for more information.
**kwargs: Additional keyword arguments that are passed to the Layer class.
See https://maplibre.org/maplibre-style-spec/layers/ for more info.
Expand Down Expand Up @@ -787,7 +799,7 @@ def add_geojson(
self.add_layer(layer, before_id=before_id, name=name, visible=visible)
self.add_popup(name)
if fit_bounds and bounds is not None:
self.fit_bounds(bounds)
self.fit_bounds(bounds, fit_bounds_options)

if isinstance(paint, dict) and f"{layer_type}-opacity" in paint:
self.set_opacity(name, paint[f"{layer_type}-opacity"])
Expand Down Expand Up @@ -3699,6 +3711,9 @@ def add_gps_trace(
if line_args is None:
line_args = {}
self.add_gdf(line_gdf, name=f"{name} Line", **line_args)

if "fit_bounds_options" not in kwargs:
kwargs["fit_bounds_options"] = {"animate": False}
self.add_geojson(geojson, layer_type="circle", paint=paint, name=name, **kwargs)


Expand Down Expand Up @@ -3949,7 +3964,7 @@ def edit_gps_trace(
rows: int = 11,
fig_width: str = "1550px",
fig_height: str = "300px",
time_format: str = "%Y-%m-%d %H:%M:%S",
time_format: str = "%Y-%m-%dT%H:%M:%S",
stroke_color: str = "lightgray",
circle_size: int = 48,
webGL: bool = False,
Expand Down

0 comments on commit 8224a53

Please sign in to comment.