Skip to content

Commit e5c2004

Browse files
author
Stefan Kuethe
committed
Refactor
1 parent 511f633 commit e5c2004

File tree

6 files changed

+46
-77
lines changed

6 files changed

+46
-77
lines changed

docs/api/basemaps.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
::: openlayers.basemaps.BasemapLayer
44

5-
::: openlayers.basemaps.Carto
5+
::: openlayers.basemaps.CartoBasemapLayer
6+
7+
::: openlayers.basemaps.MapTilerBasemapLayer

docs/examples/concepts/basemaps.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import openlayers as ol
2+
from openlayers.basemaps import CartoBasemapLayer, Carto
23

3-
# Use OSM basemap
4-
m = ol.Map(layers=[ol.BasemapLayer.osm()])
4+
# Use default OSM basemap
5+
m = ol.Map(layers=[ol.BasemapLayer()])
56

67
# Use a CartoDB basemap
7-
m = ol.Map(layers=[ol.BasemapLayer.carto()])
8+
m = ol.Map(layers=[CartoBasemapLayer(Carto.DARK_NO_LABELS)])

docs/examples/concepts/layers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
m = ol.Map(
1313
ol.View(rotation=3.14 / 8),
14-
layers=[ol.BasemapLayer.carto(), vector]
14+
layers=[ol.BasemapLayer(), vector]
1515
)
1616
m.add_default_tooltip()

docs/examples/concepts/view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
m = ol.Map(initial_view)
66

77
# Change view settings afterwards
8-
m.set_center((172.606201, -43.556510))
8+
m.set_center(lon=172.606201, lat=-43.556510)
99
m.set_zoom(14)
1010

1111
m.set_view(ol.View(center=(172.606201, -43.556510), zoom=12))
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import openlayers as ol
22
from openlayers.basemaps import MapTilerBasemapLayer, MapTiler, CartoBasemapLayer, Carto
33

4-
m = ol.Map()
4+
m = ol.Map(layers=[])
55
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())
6+
m.add_layer(ol.BasemapLayer())
7+
# m.add_layer(MapTilerBasemapLayer(MapTiler.AQUARELLE))
8+
# m.add_layer(CartoBasemapLayer(Carto.VOYAGER_LABELS_UNDER))
9+
m.add_control(ol.MapTilerGeocodingControl(country="de"))
910
m.save()

src/openlayers/basemaps.py

Lines changed: 32 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@
1111
from .models.sources import OSM, ImageTileSource, TileJSONSource
1212
from .constants import MAPTILER_API_KEY_ENV_VAR
1313

14+
# --- OSM
15+
16+
17+
class BasemapLayer(LayerLike):
18+
"""An OSM raster tile layer"""
19+
20+
def __init__(self) -> None:
21+
self._model = TileLayer(id="osm", source=OSM())
22+
23+
@property
24+
def model(self) -> TileLayer:
25+
return self._model
26+
27+
28+
"""
29+
new OGCMapTile({
30+
url: 'https://maps.gnosis.earth/ogcapi/collections/NaturalEarth:raster:HYP_HR_SR_OB_DR/map/tiles/WebMercatorQuad',
31+
crossOrigin: '',
32+
}),
33+
"""
34+
35+
# --- CartoDB
36+
1437
# light_all,
1538
# dark_all,
1639
# light_nolabels,
@@ -58,66 +81,9 @@ def url(self) -> str:
5881
)
5982

6083

61-
class BasemapLayer(LayerLike):
62-
def __init__(self, style: str | Carto = None) -> None:
63-
if isinstance(style, Carto):
64-
self._model = BasemapLayer.carto(style)
65-
else:
66-
self._model = BasemapLayer.osm()
67-
68-
@property
69-
def model(self) -> TileLayer:
70-
return self._model
71-
72-
@staticmethod
73-
def osm() -> TileLayer:
74-
"""Create a OSM tile layer object
75-
76-
Returns:
77-
An OSM raster tile layer
78-
79-
Examples:
80-
>>> from openlayers.basemaps import BasemapLayer
81-
>>> osm = BasemapLayer.osm()
82-
"""
83-
return TileLayer(id="osm", source=OSM())
84-
85-
@staticmethod
86-
def carto(
87-
style_name: str | Carto = Carto.DARK_ALL, double_resolution: bool = True
88-
) -> TileLayer:
89-
"""Create a CartoDB tile layer object
90-
91-
Note:
92-
See [CartoDB/basemap-styles](https://github.com/CartoDB/basemap-styles) for available styles.
93-
94-
Args:
95-
style_name (str | Carto): The name of the style
96-
double_resolution (bool): Whether to use double resolution tiles
97-
98-
Returns:
99-
A CartoDB raster tile layer
100-
101-
Examples:
102-
>>> from openlayers.basemaps import BasemapLayer
103-
>>> carto = BasemapLayer.carto()
104-
"""
105-
style = CartoRasterStyle(style=style_name, double_resolution=double_resolution)
106-
return TileLayer(
107-
id=f"carto-{Carto(style_name).value.replace('_', '-').replace('/', '-')}",
108-
source=ImageTileSource(url=style.url, attributions=style.attribution),
109-
)
110-
111-
112-
"""
113-
new OGCMapTile({
114-
url: 'https://maps.gnosis.earth/ogcapi/collections/NaturalEarth:raster:HYP_HR_SR_OB_DR/map/tiles/WebMercatorQuad',
115-
crossOrigin: '',
116-
}),
117-
"""
84+
class CartoBasemapLayer(BasemapLayer):
85+
"""A CartoDB raster tile layer"""
11886

119-
120-
class CartoBasemapLayer(LayerLike):
12187
def __init__(
12288
self, style_name: Carto | str = Carto.DARK_ALL, double_resolution: bool = True
12389
):
@@ -127,12 +93,13 @@ def __init__(
12793
source=ImageTileSource(url=style.url, attributions=style.attribution),
12894
)
12995

130-
@property
131-
def model(self) -> TileLayer:
132-
return self._model
96+
97+
# --- MapTiler
13398

13499

135100
class MapTiler(Enum):
101+
"""MapTiler basemap styles"""
102+
136103
BASIC_V2 = "basic-v2"
137104
STREETS_V2 = "streets-v2"
138105
HYBRID = "hybrid"
@@ -158,7 +125,9 @@ class MapTilerValidator(BaseModel):
158125
api_key: str = Field(os.getenv(MAPTILER_API_KEY_ENV_VAR), validate_default=True)
159126

160127

161-
class MapTilerBasemapLayer(LayerLike):
128+
class MapTilerBasemapLayer(BasemapLayer):
129+
"""A MapTiler raster tile layer"""
130+
162131
def __init__(
163132
self,
164133
style_name: MapTiler | str = MapTiler.STREETS_V2,
@@ -175,7 +144,3 @@ def __init__(
175144
id=f"maptiler-{style}",
176145
source=TileJSONSource(url=url, tile_size=512, cross_origin="anonymous"),
177146
)
178-
179-
@property
180-
def model(self) -> TileLayer:
181-
return self._model

0 commit comments

Comments
 (0)