from __future__ import annotations import pytest from dpg_map.projection import ( WEB_MERCATOR_MAX_LAT, clamp_latitude, latlon_to_tile, latlon_to_world, screen_to_world, world_to_latlon, world_to_screen, ) @pytest.mark.parametrize( ("lat", "lon", "zoom"), [ (0.0, 0.0, 0), (47.9029, 1.9093, 15), (-33.8688, 151.2093, 10), (WEB_MERCATOR_MAX_LAT, 179.999, 4), ], ) def test_projection_roundtrip(lat: float, lon: float, zoom: int) -> None: x, y = latlon_to_world(lat, lon, zoom) roundtrip_lat, roundtrip_lon = world_to_latlon(x, y, zoom) assert roundtrip_lat == pytest.approx(clamp_latitude(lat), abs=1e-7) assert roundtrip_lon == pytest.approx(lon, abs=1e-7) def test_latlon_to_tile_returns_xyz_coordinate() -> None: assert latlon_to_tile(0.0, 0.0, 1) == (1, 1, 1) def test_world_screen_roundtrip() -> None: center = (47.9029, 1.9093) world = latlon_to_world(47.91, 1.92, 14) screen = world_to_screen(*world, center=center, zoom=14, width=800, height=600) assert screen_to_world(*screen, center=center, zoom=14, width=800, height=600) == pytest.approx( world )