Skip to content

Refactor _load_remote_dataset to write CRS information for raster images #3678

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

Merged
merged 3 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -98,7 +93,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 @@ -97,7 +92,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
16 changes: 16 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 @@ -48,13 +53,17 @@
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. Need to be set for images,
and should be ``None`` for grids.
"""

description: str
kind: Literal["grid", "image"]
units: str | None
resolutions: dict[str, Resolution]
extra_attributes: dict[str, Any]
crs: str | None = None


datasets = {
Expand All @@ -81,6 +90,7 @@
description="NASA Day Images",
kind="image",
units=None,
crs="OGC:CRS84",
extra_attributes={"long_name": "blue_marble", "horizontal_datum": "WGS84"},
resolutions={
"01d": Resolution("01d", registrations=["pixel"]),
Expand Down Expand Up @@ -300,6 +310,7 @@
description="NASA Night Images",
kind="image",
units=None,
crs="OGC:CRS84",
extra_attributes={"long_name": "black_marble", "horizontal_datum": "WGS84"},
resolutions={
"01d": Resolution("01d", registrations=["pixel"]),
Expand Down Expand Up @@ -598,4 +609,9 @@
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)

Check warning on line 615 in pygmt/datasets/load_remote_dataset.py

View check run for this annotation

Codecov / codecov/patch

pygmt/datasets/load_remote_dataset.py#L615

Added line #L615 was not covered by tests

return grid
Loading