Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load_tile_map: Register the rio accessor by importing rioxarray so the returned raster has CRS #3323

Merged
merged 8 commits into from
Jul 10, 2024
Merged
9 changes: 9 additions & 0 deletions pygmt/datasets/tile_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
:class:`xarray.DataArray`.
"""

import contextlib
from typing import Literal

from packaging.version import Version
Expand All @@ -16,6 +17,10 @@
TileProvider = None
_HAS_CONTEXTILY = False

with contextlib.suppress(ImportError):
# rioxarray is needed to register the rio accessor
import rioxarray # noqa: F401

import numpy as np
import xarray as xr

Expand Down Expand Up @@ -117,6 +122,10 @@ def load_tile_map(
* y (y) float64 ... -7.081e-10 -7.858e+04 ... -1.996e+07 -2.004e+07
* x (x) float64 ... -2.004e+07 -1.996e+07 ... 1.996e+07 2.004e+07
spatial_ref int64 ... 0
>>> # CRS is set only if rioxarray is available
>>> if hasattr(raster, "rio"):
... raster.rio.crs
Comment on lines 124 to +127
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, we probably don't need the if hasattr(raster, "rio") check, because the spatial_ref int64 ... 0 line above (added in #3321) already means that rioxarray is used (the doctests CI run with rioxarray installed). But I guess it'll be good to mention rioxarray somewhere in the docstring, albeit indirectly.

CRS.from_epsg(3857)
"""
if not _HAS_CONTEXTILY:
raise ImportError(
Expand Down