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

Inject custom serializer/deserializer for rasterio.crs.CRS. #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions rslearn/tile_stores/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from upath import UPath

from rslearn.config import LayerConfig
from rslearn.utils.jsonargparse import init_jsonargparse

from .default import DefaultTileStore
from .tile_store import TileStore, TileStoreWithLayer
Expand All @@ -32,6 +33,7 @@ def load_tile_store(config: dict[str, Any], ds_path: UPath) -> TileStore:
tile_store.set_dataset_path(ds_path)
return tile_store

init_jsonargparse()
parser = jsonargparse.ArgumentParser()
parser.add_argument("--tile_store", type=TileStore)
cfg = parser.parse_object({"tile_store": config})
Expand Down
33 changes: 33 additions & 0 deletions rslearn/utils/jsonargparse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Custom serialization for jsonargparse."""

import jsonargparse
from rasterio.crs import CRS


def crs_serializer(v: CRS) -> str:
"""Serialize CRS for jsonargparse.

Args:
v: the CRS object.

Returns:
the CRS encoded to string
"""
return v.to_string()


def crs_deserializer(v: str) -> CRS:
"""Deserialize CRS for jsonargparse.

Args:
v: the encoded CRS.

Returns:
the decoded CRS object
"""
return CRS.from_string(v)


def init_jsonargparse() -> None:
"""Initialize custom jsonargparse serializers."""
jsonargparse.typing.register_type(CRS, crs_serializer, crs_deserializer)
Loading