Skip to content

Commit 4380c50

Browse files
authored
_load_remote_dataset: Refactor to write CRS information for raster images (#3678)
1 parent d2acb93 commit 4380c50

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

pygmt/datasets/earth_day.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55
The images are available in various resolutions.
66
"""
77

8-
import contextlib
98
from collections.abc import Sequence
109
from typing import Literal
1110

1211
import xarray as xr
1312
from pygmt.datasets.load_remote_dataset import _load_remote_dataset
1413

15-
with contextlib.suppress(ImportError):
16-
# rioxarray is needed to register the rio accessor
17-
import rioxarray # noqa: F401
18-
1914
__doctest_skip__ = ["load_blue_marble"]
2015

2116

@@ -98,7 +93,4 @@ def load_blue_marble(
9893
region=region,
9994
registration="pixel",
10095
)
101-
# If rioxarray is installed, set the coordinate reference system
102-
if hasattr(image, "rio"):
103-
image = image.rio.write_crs(input_crs="OGC:CRS84")
10496
return image

pygmt/datasets/earth_night.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55
The images are available in various resolutions.
66
"""
77

8-
import contextlib
98
from collections.abc import Sequence
109
from typing import Literal
1110

1211
import xarray as xr
1312
from pygmt.datasets.load_remote_dataset import _load_remote_dataset
1413

15-
with contextlib.suppress(ImportError):
16-
# rioxarray is needed to register the rio accessor
17-
import rioxarray # noqa: F401
18-
1914
__doctest_skip__ = ["load_black_marble"]
2015

2116

@@ -97,7 +92,4 @@ def load_black_marble(
9792
region=region,
9893
registration="pixel",
9994
)
100-
# If rioxarray is installed, set the coordinate reference system
101-
if hasattr(image, "rio"):
102-
image = image.rio.write_crs(input_crs="OGC:CRS84")
10395
return image

pygmt/datasets/load_remote_dataset.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Internal function to load GMT remote datasets.
33
"""
44

5+
import contextlib
56
from collections.abc import Sequence
67
from typing import Any, Literal, NamedTuple
78

@@ -11,6 +12,10 @@
1112
from pygmt.helpers import build_arg_list, kwargs_to_strings
1213
from pygmt.src import which
1314

15+
with contextlib.suppress(ImportError):
16+
# rioxarray is needed to register the rio accessor
17+
import rioxarray # noqa: F401
18+
1419

1520
class Resolution(NamedTuple):
1621
"""
@@ -48,13 +53,17 @@ class GMTRemoteDataset(NamedTuple):
4853
Dictionary of available resolution as keys and Resolution objects as values.
4954
extra_attributes
5055
A dictionary of extra or unique attributes of the dataset.
56+
crs
57+
The coordinate reference system of the raster image. Need to be set for images,
58+
and should be ``None`` for grids.
5159
"""
5260

5361
description: str
5462
kind: Literal["grid", "image"]
5563
units: str | None
5664
resolutions: dict[str, Resolution]
5765
extra_attributes: dict[str, Any]
66+
crs: str | None = None
5867

5968

6069
datasets = {
@@ -81,6 +90,7 @@ class GMTRemoteDataset(NamedTuple):
8190
description="NASA Day Images",
8291
kind="image",
8392
units=None,
93+
crs="OGC:CRS84",
8494
extra_attributes={"long_name": "blue_marble", "horizontal_datum": "WGS84"},
8595
resolutions={
8696
"01d": Resolution("01d", registrations=["pixel"]),
@@ -300,6 +310,7 @@ class GMTRemoteDataset(NamedTuple):
300310
description="NASA Night Images",
301311
kind="image",
302312
units=None,
313+
crs="OGC:CRS84",
303314
extra_attributes={"long_name": "black_marble", "horizontal_datum": "WGS84"},
304315
resolutions={
305316
"01d": Resolution("01d", registrations=["pixel"]),
@@ -598,4 +609,9 @@ def _load_remote_dataset(
598609
grid.attrs.pop("actual_range", None)
599610
for coord in grid.coords:
600611
grid[coord].attrs.pop("actual_range", None)
612+
613+
# For images, if rioxarray is installed, set the coordinate reference system.
614+
if dataset.crs is not None and hasattr(grid, "rio"):
615+
grid = grid.rio.write_crs(input_crs=dataset.crs)
616+
601617
return grid

0 commit comments

Comments
 (0)