|
1 | | -from typing import NamedTuple |
| 1 | +from __future__ import annotations |
2 | 2 |
|
| 3 | +from dataclasses import asdict, dataclass |
| 4 | +from typing import Any |
3 | 5 |
|
4 | | -class ViewState(NamedTuple): |
5 | | - """State of a view position of a map.""" |
| 6 | + |
| 7 | +class BaseViewState: |
| 8 | + """Base class for view states.""" |
| 9 | + |
| 10 | + |
| 11 | +@dataclass(frozen=True) |
| 12 | +class MapViewState(BaseViewState): |
| 13 | + """State of a [MapView][lonboard.view.MapView].""" |
| 14 | + |
| 15 | + longitude: float = 0 |
| 16 | + """longitude at the map center""" |
| 17 | + |
| 18 | + latitude: float = 10 |
| 19 | + """latitude at the map center.""" |
| 20 | + |
| 21 | + zoom: float = 0.5 |
| 22 | + """zoom level.""" |
| 23 | + |
| 24 | + pitch: float = 0 |
| 25 | + """pitch angle in degrees. `0` is top-down.""" |
| 26 | + |
| 27 | + bearing: float = 0 |
| 28 | + """bearing angle in degrees. `0` is north.""" |
| 29 | + |
| 30 | + max_zoom: float = 20 |
| 31 | + """max zoom level.""" |
| 32 | + |
| 33 | + min_zoom: float = 0 |
| 34 | + """min zoom level.""" |
| 35 | + |
| 36 | + max_pitch: float = 60 |
| 37 | + """max pitch angle.""" |
| 38 | + |
| 39 | + min_pitch: float = 0 |
| 40 | + """min pitch angle.""" |
| 41 | + |
| 42 | + |
| 43 | +@dataclass(frozen=True) |
| 44 | +class GlobeViewState(BaseViewState): |
| 45 | + """State of a [GlobeView][lonboard.view.GlobeView].""" |
6 | 46 |
|
7 | 47 | longitude: float |
8 | | - """Longitude at the map center""" |
| 48 | + """longitude at the viewport center.""" |
9 | 49 |
|
10 | 50 | latitude: float |
11 | | - """Latitude at the map center.""" |
| 51 | + """latitude at the viewport center.""" |
12 | 52 |
|
13 | 53 | zoom: float |
14 | | - """Zoom level.""" |
| 54 | + """zoom level.""" |
| 55 | + |
| 56 | + max_zoom: float = 20 |
| 57 | + """max zoom level. Default 20.""" |
| 58 | + |
| 59 | + min_zoom: float = 0 |
| 60 | + """min zoom level. Default 0.""" |
| 61 | + |
| 62 | + |
| 63 | +@dataclass(frozen=True) |
| 64 | +class FirstPersonViewState(BaseViewState): |
| 65 | + """State of a [FirstPersonView][lonboard.view.FirstPersonView].""" |
| 66 | + |
| 67 | + longitude: float |
| 68 | + """longitude of the camera position.""" |
| 69 | + |
| 70 | + latitude: float |
| 71 | + """latitude of the camera position.""" |
| 72 | + |
| 73 | + position: tuple[float, float, float] = (0.0, 0.0, 0.0) |
| 74 | + """Meter offsets of the camera from the lng-lat anchor point.""" |
| 75 | + |
| 76 | + bearing: float = 0.0 |
| 77 | + """bearing angle in degrees. `0` is north.""" |
| 78 | + |
| 79 | + pitch: float = 0.0 |
| 80 | + """pitch angle in degrees. `0` is horizontal.""" |
| 81 | + |
| 82 | + max_pitch: float = 90.0 |
| 83 | + """max pitch angle. Default 90 (down).""" |
| 84 | + |
| 85 | + min_pitch: float = -90.0 |
| 86 | + """min pitch angle. Default -90 (up).""" |
| 87 | + |
| 88 | + |
| 89 | +@dataclass(frozen=True) |
| 90 | +class OrthographicViewState(BaseViewState): |
| 91 | + """State of an [OrthographicView][lonboard.view.OrthographicView].""" |
| 92 | + |
| 93 | + target: tuple[float, float, float] = (0.0, 0.0, 0.0) |
| 94 | + """The world position at the center of the viewport.""" |
| 95 | + |
| 96 | + zoom: float | tuple[float, float] = 0.0 |
| 97 | + """The zoom level of the viewport. |
| 98 | +
|
| 99 | + - `zoom: 0` maps one unit distance to one pixel on screen, and increasing `zoom` by `1` scales the same object to twice as large. For example `zoom: 1` is 2x the original size, `zoom: 2` is 4x, `zoom: 3` is 8x etc. |
| 100 | +
|
| 101 | + To apply independent zoom levels to the X and Y axes, supply a tuple [zoomX, zoomY]. |
| 102 | +
|
| 103 | + Default 0. |
| 104 | + """ |
| 105 | + |
| 106 | + min_zoom: float | None = None |
| 107 | + """The min zoom level of the viewport. Default -Infinity.""" |
| 108 | + |
| 109 | + max_zoom: float | None = None |
| 110 | + """The max zoom level of the viewport. Default Infinity.""" |
| 111 | + |
| 112 | + |
| 113 | +@dataclass(frozen=True) |
| 114 | +class OrbitViewState(BaseViewState): |
| 115 | + """State of an [OrbitView][lonboard.view.OrbitView].""" |
| 116 | + |
| 117 | + target: tuple[float, float, float] = (0.0, 0.0, 0.0) |
| 118 | + """The world position at the center of the viewport.""" |
| 119 | + |
| 120 | + rotation_orbit: float = 0.0 |
| 121 | + """Rotating angle around orbit axis. Default 0.""" |
| 122 | + |
| 123 | + rotation_x: float = 0.0 |
| 124 | + """Rotating angle around X axis. Default 0.""" |
| 125 | + |
| 126 | + zoom: float = 0.0 |
| 127 | + """The zoom level of the viewport. |
| 128 | +
|
| 129 | + `zoom: 0` maps one unit distance to one pixel on screen, and increasing `zoom` by |
| 130 | + `1` scales the same object to twice as large. |
| 131 | +
|
| 132 | + Default 0. |
| 133 | + """ |
| 134 | + |
| 135 | + min_zoom: float | None = None |
| 136 | + """The min zoom level of the viewport. Default -Infinity.""" |
| 137 | + |
| 138 | + max_zoom: float | None = None |
| 139 | + """The max zoom level of the viewport. Default Infinity.""" |
| 140 | + |
| 141 | + min_rotation_x: float = -90.0 |
| 142 | + """The min rotating angle around X axis. Default -90.""" |
| 143 | + |
| 144 | + max_rotation_x: float = 90.0 |
| 145 | + """The max rotating angle around X axis. Default 90.""" |
| 146 | + |
| 147 | + |
| 148 | +def _to_camel(s: str) -> str: |
| 149 | + parts = s.split("_") |
| 150 | + return parts[0] + "".join(p.title() for p in parts[1:]) |
| 151 | + |
15 | 152 |
|
16 | | - pitch: float |
17 | | - """Pitch angle in degrees. `0` is top-down.""" |
| 153 | +def _serialize_view_state(data: BaseViewState | None, _obj: Any) -> Any: |
| 154 | + if data is None: |
| 155 | + return None |
18 | 156 |
|
19 | | - bearing: float |
20 | | - """Bearing angle in degrees. `0` is north.""" |
| 157 | + d = asdict(data) # type: ignore |
| 158 | + # Convert to camel case and remove None values |
| 159 | + return {_to_camel(k): v for k, v in d.items() if v is not None} |
0 commit comments