2
2
tilemap - Plot XYZ tile maps.
3
3
"""
4
4
5
- from __future__ import annotations
5
+ from typing import Literal
6
6
7
7
from pygmt .clib import Session
8
8
from pygmt .datasets .tile_map import load_tile_map
9
9
from pygmt .helpers import build_arg_string , fmt_docstring , kwargs_to_strings , use_alias
10
10
11
11
try :
12
12
import rioxarray # noqa: F401
13
+ from xyzservices import TileProvider
13
14
14
15
_HAS_RIOXARRAY = True
15
16
except ImportError :
17
+ TileProvider = None
16
18
_HAS_RIOXARRAY = False
17
19
18
20
34
36
@kwargs_to_strings (c = "sequence_comma" , p = "sequence" ) # R="sequence",
35
37
def tilemap (
36
38
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 ,
43
45
zoom_adjust : int | None = None ,
44
46
** kwargs ,
45
47
):
46
48
r"""
47
49
Plot an XYZ tile map.
48
50
49
51
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`.
52
54
53
55
**Note**: By default, standard web map tiles served in a Spherical Mercator
54
56
(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.
59
60
60
61
{aliases}
61
62
62
63
Parameters
63
64
----------
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.
77
76
78
77
.. note::
79
78
The maximum possible zoom level may be smaller than ``22``, and depends on
80
79
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.
94
89
``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
98
92
:doc:`contextily:working_with_local_files`.
99
93
100
94
.. important::
101
95
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.
117
105
zoom_adjust
118
106
The amount to adjust a chosen zoom level if it is chosen automatically. Values
119
107
outside of -1 to 1 are not recommended as they can lead to slow execution.
@@ -128,18 +116,16 @@ def tilemap(
128
116
------
129
117
ImportError
130
118
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.
134
121
"""
135
122
kwargs = self ._preprocess (** kwargs )
136
123
137
124
if not _HAS_RIOXARRAY :
138
125
raise ImportError (
139
126
"Package `rioxarray` is required to be installed to use this function. "
140
127
"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."
143
129
)
144
130
145
131
raster = load_tile_map (
@@ -152,14 +138,14 @@ def tilemap(
152
138
zoom_adjust = zoom_adjust ,
153
139
)
154
140
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
157
143
if lonlat and raster .rio .crs == "EPSG:3857" :
158
144
raster = raster .rio .reproject (dst_crs = "OGC:CRS84" )
159
145
raster .gmt .gtype = 1 # set to geographic type
160
146
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
163
149
if kwargs .get ("N" ) in [None , False ]:
164
150
kwargs ["R" ] = "/" .join (str (coordinate ) for coordinate in region )
165
151
0 commit comments