Skip to content

Commit c9c3a4a

Browse files
weiji14seismanyvonnefroehlich
authored
Add zoom_adjust param to pygmt.datasets.load_tile_map and Figure.tilemap (#2934)
The zoom_adjust parameter is used to adjust the automatic zoom level by integer increments. This was added in contextily=1.5.0, see geopandas/contextily#228. * Document that zoom_adjust requires contextily>=1.5.0 * Raise error if zoom_adjust parameter is used with contextily<1.5.0 * Add type hint for zoom_adjust parameter * Fix typecheck by using np.array constructor instead of np.uint8 Fixes `error: Argument 1 to "unsignedinteger" has incompatible type "list[int]"; expected "SupportsInt | str | bytes | SupportsIndex" [arg-type]` * Change directive for zoom_adjust from versionadded to note * Use note and important admonitions in other parts of the docstring * Rely on sphinx-autodoc-typehints --------- Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Yvonne Fröhlich <[email protected]>
1 parent b379976 commit c9c3a4a

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

pygmt/datasets/tile_map.py

+35-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
Function to load raster tile maps from XYZ tile providers, and load as
33
:class:`xarray.DataArray`.
44
"""
5+
from __future__ import annotations
6+
7+
from packaging.version import Version
58

69
try:
710
import contextily
@@ -16,7 +19,15 @@
1619
__doctest_requires__ = {("load_tile_map"): ["contextily"]}
1720

1821

19-
def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_retries=2):
22+
def load_tile_map(
23+
region,
24+
zoom="auto",
25+
source=None,
26+
lonlat=True,
27+
wait=0,
28+
max_retries=2,
29+
zoom_adjust: int | None = None,
30+
):
2031
"""
2132
Load a georeferenced raster tile map from XYZ tile providers.
2233
@@ -40,9 +51,9 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret
4051
``"auto"`` to automatically determine the zoom level based on the
4152
bounding box region extent].
4253
43-
**Note**: The maximum possible zoom level may be smaller than ``22``,
44-
and depends on what is supported by the chosen web tile provider
45-
source.
54+
.. note::
55+
The maximum possible zoom level may be smaller than ``22``, and depends on
56+
what is supported by the chosen web tile provider source.
4657
4758
source : xyzservices.TileProvider or str
4859
Optional. The tile source: web tile provider or path to a local file.
@@ -62,8 +73,8 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret
6273
basemap. See
6374
:doc:`contextily:working_with_local_files`.
6475
65-
IMPORTANT: Tiles are assumed to be in the Spherical Mercator projection
66-
(EPSG:3857).
76+
.. important::
77+
Tiles are assumed to be in the Spherical Mercator projection (EPSG:3857).
6778
6879
lonlat : bool
6980
Optional. If ``False``, coordinates in ``region`` are assumed to be
@@ -79,6 +90,13 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret
7990
will stop trying to fetch more tiles from a rate-limited API [Default
8091
is ``2``].
8192
93+
zoom_adjust
94+
The amount to adjust a chosen zoom level if it is chosen automatically. Values
95+
outside of -1 to 1 are not recommended as they can lead to slow execution.
96+
97+
.. note::
98+
The ``zoom_adjust`` parameter requires ``contextily>=1.5.0``.
99+
82100
Returns
83101
-------
84102
raster : xarray.DataArray
@@ -117,6 +135,15 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret
117135
"to install the package."
118136
)
119137

138+
contextily_kwargs = {}
139+
if zoom_adjust is not None:
140+
contextily_kwargs["zoom_adjust"] = zoom_adjust
141+
if Version(contextily.__version__) < Version("1.5.0"):
142+
raise TypeError(
143+
"The `zoom_adjust` parameter requires `contextily>=1.5.0` to work. "
144+
"Please upgrade contextily, or manually set the `zoom` level instead."
145+
)
146+
120147
west, east, south, north = region
121148
image, extent = contextily.bounds2img(
122149
w=west,
@@ -128,6 +155,7 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret
128155
ll=lonlat,
129156
wait=wait,
130157
max_retries=max_retries,
158+
**contextily_kwargs,
131159
)
132160

133161
# Turn RGBA img from channel-last to channel-first and get 3-band RGB only
@@ -139,7 +167,7 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret
139167
dataarray = xr.DataArray(
140168
data=rgb_image,
141169
coords={
142-
"band": np.uint8([0, 1, 2]), # Red, Green, Blue
170+
"band": np.array(object=[0, 1, 2], dtype=np.uint8), # Red, Green, Blue
143171
"y": np.linspace(start=top, stop=bottom, num=rgb_image.shape[1]),
144172
"x": np.linspace(start=left, stop=right, num=rgb_image.shape[2]),
145173
},

pygmt/src/tilemap.py

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
tilemap - Plot XYZ tile maps.
33
"""
4+
from __future__ import annotations
5+
46
from pygmt.clib import Session
57
from pygmt.datasets.tile_map import load_tile_map
68
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
@@ -30,7 +32,15 @@
3032
)
3133
@kwargs_to_strings(c="sequence_comma", p="sequence") # R="sequence",
3234
def tilemap(
33-
self, region, zoom="auto", source=None, lonlat=True, wait=0, max_retries=2, **kwargs
35+
self,
36+
region,
37+
zoom="auto",
38+
source=None,
39+
lonlat=True,
40+
wait=0,
41+
max_retries=2,
42+
zoom_adjust: int | None = None,
43+
**kwargs,
3444
):
3545
r"""
3646
Plots an XYZ tile map.
@@ -64,9 +74,9 @@ def tilemap(
6474
``"auto"`` to automatically determine the zoom level based on the
6575
bounding box region extent].
6676
67-
**Note**: The maximum possible zoom level may be smaller than ``22``,
68-
and depends on what is supported by the chosen web tile provider
69-
source.
77+
.. note::
78+
The maximum possible zoom level may be smaller than ``22``, and depends on
79+
what is supported by the chosen web tile provider source.
7080
7181
source : xyzservices.TileProvider or str
7282
Optional. The tile source: web tile provider or path to a local file.
@@ -86,8 +96,8 @@ def tilemap(
8696
basemap. See
8797
:doc:`contextily:working_with_local_files`.
8898
89-
IMPORTANT: Tiles are assumed to be in the Spherical Mercator projection
90-
(EPSG:3857).
99+
.. important::
100+
Tiles are assumed to be in the Spherical Mercator projection (EPSG:3857).
91101
92102
lonlat : bool
93103
Optional. If ``False``, coordinates in ``region`` are assumed to be
@@ -103,6 +113,13 @@ def tilemap(
103113
will stop trying to fetch more tiles from a rate-limited API [Default
104114
is ``2``].
105115
116+
zoom_adjust
117+
The amount to adjust a chosen zoom level if it is chosen automatically. Values
118+
outside of -1 to 1 are not recommended as they can lead to slow execution.
119+
120+
.. note::
121+
The ``zoom_adjust`` parameter requires ``contextily>=1.5.0``.
122+
106123
kwargs : dict
107124
Extra keyword arguments to pass to :meth:`pygmt.Figure.grdimage`.
108125
@@ -131,6 +148,7 @@ def tilemap(
131148
lonlat=lonlat,
132149
wait=wait,
133150
max_retries=max_retries,
151+
zoom_adjust=zoom_adjust,
134152
)
135153

136154
# Reproject raster from Spherical Mercator (EPSG:3857) to

0 commit comments

Comments
 (0)