Skip to content

Commit af998d1

Browse files
authored
Feature/new tile providers (flopp#23)
- allow for specifying API keys for tile providers on the command line - add carto providers with labels - new tile_provider "stadia maps" (requires key) - new tile_provider "jawg maps" (requires key)
1 parent f3bd3c2 commit af998d1

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

staticmaps/cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ def main() -> None:
8686
choices=staticmaps.default_tile_providers.keys(),
8787
default=staticmaps.tile_provider_OSM.name(),
8888
)
89+
args_parser.add_argument(
90+
"--tiles-api-key",
91+
dest="tiles_api_key",
92+
metavar="API_KEY",
93+
type=str,
94+
default=None,
95+
)
8996
args_parser.add_argument(
9097
"--file-format",
9198
metavar="FORMAT",
@@ -104,7 +111,7 @@ def main() -> None:
104111

105112
context = staticmaps.Context()
106113

107-
context.set_tile_provider(staticmaps.default_tile_providers[args.tiles])
114+
context.set_tile_provider(staticmaps.default_tile_providers[args.tiles], args.tiles_api_key)
108115

109116
if args.center is not None:
110117
context.set_center(staticmaps.parse_latlng(args.center))

staticmaps/context.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ def set_tile_downloader(self, downloader: TileDownloader) -> None:
7777
"""
7878
self._tile_downloader = downloader
7979

80-
def set_tile_provider(self, provider: TileProvider) -> None:
80+
def set_tile_provider(self, provider: TileProvider, api_key: typing.Optional[str] = None) -> None:
8181
"""Set tile provider
8282
8383
:param provider: tile provider
8484
:type provider: TileProvider
85+
:param api_key: api key (if needed)
86+
:type api_key: str
8587
"""
8688
self._tile_provider = provider
89+
if api_key:
90+
self._tile_provider.set_api_key(api_key)
8791

8892
def add_object(self, obj: Object) -> None:
8993
"""Add object for the static map (e.g. line, area, marker)

staticmaps/tile_provider.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ def url(self, zoom: int, x: int, y: int) -> typing.Optional[str]:
127127
max_zoom=24,
128128
)
129129

130+
tile_provider_Carto = TileProvider(
131+
"carto",
132+
url_pattern="http://$s.basemaps.cartocdn.com/rastertiles/light_all/$z/$x/$y.png",
133+
shards=["a", "b", "c", "d"],
134+
attribution="Maps (C) CARTO (C) OpenStreetMap.org contributors",
135+
max_zoom=20,
136+
)
137+
130138
tile_provider_CartoNoLabels = TileProvider(
131139
"carto-nolabels",
132140
url_pattern="http://$s.basemaps.cartocdn.com/rastertiles/light_nolabels/$z/$x/$y.png",
@@ -135,6 +143,14 @@ def url(self, zoom: int, x: int, y: int) -> typing.Optional[str]:
135143
max_zoom=20,
136144
)
137145

146+
tile_provider_CartoDark = TileProvider(
147+
"carto-dark",
148+
url_pattern="http://$s.basemaps.cartocdn.com/rastertiles/dark_all/$z/$x/$y.png",
149+
shards=["a", "b", "c", "d"],
150+
attribution="Maps (C) CARTO (C) OpenStreetMap.org contributors",
151+
max_zoom=20,
152+
)
153+
138154
tile_provider_CartoDarkNoLabels = TileProvider(
139155
"carto-darknolabels",
140156
url_pattern="http://$s.basemaps.cartocdn.com/rastertiles/dark_nolabels/$z/$x/$y.png",
@@ -143,6 +159,39 @@ def url(self, zoom: int, x: int, y: int) -> typing.Optional[str]:
143159
max_zoom=20,
144160
)
145161

162+
tile_provider_StadiaAlidadeSmooth = TileProvider(
163+
"stadia-alidade-smooth",
164+
url_pattern="https://tiles.stadiamaps.com/tiles/alidade_smooth/$z/$x/$y.png?api_key=$k",
165+
shards=["a", "b", "c", "d"],
166+
attribution="Maps (C) Stadia Maps (C) OpenMapTiles (C) OpenStreetMap.org contributors",
167+
max_zoom=20,
168+
api_key="",
169+
)
170+
171+
tile_provider_StadiaAlidadeSmoothDark = TileProvider(
172+
"stadia-alidade-smooth",
173+
url_pattern="https://tiles.stadiamaps.com/tiles/alidade_smooth_dark/$z/$x/$y.png?api_key=$k",
174+
shards=["a", "b", "c", "d"],
175+
attribution="Maps (C) Stadia Maps (C) OpenMapTiles (C) OpenStreetMap.org contributors",
176+
max_zoom=20,
177+
)
178+
179+
tile_provider_JawgLight = TileProvider(
180+
"jawg-light",
181+
url_pattern="https://$s.tile.jawg.io/jawg-light/$z/$x/$y.png?access-token=$k",
182+
shards=["a", "b", "c", "d"],
183+
attribution="Maps (C) Jawg Maps (C) OpenStreetMap.org contributors",
184+
max_zoom=22,
185+
)
186+
187+
tile_provider_JawgDark = TileProvider(
188+
"jawg-dark",
189+
url_pattern="https://$s.tile.jawg.io/jawg-dark/$z/$x/$y.png?access-token=$k",
190+
shards=["a", "b", "c", "d"],
191+
attribution="Maps (C) Jawg Maps (C) OpenStreetMap.org contributors",
192+
max_zoom=22,
193+
)
194+
146195
tile_provider_None = TileProvider(
147196
"none",
148197
url_pattern="",
@@ -156,7 +205,13 @@ def url(self, zoom: int, x: int, y: int) -> typing.Optional[str]:
156205
tile_provider_StamenToner.name(): tile_provider_StamenToner,
157206
tile_provider_StamenTonerLite.name(): tile_provider_StamenTonerLite,
158207
tile_provider_ArcGISWorldImagery.name(): tile_provider_ArcGISWorldImagery,
208+
tile_provider_Carto.name(): tile_provider_Carto,
159209
tile_provider_CartoNoLabels.name(): tile_provider_CartoNoLabels,
210+
tile_provider_CartoDark.name(): tile_provider_CartoDark,
160211
tile_provider_CartoDarkNoLabels.name(): tile_provider_CartoDarkNoLabels,
212+
tile_provider_StadiaAlidadeSmooth.name(): tile_provider_StadiaAlidadeSmooth,
213+
tile_provider_StadiaAlidadeSmoothDark.name(): tile_provider_StadiaAlidadeSmoothDark,
214+
tile_provider_JawgLight.name(): tile_provider_JawgLight,
215+
tile_provider_JawgDark.name(): tile_provider_JawgDark,
161216
tile_provider_None.name(): tile_provider_None,
162217
}

0 commit comments

Comments
 (0)