Skip to content

Commit 511f633

Browse files
author
Stefan Kuethe
committed
Refactor basemap layers
1 parent fbb4731 commit 511f633

File tree

5 files changed

+84
-6
lines changed

5 files changed

+84
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import openlayers as ol
2+
from openlayers.basemaps import MapTilerBasemapLayer, MapTiler, CartoBasemapLayer, Carto
3+
4+
m = ol.Map()
5+
m.set_zoom(2)
6+
# m.add_layer(MapTilerBasemapLayer(MapTiler.TONER))
7+
m.add_layer(CartoBasemapLayer(Carto.VOYAGER_LABELS_UNDER))
8+
m.add_control(ol.MapTilerGeocodingControl())
9+
m.save()

src/openlayers/basemaps.py

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from __future__ import annotations
22

3+
import os
4+
35
from enum import Enum
46

5-
from pydantic import BaseModel
7+
from pydantic import BaseModel, Field
68

79
from .abstracts import LayerLike
810
from .models.layers import TileLayer
9-
from .models.sources import OSM, ImageTileSource
11+
from .models.sources import OSM, ImageTileSource, TileJSONSource
12+
from .constants import MAPTILER_API_KEY_ENV_VAR
1013

1114
# light_all,
1215
# dark_all,
@@ -25,7 +28,14 @@ class Carto(Enum):
2528

2629
LIGHT_ALL = "light_all"
2730
DARK_ALL = "dark_all"
28-
VOYAGER_NO_LABLES = "rastertiles/voyager_nolabels"
31+
LIGHT_NO_LABELS = "light_nolabels"
32+
LIGHT_ONLY_LABELS = "light_only_labels"
33+
DARK_NO_LABELS = "dark_nolabels"
34+
DARK_ONLY_LABELS = "dark_only_labels"
35+
VOYAGER = "rastertiles/voyager"
36+
VOYAGER_NO_LABELS = "rastertiles/voyager_nolabels"
37+
VOYAGER_ONLY_LABELS = "rastertiles/voyager_only_labels"
38+
VOYAGER_LABELS_UNDER = "rastertiles/voyager_labels_under"
2939

3040

3141
class CartoRasterStyle(BaseModel):
@@ -108,8 +118,63 @@ def carto(
108118

109119

110120
class CartoBasemapLayer(LayerLike):
111-
def __init__(self, style_name: Carto | str = Carto.DARK_ALL):
112-
self._model = BasemapLayer.carto(style_name)
121+
def __init__(
122+
self, style_name: Carto | str = Carto.DARK_ALL, double_resolution: bool = True
123+
):
124+
style = CartoRasterStyle(style=style_name, double_resolution=double_resolution)
125+
self._model = TileLayer(
126+
id=f"carto-{Carto(style_name).value.replace('_', '-').replace('/', '-')}",
127+
source=ImageTileSource(url=style.url, attributions=style.attribution),
128+
)
129+
130+
@property
131+
def model(self) -> TileLayer:
132+
return self._model
133+
134+
135+
class MapTiler(Enum):
136+
BASIC_V2 = "basic-v2"
137+
STREETS_V2 = "streets-v2"
138+
HYBRID = "hybrid"
139+
DATAVIZ_DARK = "dataviz-dark"
140+
141+
AQUARELLE = "aquarelle"
142+
BACKDROP = "backdrop"
143+
BASIC = "basic"
144+
BRIGHT = "bright"
145+
DATAVIZ = "dataviz"
146+
LANDSCAPE = "landscape"
147+
OCEAN = "ocean"
148+
OPEN_STREET_MAP = "openstreetmap"
149+
OUTDOOR = "outdoor"
150+
SATELLITE = "satellite"
151+
STREETS = "streets"
152+
TONER = "toner"
153+
TOPO = "topo"
154+
WINTER = "winter"
155+
156+
157+
class MapTilerValidator(BaseModel):
158+
api_key: str = Field(os.getenv(MAPTILER_API_KEY_ENV_VAR), validate_default=True)
159+
160+
161+
class MapTilerBasemapLayer(LayerLike):
162+
def __init__(
163+
self,
164+
style_name: MapTiler | str = MapTiler.STREETS_V2,
165+
api_key: str = os.getenv(MAPTILER_API_KEY_ENV_VAR),
166+
) -> None:
167+
style = (
168+
MapTiler(style_name).value
169+
if isinstance(style_name, MapTiler)
170+
else style_name
171+
)
172+
key = MapTilerValidator(api_key=api_key).api_key
173+
url = f"https://api.maptiler.com/maps/{style}/tiles.json?key={key}"
174+
self._model = TileLayer(
175+
id=f"maptiler-{style}",
176+
source=TileJSONSource(url=url, tile_size=512, cross_origin="anonymous"),
177+
)
113178

114179
@property
115180
def model(self) -> TileLayer:

src/openlayers/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
CARTO_ATTRIBUTION = '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, &copy; <a href="https://carto.com/attributions">CARTO</a>'
2+
3+
MAPTILER_API_KEY_ENV_VAR = "MAPTILER_API_KEY"

src/openlayers/models/controls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .core import OLBaseModel
1010
from .layers import LayerT, TileLayer
1111
from .sources import OSM
12+
from ..constants import MAPTILER_API_KEY_ENV_VAR
1213

1314

1415
# -- Base control
@@ -94,7 +95,7 @@ class MapTilerGeocodingControl(Control):
9495
"""MapTiler geocoding control"""
9596

9697
api_key: str = Field(
97-
os.getenv("MAPTILER_API_TOKEN"),
98+
os.getenv(MAPTILER_API_KEY_ENV_VAR),
9899
serialization_alias="apiKey",
99100
validate_default=True,
100101
)

src/openlayers/models/sources.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class TileJSONSource(ImageTileSource):
6464
See [TileJSON](https://openlayers.org/en/latest/apidoc/module-ol_source_TileJSON-TileJSON.html) for details.
6565
"""
6666

67+
tile_size: int | None = Field(None, serialization_alias="tileSize")
6768
cross_origin: str = Field("anonymous", serialization_alias="crossOrigin")
6869

6970
@property

0 commit comments

Comments
 (0)