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

Fix Issue #9371 - Set default map style if not provided #9419

Merged
merged 4 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 13 additions & 4 deletions bindings/pydeck/pydeck/bindings/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from .view import View
from .view_state import ViewState
from .base_map_provider import BaseMapProvider
from .map_styles import DARK, get_from_map_identifier
from .map_styles import DARK, get_from_map_identifier, get_default_map_identifier


# Special default value to for querying the default style for a map provider.
_DEFAULT_MAP_STYLE_SENTINEL = '__MAP_STYLE__'


def has_jupyter_extra():
Expand All @@ -29,7 +33,7 @@ def __init__(
self,
layers=None,
views=[View(type="MapView", controller=True)],
map_style=DARK,
map_style=_DEFAULT_MAP_STYLE_SENTINEL,
api_keys=None,
initial_view_state=ViewState(latitude=0, longitude=0, zoom=1),
width="100%",
Expand Down Expand Up @@ -61,8 +65,11 @@ def __init__(
Values can be ``carto``, ``mapbox`` or ``google_maps``
map_style : str or dict, default 'dark'
One of 'light', 'dark', 'road', 'satellite', 'dark_no_labels', and 'light_no_labels', a URI for a basemap
style, which varies by provider, or a dict that follows the Mapbox style `specification <https://docs.mapbox.com/mapbox-gl-js/style-spec/>`.
The default is Carto's Dark Matter map. For Mapbox examples, see Mapbox's `gallery <https://www.mapbox.com/gallery/>`.
style, which varies by provider, or a dict that follows the Mapbox style `specification <https://docs.mapbox.com/mapbox-gl-js/style-spec/>`_.
If ``map_provider='google_maps'``, the default is the ``roadmap`` map type.
if ``map_provider='mapbox'``, the default is `Mapbox Dark <https://www.mapbox.com/maps/dark>`_ style.
Otherwise, the default is Carto's Dark Matter map.
For Mapbox examples, see Mapbox's `gallery <https://www.mapbox.com/gallery/>`_.
If not using a basemap, set ``map_provider=None``.
initial_view_state : pydeck.ViewState, default ``pydeck.ViewState(latitude=0, longitude=0, zoom=1)``
Initial camera angle relative to the map, defaults to a fully zoomed out 0, 0-centered map
Expand Down Expand Up @@ -119,6 +126,8 @@ def __init__(
assert map_provider == BaseMapProvider.MAPBOX.value, custom_map_style_error
self.map_style = map_style
else:
if map_provider and map_style == _DEFAULT_MAP_STYLE_SENTINEL:
map_style = get_default_map_identifier(map_provider)
self.map_style = get_from_map_identifier(map_style, map_provider)

self.parameters = parameters
Expand Down
17 changes: 16 additions & 1 deletion bindings/pydeck/pydeck/bindings/map_styles.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import warnings
from .base_map_provider import BaseMapProvider


DARK = "dark"
Expand Down Expand Up @@ -31,6 +31,12 @@
SATELLITE: {"mapbox": MAPBOX_SATELLITE, "google_maps": GOOGLE_SATELLITE},
}

_default_map_identifers = {
BaseMapProvider.CARTO: DARK,
BaseMapProvider.MAPBOX: DARK,
BaseMapProvider.GOOGLE_MAPS: GOOGLE_ROAD,
}


def get_from_map_identifier(map_identifier: str, provider: str) -> str:
"""Attempt to get a style URI by map provider, otherwise pass the map identifier
Expand All @@ -57,3 +63,12 @@ def get_from_map_identifier(map_identifier: str, provider: str) -> str:
return styles[map_identifier][provider]
except KeyError:
return map_identifier


def get_default_map_identifier(provider: str):
try:
provider_enum = BaseMapProvider(provider)
except KeyError:
return DARK

return _default_map_identifers[provider_enum]
18 changes: 18 additions & 0 deletions bindings/pydeck/tests/bindings/test_deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from mock import MagicMock

from pydeck import Deck
from pydeck import map_styles
from pydeck.bindings.base_map_provider import BaseMapProvider
from IPython.display import HTML

from . import pydeck_examples
Expand All @@ -23,6 +25,22 @@ def test_deck_layer_args():
assert r.layers == expected_output


@pytest.mark.parametrize(
"map_provider_enum, expected_map_style",
(
(BaseMapProvider.CARTO, map_styles.CARTO_DARK),
(BaseMapProvider.MAPBOX, map_styles.MAPBOX_DARK),
(BaseMapProvider.GOOGLE_MAPS, map_styles.GOOGLE_ROAD),
),
ids=[BaseMapProvider.CARTO, BaseMapProvider.MAPBOX, BaseMapProvider.GOOGLE_MAPS],
)
def test_deck_default_map_style(map_provider_enum: BaseMapProvider, expected_map_style: str):
"""Verify the a default map style is provided for all map providers."""
r = Deck(**{"layers": [], "map_provider": map_provider_enum.value})
assert r.map_provider == map_provider_enum.value
assert r.map_style == expected_map_style


def test_json_output():
"""Verify that the JSON rendering produces an @deck.gl/json library-compliant JSON object

Expand Down
Loading