|
2 | 2 | tilemap - Plot XYZ tile maps.
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import contextlib |
5 | 6 | from typing import Literal
|
6 | 7 |
|
7 | 8 | from pygmt.clib import Session
|
8 | 9 | from pygmt.datasets.tile_map import load_tile_map
|
9 | 10 | from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
|
10 | 11 |
|
11 | 12 | try:
|
12 |
| - import rioxarray # noqa: F401 |
13 | 13 | from xyzservices import TileProvider
|
14 | 14 |
|
15 | 15 | _HAS_RIOXARRAY = True
|
16 | 16 | except ImportError:
|
17 | 17 | TileProvider = None
|
18 | 18 | _HAS_RIOXARRAY = False
|
19 | 19 |
|
| 20 | +with contextlib.suppress(ImportError): |
| 21 | + # rioxarray is needed to register the rio accessor |
| 22 | + import rioxarray # noqa: F401 |
| 23 | + |
20 | 24 |
|
21 | 25 | @fmt_docstring
|
22 | 26 | @use_alias(
|
@@ -111,38 +115,21 @@ def tilemap(
|
111 | 115 |
|
112 | 116 | kwargs : dict
|
113 | 117 | Extra keyword arguments to pass to :meth:`pygmt.Figure.grdimage`.
|
114 |
| -
|
115 |
| - Raises |
116 |
| - ------ |
117 |
| - ImportError |
118 |
| - If ``rioxarray`` is not installed. Follow |
119 |
| - :doc:`install instructions for rioxarray <rioxarray:installation>`, (e.g. via |
120 |
| - ``python -m pip install rioxarray``) before using this function. |
121 | 118 | """
|
122 | 119 | kwargs = self._preprocess(**kwargs)
|
123 | 120 |
|
124 |
| - if not _HAS_RIOXARRAY: |
125 |
| - raise ImportError( |
126 |
| - "Package `rioxarray` is required to be installed to use this function. " |
127 |
| - "Please use `python -m pip install rioxarray` or " |
128 |
| - "`mamba install -c conda-forge rioxarray` to install the package." |
129 |
| - ) |
130 |
| - |
131 | 121 | raster = load_tile_map(
|
132 | 122 | region=region,
|
133 | 123 | zoom=zoom,
|
134 | 124 | source=source,
|
135 | 125 | lonlat=lonlat,
|
| 126 | + crs="OGC:CRS84" if lonlat is True else "EPSG:3857", |
136 | 127 | wait=wait,
|
137 | 128 | max_retries=max_retries,
|
138 | 129 | zoom_adjust=zoom_adjust,
|
139 | 130 | )
|
140 |
| - |
141 |
| - # Reproject raster from Spherical Mercator (EPSG:3857) to lonlat (OGC:CRS84) if |
142 |
| - # bounding box region was provided in lonlat |
143 |
| - if lonlat and raster.rio.crs == "EPSG:3857": |
144 |
| - raster = raster.rio.reproject(dst_crs="OGC:CRS84") |
145 |
| - raster.gmt.gtype = 1 # set to geographic type |
| 131 | + if lonlat: |
| 132 | + raster.gmt.gtype = 1 # Set to geographic type |
146 | 133 |
|
147 | 134 | # Only set region if no_clip is None or False, so that plot is clipped to exact
|
148 | 135 | # bounding box region
|
|
0 commit comments