From a564331af1ab727dbaf8ea73db2f7720e2102528 Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Tue, 11 Jul 2023 22:31:53 +0200 Subject: [PATCH 1/2] add srs and forwards options --- CHANGES.md | 6 ++++++ morecantile/models.py | 21 +++++++++++++-------- tests/test_models.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index c9e0f3b..751d688 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,9 @@ + +## 4.3.0 (2023-07-11) + +* add `.srs` property to `CRSType` +* forward arguments to `pyproj.CRS` methods for `to_epsg()`, `to_wkt()`, `to_proj4()` and `to_json()` CRSType methods + ## 4.2.1 (2023-07-02) * limit pydantic requirement to `~=1.0`` diff --git a/morecantile/models.py b/morecantile/models.py index d08981d..2db0f94 100644 --- a/morecantile/models.py +++ b/morecantile/models.py @@ -110,25 +110,30 @@ def __init__(self, **data): self._pyproj_crs = CRS.from_user_input(data.get("__root__")) - def to_epsg(self) -> Optional[int]: + @property + def srs(self) -> Optional[int]: + """return the string form of the user input used to create the CRS.""" + return self._pyproj_crs.srs + + def to_epsg(self, *args: Any, **kwargs: Any) -> Optional[int]: """return EPSG number of the CRS.""" - return self._pyproj_crs.to_epsg() + return self._pyproj_crs.to_epsg(*args, **kwargs) - def to_wkt(self) -> str: + def to_wkt(self, *args: Any, **kwargs: Any) -> str: """return WKT version of the CRS.""" - return self._pyproj_crs.to_wkt() + return self._pyproj_crs.to_wkt(*args, **kwargs) - def to_proj4(self) -> str: + def to_proj4(self, *args: Any, **kwargs: Any) -> str: """return PROJ4 version of the CRS.""" - return self._pyproj_crs.to_proj4() + return self._pyproj_crs.to_proj4(*args, **kwargs) def to_dict(self) -> Dict: """return DICT version of the CRS.""" return self._pyproj_crs.to_dict() - def to_json(self) -> str: + def to_json(self, *args: Any, **kwargs: Any) -> str: """return JSON version of the CRS.""" - return self._pyproj_crs.to_json() + return self._pyproj_crs.to_json(*args, **kwargs) def CRS_to_uri(crs: CRS) -> str: diff --git a/tests/test_models.py b/tests/test_models.py index 29daa4c..87a78db 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -14,7 +14,7 @@ import morecantile from morecantile.commons import Tile from morecantile.errors import InvalidIdentifier -from morecantile.models import TileMatrix, TileMatrixSet +from morecantile.models import CRSType, TileMatrix, TileMatrixSet data_dir = os.path.join(os.path.dirname(__file__), "../morecantile/data") tilesets = [ @@ -520,3 +520,33 @@ def test_boundingbox(): ], } ) + + +def test_crs_type(): + """Test CRSType Model.""" + uri = "http://www.opengis.net/def/crs/EPSG/0/3857" + crs = CRSType(__root__=uri) + assert crs.__root__ == uri + + # PROJ methods + assert crs._pyproj_crs == CRS.from_epsg(3857) + assert crs.srs == "http://www.opengis.net/def/crs/EPSG/0/3857" + assert crs.to_epsg() == 3857 + assert crs.to_wkt() == CRS.from_epsg(3857).to_wkt() + assert crs.to_proj4() == CRS.from_epsg(3857).to_proj4() + assert crs.to_dict() == CRS.from_epsg(3857).to_dict() + assert crs.to_json() == CRS.from_epsg(3857).to_json() + + # with Options + assert crs.to_epsg(min_confidence=10) == 3857 + assert crs.to_wkt(pretty=True) == CRS.from_epsg(3857).to_wkt(pretty=True) + assert crs.to_proj4(5) == CRS.from_epsg(3857).to_proj4(5) + assert crs.to_json(pretty=True) == CRS.from_epsg(3857).to_json(pretty=True) + + wkt = CRS.from_epsg(3857).to_wkt() + crs = CRSType(__root__=wkt) + assert crs.__root__ == wkt + # PROJ methods + assert crs._pyproj_crs == CRS.from_epsg(3857) + assert crs.srs == wkt + assert crs.to_epsg() == 3857 From 8bc9072fd39b4593abb33427f0950eff3e083434 Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Tue, 11 Jul 2023 22:35:19 +0200 Subject: [PATCH 2/2] fix type --- morecantile/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/morecantile/models.py b/morecantile/models.py index 2db0f94..f8d5bd1 100644 --- a/morecantile/models.py +++ b/morecantile/models.py @@ -111,7 +111,7 @@ def __init__(self, **data): self._pyproj_crs = CRS.from_user_input(data.get("__root__")) @property - def srs(self) -> Optional[int]: + def srs(self) -> str: """return the string form of the user input used to create the CRS.""" return self._pyproj_crs.srs