11from __future__ import annotations
22
33import warnings
4+ from dataclasses import replace
45from pathlib import Path
56from typing import IO , TYPE_CHECKING , Any , TextIO , overload
67
2122)
2223from lonboard .layer import BaseLayer
2324from lonboard .traits import HeightTrait , VariableLengthTuple , ViewStateTrait
24- from lonboard .traits . _map import DEFAULT_INITIAL_VIEW_STATE
25- from lonboard .view import BaseView
25+ from lonboard .view import BaseView , GlobeView , MapView
26+ from lonboard .view_state import BaseViewState , GlobeViewState , MapViewState
2627
2728if TYPE_CHECKING :
2829 import sys
@@ -154,13 +155,13 @@ def on_click(self, callback: Callable, *, remove: bool = False) -> None:
154155 _esm = bundler_output_dir / "index.js"
155156 _css = bundler_output_dir / "index.css"
156157
157- # TODO: change this view state to allow non-map view states if we have non-map views
158- # Also allow a list/tuple of view states for multiple views
159- view_state = ViewStateTrait ()
158+ view_state : BaseViewState | None = ViewStateTrait () # type: ignore
160159 """
161160 The view state of the map.
162161
163- - Type: [`ViewState`][lonboard.models.ViewState]
162+ - Type: A subclass of [`BaseViewState`][lonboard.view_state.BaseViewState], such as
163+ [`MapViewState`][lonboard.view_state.MapViewState] or
164+ [`GlobeViewState`][lonboard.view_state.GlobeViewState].
164165 - Default: Automatically inferred from the data passed to the map.
165166
166167 You can initialize the map to a specific view state using this property:
@@ -492,8 +493,9 @@ def add_layer(
492493 elif reset_zoom :
493494 self .view_state = compute_view (self .layers ) # type: ignore
494495
495- def set_view_state (
496+ def set_view_state ( # noqa: PLR0913
496497 self ,
498+ view_state : BaseViewState | None = None ,
497499 * ,
498500 longitude : float | None = None ,
499501 latitude : float | None = None ,
@@ -505,6 +507,9 @@ def set_view_state(
505507
506508 Any parameters that are unset will not be changed.
507509
510+ Args:
511+ view_state: A complete view state object to set on the map.
512+
508513 Keyword Args:
509514 longitude: the new longitude to set on the map. Defaults to None.
510515 latitude: the new latitude to set on the map. Defaults to None.
@@ -513,24 +518,38 @@ def set_view_state(
513518 bearing: the new bearing to set on the map. Defaults to None.
514519
515520 """
516- view_state = (
517- self .view_state ._asdict () # type: ignore
518- if self .view_state is not None
519- else DEFAULT_INITIAL_VIEW_STATE
520- )
521+ if view_state is not None :
522+ self .view_state = view_state
523+ return
521524
525+ current_view_state = self .view_state
526+
527+ changes = {}
522528 if longitude is not None :
523- view_state ["longitude" ] = longitude
529+ changes ["longitude" ] = longitude
524530 if latitude is not None :
525- view_state ["latitude" ] = latitude
531+ changes ["latitude" ] = latitude
526532 if zoom is not None :
527- view_state ["zoom" ] = zoom
533+ changes ["zoom" ] = zoom
534+
535+ # Only params allowed by globe view state
536+ if isinstance (current_view_state , GlobeViewState ):
537+ self .view_state = replace (current_view_state , ** changes )
538+ return
539+
540+ # Add more params allowed by map view state
528541 if pitch is not None :
529- view_state ["pitch" ] = pitch
542+ changes ["pitch" ] = pitch
530543 if bearing is not None :
531- view_state ["bearing" ] = bearing
544+ changes ["bearing" ] = bearing
545+
546+ if isinstance (current_view_state , MapViewState ):
547+ self .view_state = replace (current_view_state , ** changes )
548+ return
532549
533- self .view_state = view_state
550+ raise TypeError (
551+ "Can only set MapViewState or GlobeViewState parameters individually via set_view_state.\n For other view state types, pass a complete view_state object." ,
552+ )
534553
535554 def fly_to ( # noqa: PLR0913
536555 self ,
@@ -656,4 +675,7 @@ def as_html(self) -> HTML:
656675
657676 @traitlets .default ("view_state" )
658677 def _default_initial_view_state (self ) -> dict [str , Any ]:
659- return compute_view (self .layers ) # type: ignore
678+ if isinstance (self .views , (MapView , GlobeView )):
679+ return compute_view (self .layers ) # type: ignore
680+
681+ return {}
0 commit comments