Skip to content

Commit 598154a

Browse files
authored
TYP: Add type hints to the Figure.tilemap method (#3095)
1 parent 7fd8253 commit 598154a

File tree

1 file changed

+52
-66
lines changed

1 file changed

+52
-66
lines changed

pygmt/src/tilemap.py

+52-66
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
tilemap - Plot XYZ tile maps.
33
"""
44

5-
from __future__ import annotations
5+
from typing import Literal
66

77
from pygmt.clib import Session
88
from pygmt.datasets.tile_map import load_tile_map
99
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
1010

1111
try:
1212
import rioxarray # noqa: F401
13+
from xyzservices import TileProvider
1314

1415
_HAS_RIOXARRAY = True
1516
except ImportError:
17+
TileProvider = None
1618
_HAS_RIOXARRAY = False
1719

1820

@@ -34,86 +36,72 @@
3436
@kwargs_to_strings(c="sequence_comma", p="sequence") # R="sequence",
3537
def tilemap(
3638
self,
37-
region,
38-
zoom="auto",
39-
source=None,
40-
lonlat=True,
41-
wait=0,
42-
max_retries=2,
39+
region: list,
40+
zoom: int | Literal["auto"] = "auto",
41+
source: TileProvider | str | None = None,
42+
lonlat: bool = True,
43+
wait: int = 0,
44+
max_retries: int = 2,
4345
zoom_adjust: int | None = None,
4446
**kwargs,
4547
):
4648
r"""
4749
Plot an XYZ tile map.
4850
4951
This method loads XYZ tile maps from a tile server or local file using
50-
:func:`pygmt.datasets.load_tile_map` into a georeferenced form, and plots
51-
the tiles as a basemap or overlay using :meth:`pygmt.Figure.grdimage`.
52+
:func:`pygmt.datasets.load_tile_map` into a georeferenced form, and plots the tiles
53+
as a basemap or overlay using :meth:`pygmt.Figure.grdimage`.
5254
5355
**Note**: By default, standard web map tiles served in a Spherical Mercator
5456
(EPSG:3857) Cartesian format will be reprojected to a geographic coordinate
55-
reference system (OGC:WGS84) and plotted with longitude/latitude bounds
56-
when ``lonlat=True``. If reprojection is not desired, please set
57-
``lonlat=False`` and provide Spherical Mercator (EPSG:3857) coordinates to
58-
the ``region`` parameter.
57+
reference system (OGC:WGS84) and plotted with longitude/latitude bounds when
58+
``lonlat=True``. If reprojection is not desired, please set ``lonlat=False`` and
59+
provide Spherical Mercator (EPSG:3857) coordinates to the ``region`` parameter.
5960
6061
{aliases}
6162
6263
Parameters
6364
----------
64-
region : list
65-
The bounding box of the map in the form of a list [*xmin*, *xmax*,
66-
*ymin*, *ymax*]. These coordinates should be in longitude/latitude if
67-
``lonlat=True`` or Spherical Mercator (EPSG:3857) if ``lonlat=False``.
68-
69-
zoom : int or str
70-
Optional. Level of detail. Higher levels (e.g. ``22``) mean a zoom
71-
level closer to the Earth's surface, with more tiles covering a smaller
72-
geographical area and thus more detail. Lower levels (e.g. ``0``) mean
73-
a zoom level further from the Earth's surface, with less tiles covering
74-
a larger geographical area and thus less detail [Default is
75-
``"auto"`` to automatically determine the zoom level based on the
76-
bounding box region extent].
65+
region
66+
The bounding box of the map in the form of a list [*xmin*, *xmax*, *ymin*,
67+
*ymax*]. These coordinates should be in longitude/latitude if ``lonlat=True`` or
68+
Spherical Mercator (EPSG:3857) if ``lonlat=False``.
69+
zoom
70+
Level of detail. Higher levels (e.g. ``22``) mean a zoom level closer to the
71+
Earth's surface, with more tiles covering a smaller geographical area and thus
72+
more detail. Lower levels (e.g. ``0``) mean a zoom level further from the
73+
Earth's surface, with less tiles covering a larger geographical area and thus
74+
less detail. Default is ``"auto"`` to automatically determine the zoom level
75+
based on the bounding box region extent.
7776
7877
.. note::
7978
The maximum possible zoom level may be smaller than ``22``, and depends on
8079
what is supported by the chosen web tile provider source.
81-
82-
source : xyzservices.TileProvider or str
83-
Optional. The tile source: web tile provider or path to a local file.
84-
Provide either:
85-
86-
- A web tile provider in the form of a
87-
:class:`xyzservices.TileProvider` object. See
88-
:doc:`Contextily providers <contextily:providers_deepdive>` for a
89-
list of tile providers [Default is
90-
``xyzservices.providers.OpenStreetMap.HOT``, i.e. OpenStreetMap
91-
Humanitarian web tiles].
92-
- A web tile provider in the form of a URL. The placeholders for the
93-
XYZ in the URL need to be {{x}}, {{y}}, {{z}}, respectively. E.g.
80+
source
81+
The tile source: web tile provider or path to a local file. Provide either:
82+
83+
- A web tile provider in the form of a :class:`xyzservices.TileProvider` object.
84+
See :doc:`Contextily providers <contextily:providers_deepdive>` for a list of
85+
tile providers. Default is ``xyzservices.providers.OpenStreetMap.HOT``, i.e.
86+
OpenStreetMap Humanitarian web tiles.
87+
- A web tile provider in the form of a URL. The placeholders for the XYZ in the
88+
URL need to be ``{{x}}``, ``{{y}}``, ``{{z}}``, respectively. E.g.
9489
``https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png``.
95-
- A local file path. The file is read with
96-
:doc:`rasterio <rasterio:index>` and all bands are loaded into the
97-
basemap. See
90+
- A local file path. The file is read with :doc:`rasterio <rasterio:index>` and
91+
all bands are loaded into the basemap. See
9892
:doc:`contextily:working_with_local_files`.
9993
10094
.. important::
10195
Tiles are assumed to be in the Spherical Mercator projection (EPSG:3857).
102-
103-
lonlat : bool
104-
Optional. If ``False``, coordinates in ``region`` are assumed to be
105-
Spherical Mercator as opposed to longitude/latitude [Default is
106-
``True``].
107-
108-
wait : int
109-
Optional. If the tile API is rate-limited, the number of seconds to
110-
wait between a failed request and the next try [Default is ``0``].
111-
112-
max_retries : int
113-
Optional. Total number of rejected requests allowed before contextily
114-
will stop trying to fetch more tiles from a rate-limited API [Default
115-
is ``2``].
116-
96+
lonlat
97+
If ``False``, coordinates in ``region`` are assumed to be Spherical Mercator as
98+
opposed to longitude/latitude.
99+
wait
100+
If the tile API is rate-limited, the number of seconds to wait between a failed
101+
request and the next try.
102+
max_retries
103+
Total number of rejected requests allowed before contextily will stop trying to
104+
fetch more tiles from a rate-limited API.
117105
zoom_adjust
118106
The amount to adjust a chosen zoom level if it is chosen automatically. Values
119107
outside of -1 to 1 are not recommended as they can lead to slow execution.
@@ -128,18 +116,16 @@ def tilemap(
128116
------
129117
ImportError
130118
If ``rioxarray`` is not installed. Follow
131-
:doc:`install instructions for rioxarray <rioxarray:installation>`,
132-
(e.g. via ``python -m pip install rioxarray``) before using this
133-
function.
119+
:doc:`install instructions for rioxarray <rioxarray:installation>`, (e.g. via
120+
``python -m pip install rioxarray``) before using this function.
134121
"""
135122
kwargs = self._preprocess(**kwargs)
136123

137124
if not _HAS_RIOXARRAY:
138125
raise ImportError(
139126
"Package `rioxarray` is required to be installed to use this function. "
140127
"Please use `python -m pip install rioxarray` or "
141-
"`mamba install -c conda-forge rioxarray` "
142-
"to install the package."
128+
"`mamba install -c conda-forge rioxarray` to install the package."
143129
)
144130

145131
raster = load_tile_map(
@@ -152,14 +138,14 @@ def tilemap(
152138
zoom_adjust=zoom_adjust,
153139
)
154140

155-
# Reproject raster from Spherical Mercator (EPSG:3857) to
156-
# lonlat (OGC:CRS84) if bounding box region was provided in lonlat
141+
# Reproject raster from Spherical Mercator (EPSG:3857) to lonlat (OGC:CRS84) if
142+
# bounding box region was provided in lonlat
157143
if lonlat and raster.rio.crs == "EPSG:3857":
158144
raster = raster.rio.reproject(dst_crs="OGC:CRS84")
159145
raster.gmt.gtype = 1 # set to geographic type
160146

161-
# Only set region if no_clip is None or False, so that plot is clipped to
162-
# exact bounding box region
147+
# Only set region if no_clip is None or False, so that plot is clipped to exact
148+
# bounding box region
163149
if kwargs.get("N") in [None, False]:
164150
kwargs["R"] = "/".join(str(coordinate) for coordinate in region)
165151

0 commit comments

Comments
 (0)