Skip to content

Commit

Permalink
Add add_ee_layer function (opengeos#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Sep 16, 2023
1 parent b657c34 commit 534722d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
45 changes: 45 additions & 0 deletions leafmap/foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,51 @@ def add_layer(self, layer):
"""
layer.add_to(self)

def add_ee_layer(
self,
asset_id: str,
name: str = None,
attribution: str = "Google Earth Engine",
shown: bool = True,
opacity: float = 1.0,
**kwargs,
) -> None:
"""
Adds a Google Earth Engine tile layer to the map based on the tile layer URL from
https://github.com/opengeos/ee-tile-layers/blob/main/datasets.tsv.
Args:
asset_id (str): The ID of the Earth Engine asset.
name (str, optional): The name of the tile layer. If not provided, the asset ID will be used. Default is None.
attribution (str, optional): The attribution text to be displayed. Default is "Google Earth Engine".
shown (bool, optional): Whether the tile layer should be shown on the map. Default is True.
opacity (float, optional): The opacity of the tile layer. Default is 1.0.
**kwargs: Additional keyword arguments to be passed to the underlying `add_tile_layer` method.
Returns:
None
"""
import pandas as pd

df = pd.read_csv("https://ee-tiles.gishub.org/datasets.tsv", sep="\t")

asset_id = asset_id.strip()
if name is None:
name = asset_id

if asset_id in df["id"].values:
url = df.loc[df["id"] == asset_id, "url"].values[0]
self.add_tile_layer(
url,
name,
attribution=attribution,
shown=shown,
opacity=opacity,
**kwargs,
)
else:
print(f"The provided EE tile layer {asset_id} does not exist.")

def add_layer_control(self):
"""Adds layer control to the map."""
layer_ctrl = False
Expand Down
45 changes: 45 additions & 0 deletions leafmap/leafmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,51 @@ def add_layer(self, layer):
self.remove_layer(existing_layer)
super().add(layer)

def add_ee_layer(
self,
asset_id: str,
name: str = None,
attribution: str = "Google Earth Engine",
shown: bool = True,
opacity: float = 1.0,
**kwargs,
) -> None:
"""
Adds a Google Earth Engine tile layer to the map based on the tile layer URL from
https://github.com/opengeos/ee-tile-layers/blob/main/datasets.tsv.
Args:
asset_id (str): The ID of the Earth Engine asset.
name (str, optional): The name of the tile layer. If not provided, the asset ID will be used. Default is None.
attribution (str, optional): The attribution text to be displayed. Default is "Google Earth Engine".
shown (bool, optional): Whether the tile layer should be shown on the map. Default is True.
opacity (float, optional): The opacity of the tile layer. Default is 1.0.
**kwargs: Additional keyword arguments to be passed to the underlying `add_tile_layer` method.
Returns:
None
"""
import pandas as pd

df = pd.read_csv("https://ee-tiles.gishub.org/datasets.tsv", sep="\t")

asset_id = asset_id.strip()
if name is None:
name = asset_id

if asset_id in df["id"].values:
url = df.loc[df["id"] == asset_id, "url"].values[0]
self.add_tile_layer(
url,
name,
attribution=attribution,
shown=shown,
opacity=opacity,
**kwargs,
)
else:
print(f"The provided EE tile layer {asset_id} does not exist.")

def add_layer_control(self, position="topright"):
"""Adds a layer control to the map.
Expand Down

0 comments on commit 534722d

Please sign in to comment.