Skip to content

Commit

Permalink
Merge pull request #130 from developmentseed/addSRStoCRSType
Browse files Browse the repository at this point in the history
add srs and forwards options
  • Loading branch information
vincentsarago authored Jul 11, 2023
2 parents 7ebad59 + 8bc9072 commit 1d66604
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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``
Expand Down
21 changes: 13 additions & 8 deletions morecantile/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> str:
"""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:
Expand Down
32 changes: 31 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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

0 comments on commit 1d66604

Please sign in to comment.