Skip to content

Commit 02cefc8

Browse files
committed
posible fix for always assuming WGS84
1 parent 1829fe1 commit 02cefc8

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,6 @@ dmypy.json
130130

131131
# PyCharm:
132132
.idea
133+
134+
# vscode:
135+
.vscode

morecantile/models.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
NumType = Union[float, int]
4141
BoundsType = Tuple[NumType, NumType]
4242
LL_EPSILON = 1e-11
43-
WGS84_CRS = pyproj.CRS.from_epsg(4326)
4443
axesInfo = Annotated[List[str], Field(min_length=2, max_length=2)]
4544

4645

@@ -486,16 +485,18 @@ class TileMatrixSet(BaseModel, arbitrary_types_allowed=True):
486485
]
487486

488487
# Private attributes
489-
_geographic_crs: pyproj.CRS = PrivateAttr(default=WGS84_CRS)
488+
_geographic_crs: pyproj.CRS = PrivateAttr()
490489
_to_geographic: pyproj.Transformer = PrivateAttr()
491490
_from_geographic: pyproj.Transformer = PrivateAttr()
492491

493492
def __init__(self, **data):
494493
"""Set private attributes."""
495494
super().__init__(**data)
496495

496+
# TODO is the geodetic_crs always the geographic crs?
497+
# maybe instead switch depending on if it's a projection or a geodcrs/geogcrs
497498
self._geographic_crs = pyproj.CRS.from_user_input(
498-
data.get("_geographic_crs", WGS84_CRS)
499+
data.get("_geographic_crs", self.crs._pyproj_crs.geodetic_crs)
499500
)
500501

501502
try:
@@ -656,7 +657,7 @@ def custom(
656657
title: Optional[str] = None,
657658
id: Optional[str] = None,
658659
ordered_axes: Optional[List[str]] = None,
659-
geographic_crs: pyproj.CRS = WGS84_CRS,
660+
geographic_crs: pyproj.CRS = None,
660661
screen_pixel_size: float = 0.28e-3,
661662
decimation_base: int = 2,
662663
**kwargs: Any,
@@ -689,8 +690,8 @@ def custom(
689690
Tile Matrix Set title
690691
id: str, optional
691692
Tile Matrix Set identifier
692-
geographic_crs: pyproj.CRS
693-
Geographic (lat,lon) coordinate reference system (default is EPSG:4326)
693+
geographic_crs: pyproj.CRS, optional
694+
Geographic (lat,lon) coordinate reference system (default is CRS's geodetic CRS)
694695
ordered_axes: list of str, optional
695696
Override Axis order (e.g `["N", "S"]`) else default to CRS's metadata
696697
screen_pixel_size: float, optional
@@ -705,6 +706,9 @@ def custom(
705706
TileMatrixSet
706707
707708
"""
709+
# TODO is this correct to always assume?
710+
geographic_crs = geographic_crs or crs.geodetic_crs
711+
708712
matrix_scale = matrix_scale or [1, 1]
709713

710714
if ordered_axes:

tests/test_models.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from collections.abc import Iterable
77

88
import pyproj
9+
import pyproj.crs
910
import pytest
1011
from pydantic import ValidationError
1112
from rasterio.crs import CRS as rioCRS
@@ -288,6 +289,19 @@ def test_zoom_for_res():
288289
assert tms.zoom_for_res(10) == 14
289290
assert tms.zoom_for_res(5000) == 6
290291

292+
def test_custom_not_earth():
293+
crs = pyproj.CRS.from_user_input('IAU_2015:49900')
294+
extent = [-90, -180, 90, 180]
295+
mars_tms = morecantile.TileMatrixSet.custom(extent, crs, id="MarsGeographicCRS")
296+
assert '4326' not in mars_tms.geographic_crs.to_wkt()
297+
assert '4326' not in mars_tms.rasterio_geographic_crs.to_wkt()
298+
wkt_mars_web_mercator = "PROJCRS[\"Mars (2015) - Sphere XY / Pseudo-Mercator\",BASEGEOGCRS[\"Mars (2015) - Sphere\",DATUM[\"Mars (2015) - Sphere\",ELLIPSOID[\"Mars (2015) - Sphere\",3396190,0,LENGTHUNIT[\"metre\",1,ID[\"EPSG\",9001]]],ANCHOR[\"Viking 1 lander : 47.95137 W\"]],PRIMEM[\"Reference Meridian\",0,ANGLEUNIT[\"degree\",0.0174532925199433,ID[\"EPSG\",9122]]]],CONVERSION[\"Popular Visualisation Pseudo-Mercator\",METHOD[\"Popular Visualisation Pseudo Mercator\",ID[\"EPSG\",1024]],PARAMETER[\"Latitude of natural origin\",0,ANGLEUNIT[\"degree\",0.0174532925199433],ID[\"EPSG\",8801]],PARAMETER[\"Longitude of natural origin\",0,ANGLEUNIT[\"degree\",0.0174532925199433],ID[\"EPSG\",8802]],PARAMETER[\"False easting\",0,LENGTHUNIT[\"metre\",1],ID[\"EPSG\",8806]],PARAMETER[\"False northing\",0,LENGTHUNIT[\"metre\",1],ID[\"EPSG\",8807]]],CS[Cartesian,2],AXIS[\"easting (X)\",east,ORDER[1],LENGTHUNIT[\"metre\",1,ID[\"EPSG\",9001]]],AXIS[\"northing (Y)\",north,ORDER[2],LENGTHUNIT[\"metre\",1,ID[\"EPSG\",9001]]],USAGE[SCOPE[\"Web mapping and visualisation.\"],AREA[\"World between 85.06 S and 85.06 N.\"],BBOX[-85.850511287,-180,85.0511287,180]],REMARK[\"Use semi-major radius as sphere radius for interoperability. Source of IAU Coordinate systems: doi:10.1007/s10569-017-9805-5\"]]"
299+
crs_mars_web_mercator = pyproj.CRS.from_wkt(wkt_mars_web_mercator)
300+
extent_wm = [-85.850511287,-180,85.0511287,180]
301+
mars_tms_wm = morecantile.TileMatrixSet.custom(extent_wm, crs_mars_web_mercator, id="MarsWebMercator")
302+
assert '4326' not in mars_tms_wm.geographic_crs.to_wkt()
303+
assert '4326' not in mars_tms_wm.rasterio_geographic_crs.to_wkt()
304+
291305

292306
def test_schema():
293307
"""Translate Model to Schema."""
@@ -300,13 +314,15 @@ def test_schema():
300314
"+proj=stere +lat_0=90 +lon_0=0 +k=2 +x_0=0 +y_0=0 +R=3396190 +units=m +no_defs"
301315
)
302316
extent = [-13584760.000, -13585240.000, 13585240.000, 13584760.000]
303-
with pytest.warns(UserWarning):
304-
tms = morecantile.TileMatrixSet.custom(extent, crs, id="MarsNPolek2MOLA5k")
317+
tms = morecantile.TileMatrixSet.custom(extent, crs, id="MarsNPolek2MOLA5k")
305318
assert tms.model_json_schema()
306319
assert tms.model_dump(exclude_none=True)
307320
json_doc = json.loads(tms.model_dump_json(exclude_none=True))
308321
assert json_doc["crs"] == "http://www.opengis.net/def/crs/IAU/2015/49930"
309-
322+
# test to ensure a correct geographic crs
323+
mars_tms = TileMatrixSet.model_validate(json_doc)
324+
assert '4326' not in mars_tms.geographic_crs.to_wkt()
325+
assert '4326' not in mars_tms.rasterio_geographic_crs.to_wkt()
310326
crs = pyproj.CRS.from_epsg(3031)
311327
extent = [-948.75, -543592.47, 5817.41, -3333128.95] # From https:///epsg.io/3031
312328
tms = morecantile.TileMatrixSet.custom(extent, crs, id="MyCustomTmsEPSG3031")

0 commit comments

Comments
 (0)