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

Refactor _load_remote_dataset to write CRS information for raster images #3678

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -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
Loading