Skip to content

Commit

Permalink
Refactor _load_remote_dataset to write CRS information for raster images
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Dec 9, 2024
1 parent 7768e93 commit 83cc91d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
8 changes: 0 additions & 8 deletions pygmt/datasets/earth_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
The images are available in various resolutions.
"""

import contextlib
from collections.abc import Sequence
from typing import Literal

import xarray as xr
from pygmt.datasets.load_remote_dataset import _load_remote_dataset

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

__doctest_skip__ = ["load_blue_marble"]


Expand Down Expand Up @@ -95,7 +90,4 @@ def load_blue_marble(
region=region,
registration="pixel",
)
# If rioxarray is installed, set the coordinate reference system
if hasattr(image, "rio"):
image = image.rio.write_crs(input_crs="OGC:CRS84")
return image
8 changes: 0 additions & 8 deletions pygmt/datasets/earth_night.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
The images are available in various resolutions.
"""

import contextlib
from collections.abc import Sequence
from typing import Literal

import xarray as xr
from pygmt.datasets.load_remote_dataset import _load_remote_dataset

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

__doctest_skip__ = ["load_black_marble"]


Expand Down Expand Up @@ -95,7 +90,4 @@ def load_black_marble(
region=region,
registration="pixel",
)
# If rioxarray is installed, set the coordinate reference system
if hasattr(image, "rio"):
image = image.rio.write_crs(input_crs="OGC:CRS84")
return image
15 changes: 15 additions & 0 deletions pygmt/datasets/load_remote_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Internal function to load GMT remote datasets.
"""

import contextlib
from collections.abc import Sequence
from typing import Any, Literal, NamedTuple

Expand All @@ -11,6 +12,10 @@
from pygmt.helpers import build_arg_list, kwargs_to_strings
from pygmt.src import which

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


class Resolution(NamedTuple):
"""
Expand Down Expand Up @@ -46,12 +51,15 @@ class GMTRemoteDataset(NamedTuple):
Dictionary of available resolution as keys and Resolution objects as values.
extra_attributes
A dictionary of extra or unique attributes of the dataset.
crs
The coordinate reference system of the raster image.
"""

description: str
units: str | None
resolutions: dict[str, Resolution]
extra_attributes: dict[str, Any]
crs: str | None = None


datasets = {
Expand All @@ -76,6 +84,7 @@ class GMTRemoteDataset(NamedTuple):
"earth_day": GMTRemoteDataset(
description="NASA Day Images",
units=None,
crs="OGC:CRS84",
extra_attributes={"long_name": "blue_marble", "horizontal_datum": "WGS84"},
resolutions={
"01d": Resolution("01d", registrations=["pixel"]),
Expand Down Expand Up @@ -212,6 +221,7 @@ class GMTRemoteDataset(NamedTuple):
"earth_night": GMTRemoteDataset(
description="NASA Night Images",
units=None,
crs="OGC:CRS84",
extra_attributes={"long_name": "black_marble", "horizontal_datum": "WGS84"},
resolutions={
"01d": Resolution("01d", registrations=["pixel"]),
Expand Down Expand Up @@ -469,4 +479,9 @@ def _load_remote_dataset(
grid.attrs.pop("actual_range", None)
for coord in grid.coords:
grid[coord].attrs.pop("actual_range", None)

# For images, if rioxarray is installed, set the coordinate reference system.
if dataset.crs is not None and hasattr(grid, "rio"):
grid = grid.rio.write_crs(input_crs=dataset.crs)

return grid

0 comments on commit 83cc91d

Please sign in to comment.