Skip to content

Commit

Permalink
Add backward compatibility for map alias
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed May 7, 2024
1 parent 047a0ce commit 9c0397e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
58 changes: 50 additions & 8 deletions geemap/basemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
import folium
import ipyleaflet
import xyzservices
from .common import (
check_package,
planet_tiles,
)
from typing import Optional, Any
from .common import check_package, planet_tiles, google_maps_api_key
from typing import Optional, Any, Dict, Union
from xyzservices import TileProvider


Expand Down Expand Up @@ -410,16 +407,16 @@ def google_map_tiles(
Returns:
dict: A dictionary where the keys are the map types
('roadmap', 'satellite', 'terrain', 'hybrid', 'traffic', 'streetview')
('roadmap', 'satellite', 'terrain', 'hybrid')
and the values are the corresponding GoogleMapsTileProvider objects.
"""
allowed_map_types = [
"roadmap",
"satellite",
"terrain",
"hybrid",
"traffic",
"streetview",
# "traffic",
# "streetview",
]

gmap_providers = {}
Expand All @@ -432,6 +429,51 @@ def google_map_tiles(
return gmap_providers


def check_basemap_alias(
basemap: str,
) -> str:
"""
Checks if the provided basemap is an alias and returns the corresponding basemap name.
Args:
basemap (str): The basemap to check.
Returns:
str: The corresponding basemap name if the input is an alias, otherwise the input itself.
"""

allowed_aliases = {
"DEFAULT": {
"Google": "Google.Roadmap",
"Esri": "OpenStreetMap.Mapnik",
},
"ROADMAP": {
"Google": "Google.Roadmap",
"Esri": "Esri.WorldStreetMap",
},
"SATELLITE": {
"Google": "Google.Satellite",
"Esri": "Esri.WorldImagery",
},
"TERRAIN": {
"Google": "Google.Terrain",
"Esri": "Esri.WorldTopoMap",
},
"HYBRID": {
"Google": "Google.Hybrid",
"Esri": "Esri.WorldImagery",
},
}

if isinstance(basemap, str) and basemap.upper() in allowed_aliases:
if google_maps_api_key() is not None:
return allowed_aliases[basemap.upper()]["Google"]
else:
return allowed_aliases[basemap.upper()]["Esri"]
else:
return basemap


def get_xyz_dict(free_only=True, france=False):
"""Returns a dictionary of xyz services.
Expand Down
12 changes: 6 additions & 6 deletions geemap/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,12 @@ def __init__(self, **kwargs):

if "basemap" not in kwargs and "Google.Roadmap" in self._available_basemaps:
kwargs["basemap"] = self._available_basemaps["Google.Roadmap"]
elif (
"basemap" in kwargs
and isinstance(kwargs["basemap"], str)
and kwargs["basemap"] in self._available_basemaps
):
kwargs["basemap"] = self._available_basemaps.get(kwargs["basemap"])
elif "basemap" in kwargs and isinstance(kwargs["basemap"], str):
kwargs["basemap"] = basemaps.check_basemap_alias(kwargs["basemap"])
if kwargs["basemap"] in self._available_basemaps:
kwargs["basemap"] = self._available_basemaps.get(kwargs["basemap"])
else:
raise ValueError(f"Basemap {kwargs['basemap']} not found.")

if "width" in kwargs:
self.width: str = kwargs.pop("width", "100%")
Expand Down

0 comments on commit 9c0397e

Please sign in to comment.