mln-rs-engine 0.0.22

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
use approx::assert_abs_diff_eq;
use mln_style::constants::{LATITUDE_MAX, LONGITUDE_MAX, TILE_SIZE_D};
use mln_style::geo::LatLng;
use mln_style::geometry::Point;
use mln_style::size::Size;
use mln_style::tile_id::CanonicalTileID;

use super::TileCoordinate;
use crate::camera::ConstrainMode;
use crate::camera::transform::Transform;
use crate::geo::ScreenCoordinate;
use crate::tile::UnwrappedTileID;

/// A position's tile coordinate at a zoom is its place in the world square
/// times the tiles across that zoom, where a view of the whole world at
/// zoom 0 shows it, and where its tile coordinate at zoom 0 zooms to.
#[test]
fn a_tile_coordinate_is_the_place_in_the_world_square_times_the_tiles_across() {
    // Positions, where a 512-pixel view of zoom 0 shows them (y up), and
    // their places in the world square (y down).
    let cases = [
        (0.0, 0.0, (256.0, 256.0), (0.5, 0.5)),
        (LATITUDE_MAX, -LONGITUDE_MAX, (0.0, 512.0), (0.0, 0.0)),
        (-LATITUDE_MAX, -LONGITUDE_MAX, (0.0, 0.0), (0.0, 1.0)),
        (-LATITUDE_MAX, LONGITUDE_MAX, (512.0, 0.0), (1.0, 1.0)),
        (LATITUDE_MAX, LONGITUDE_MAX, (512.0, 512.0), (1.0, 0.0)),
    ];
    let mut transform = Transform::new(ConstrainMode::HeightOnly);
    transform.resize(Size::new(TILE_SIZE_D as u32, TILE_SIZE_D as u32)).unwrap();

    for (lat, lng, (screen_x, screen_y), (x, y)) in cases {
        let position = LatLng::new(lat, lng).unwrap();
        let at_zoom_0 = TileCoordinate::from_lat_lng(0.0, &position);
        for zoom in [0, 1, 8, 16] {
            let tiles = f64::from(1u32 << zoom);
            let context = format!("{lat}, {lng} at zoom {zoom}");
            let coordinate = TileCoordinate::from_lat_lng(f64::from(zoom), &position);
            assert_eq!(coordinate.z, f64::from(zoom), "{context}");
            assert_abs_diff_eq!(coordinate.p.x, x * tiles, epsilon = 1e-9);
            assert_abs_diff_eq!(coordinate.p.y, y * tiles, epsilon = 1e-7);

            let zoomed = at_zoom_0.zoom_to(f64::from(zoom));
            assert_eq!(zoomed, coordinate, "{context}");

            let screen = ScreenCoordinate::new(screen_x, screen_y);
            let shown = TileCoordinate::from_screen_coordinate(transform.view(), zoom, &screen);
            assert_eq!(shown.z, coordinate.z, "{context}");
            assert_abs_diff_eq!(shown.p.x, coordinate.p.x, epsilon = 0.99);
            assert_abs_diff_eq!(shown.p.y, coordinate.p.y, epsilon = 0.99);
        }
    }
}

/// A place in the world square, in a tile's units from the tile's top left
/// corner, whatever world copy the tile is in; a place far off the tile
/// saturates.
#[test]
fn a_geometry_coordinate_is_the_place_in_the_tiles_units() {
    let max = i16::MAX;
    let cases = [
        (0, 0, 0, 0, (0.5, 0.5), (4096, 4096)),
        (0, 0, 0, 0, (0.0, 0.0), (0, 0)),
        (1, 1, 1, 0, (1.0, 1.0), (8192, 8192)),
        (2, 1, 2, 0, (0.375, 0.5), (4096, 0)),
        (16, 65535, 0, 0, (1.0, 0.0), (8192, 0)),
        (0, 0, 0, 1, (1.5, 0.25), (4096, 2048)),
        (2, 3, 0, -1, (-0.125, 0.0), (4096, 0)),
        (0, 0, 0, 0, (10.0, -10.0), (max, i16::MIN)),
    ];
    for (z, x, y, wrap, (world_x, world_y), (tile_x, tile_y)) in cases {
        let tile = UnwrappedTileID::with_wrap(wrap, CanonicalTileID::new(z, x, y));
        let point = TileCoordinate::to_geometry_coordinate(&tile, &Point::new(world_x, world_y));
        assert_eq!((point.x, point.y), (tile_x, tile_y), "{world_x}, {world_y} in {z}/{x}/{y} of wrap {wrap}");
    }
}