Skip to content

Commit c34a56f

Browse files
committed
tilemap: Use the new 'crs' parameter to simplify the tilemap function
1 parent d496104 commit c34a56f

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

pygmt/src/tilemap.py

+8-21
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22
tilemap - Plot XYZ tile maps.
33
"""
44

5+
import contextlib
56
from typing import Literal
67

78
from pygmt.clib import Session
89
from pygmt.datasets.tile_map import load_tile_map
910
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
1011

1112
try:
12-
import rioxarray # noqa: F401
1313
from xyzservices import TileProvider
1414

1515
_HAS_RIOXARRAY = True
1616
except ImportError:
1717
TileProvider = None
1818
_HAS_RIOXARRAY = False
1919

20+
with contextlib.suppress(ImportError):
21+
# rioxarray is needed to register the rio accessor
22+
import rioxarray # noqa: F401
23+
2024

2125
@fmt_docstring
2226
@use_alias(
@@ -111,38 +115,21 @@ def tilemap(
111115
112116
kwargs : dict
113117
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.
121118
"""
122119
kwargs = self._preprocess(**kwargs)
123120

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-
131121
raster = load_tile_map(
132122
region=region,
133123
zoom=zoom,
134124
source=source,
135125
lonlat=lonlat,
126+
crs="OGC:CRS84" if lonlat is True else "EPSG:3857",
136127
wait=wait,
137128
max_retries=max_retries,
138129
zoom_adjust=zoom_adjust,
139130
)
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
146133

147134
# Only set region if no_clip is None or False, so that plot is clipped to exact
148135
# bounding box region

0 commit comments

Comments
 (0)