Skip to content

Commit f89699a

Browse files
authored
Fix lonboard zoom to layer bug (#700)
1 parent c3126e4 commit f89699a

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

leafmap/common.py

+25
Original file line numberDiff line numberDiff line change
@@ -13226,3 +13226,28 @@ def nasa_datasets(keyword=None, df=None, return_short_name=False):
1322613226
return df["ShortName"].tolist()
1322713227
else:
1322813228
return df
13229+
13230+
13231+
def convert_coordinates(x, y, source_crs, target_crs="epsg:4326"):
13232+
"""Convert coordinates from the source EPSG code to the target EPSG code.
13233+
13234+
Args:
13235+
x (float): The x-coordinate of the point.
13236+
y (float): The y-coordinate of the point.
13237+
source_crs (str): The EPSG code of the source coordinate system.
13238+
target_crs (str, optional): The EPSG code of the target coordinate system.
13239+
Defaults to '4326' (EPSG code for WGS84).
13240+
13241+
Returns:
13242+
tuple: A tuple containing the converted longitude and latitude.
13243+
"""
13244+
import pyproj
13245+
13246+
# Create the transformer
13247+
transformer = pyproj.Transformer.from_crs(source_crs, target_crs, always_xy=True)
13248+
13249+
# Perform the transformation
13250+
lon, lat = transformer.transform(x, y)
13251+
13252+
# Return the converted coordinates
13253+
return lon, lat

leafmap/deckgl.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def add_gdf(
6464
color_map: Optional[Union[str, Dict]] = None,
6565
color_k: Optional[int] = 5,
6666
color_args: dict = {},
67+
zoom: Optional[float] = 10.0,
6768
**kwargs: Any,
6869
) -> None:
6970
"""Adds a GeoPandas GeoDataFrame to the map.
@@ -85,6 +86,7 @@ def add_gdf(
8586
'UserDefined'). Arguments can be passed in classification_kwds.
8687
color_k (Optional[int], optional): The number of classes to use for color encoding. Defaults to 5.
8788
color_args (dict, optional): Additional keyword arguments that will be passed to assign_continuous_colors(). Defaults to {}.
89+
zoom (Optional[float], optional): The zoom level to zoom to. Defaults to 10.0.
8890
**kwargs: Additional keyword arguments that will be passed to lonboard.Layer.from_geopandas()
8991
9092
Returns:
@@ -158,10 +160,19 @@ def add_gdf(
158160
if zoom_to_layer:
159161
try:
160162
bounds = gdf.total_bounds.tolist()
163+
x = (bounds[0] + bounds[2]) / 2
164+
y = (bounds[1] + bounds[3]) / 2
165+
166+
src_crs = gdf.crs
167+
if src_crs is None:
168+
src_crs = "EPSG:4326"
169+
170+
lon, lat = convert_coordinates(x, y, src_crs, "EPSG:4326")
171+
161172
self._initial_view_state = {
162-
"latitude": (bounds[1] + bounds[3]) / 2,
163-
"longitude": (bounds[0] + bounds[2]) / 2,
164-
"zoom": 10,
173+
"latitude": lat,
174+
"longitude": lon,
175+
"zoom": zoom,
165176
}
166177
except Exception as e:
167178
print(e)

0 commit comments

Comments
 (0)