From 06c62d88088ea56c8775d008e7a6bed33b89c08c Mon Sep 17 00:00:00 2001 From: "Dr. Andrew Annex" Date: Tue, 22 Oct 2024 12:58:34 -0700 Subject: [PATCH] Add CRS_to_urn (#752) * adds CRS_to_urn function * fix CRS expected type * switch to using typing's version of Tuple * format * make the crs info function private * update changelog --------- Co-authored-by: vincentsarago --- CHANGES.md | 3 +++ rio_tiler/utils.py | 24 +++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index b2a5ed08..62b32797 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +# 7.0.1 (2024-10-22) + +* Add `CRS_to_urn` method and update internals for `CRS_to_uri` (author @AndrewAnnex, https://github.com/cogeotiff/rio-tiler/pull/752) # 7.0.0 (2024-10-21) diff --git a/rio_tiler/utils.py b/rio_tiler/utils.py index 50c03d0c..098f0756 100644 --- a/rio_tiler/utils.py +++ b/rio_tiler/utils.py @@ -791,7 +791,7 @@ def cast_to_sequence(val: Optional[Any] = None) -> Sequence: return val -def CRS_to_uri(crs: CRS) -> Optional[str]: +def _CRS_authority_info(crs: CRS) -> Optional[Tuple[str, str, str]]: """Convert CRS to URI. Code adapted from https://github.com/developmentseed/morecantile/blob/1829fe12408e4a1feee7493308f3f02257ef4caf/morecantile/models.py#L148-L161 @@ -804,6 +804,28 @@ def CRS_to_uri(crs: CRS) -> Optional[str]: if "_" in authority: authority, version = authority.split("_") + return authority, version, code + + return None + + +def CRS_to_uri(crs: CRS) -> Optional[str]: + """Convert CRS to URI.""" + if info := _CRS_authority_info(crs): + authority, version, code = info + return f"http://www.opengis.net/def/crs/{authority}/{version}/{code}" return None + + +def CRS_to_urn(crs: CRS) -> Optional[str]: + """Convert CRS to URN.""" + if info := _CRS_authority_info(crs): + authority, version, code = info + if version == "0": + version = "" + + return f"urn:ogc:def:crs:{authority}:{version}:{code}" + + return None