mln-rs-engine 0.0.23

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
use mln_style::crs::{WorldBox, WorldMapping};
use mln_style::geo::{LatLng, LatLngBounds};
use mln_style::geometry::Point;
use mln_style::tile_id::CanonicalTileID;

use super::TileRange;

fn ll(lat: f64, lon: f64) -> LatLng {
    LatLng::new(lat, lon).unwrap()
}

fn c(z: u8, x: u32, y: u32) -> CanonicalTileID {
    CanonicalTileID::new(z, x, y)
}

/// The bounds from the corner `(south, west)` to `(north, east)`.
fn hull((south, west): (f64, f64), (north, east): (f64, f64)) -> LatLngBounds {
    LatLngBounds::hull(ll(south, west), ll(north, east))
}

/// Every tile of row `y` at zoom `z`.
fn row(z: u8, y: u32) -> Vec<CanonicalTileID> {
    (0..1 << z).map(|x| c(z, x, y)).collect()
}

/// Every tile of zoom `z`.
fn level(z: u8) -> Vec<CanonicalTileID> {
    (0..1 << z).flat_map(|y| row(z, y)).collect()
}

#[track_caller]
fn check(what: &str, range: &TileRange, inside: &[CanonicalTileID], outside: &[CanonicalTileID]) {
    for tile in inside {
        assert!(range.contains(tile), "{what}: {tile:?} is inside");
    }
    for tile in outside {
        assert!(!range.contains(tile), "{what}: {tile:?} is outside");
    }
}

#[test]
fn a_range_of_bounds_holds_the_tiles_they_reach() {
    let at = TileRange::from_lat_lng_bounds;
    let san_francisco = hull((37.6609, -122.5744), (37.8271, -122.3204));
    let across_the_antimeridian = hull((-20.9615, -214.309), (19.477, -155.830));
    let just_under_180 = f64::from_bits(180.0f64.to_bits() - 1);
    // A whole number of worlds east of the prime meridian: 180 more degrees
    // round away at this magnitude.
    let huge = 360.0 * 2.0f64.powf(52.0);
    let tile = |id: CanonicalTileID| LatLngBounds::from_canonical_tile(id.z, id.x, id.y);
    // (what, range, tiles inside, tiles outside)
    let cases = [
        ("the world", at(&LatLngBounds::world(), 0), vec![c(0, 0, 0)], vec![c(10, 0, 0)]),
        ("a tile's bounds", at(&tile(c(3, 7, 0)), 3), vec![c(3, 7, 0)], vec![]),
        ("a tile's bounds", at(&tile(c(10, 162, 395)), 10), vec![c(10, 162, 395)], vec![]),
        (
            "a city, from zoom 5 to 13",
            TileRange::from_lat_lng_bounds_range(&san_francisco, 5, 13),
            vec![c(5, 5, 12), c(6, 10, 24), c(13, 1310, 3166)],
            vec![c(0, 0, 0), c(5, 3, 11), c(6, 9, 22)],
        ),
        ("a city", at(&san_francisco, 13), vec![c(13, 1310, 3166)], vec![c(13, 1316, 3100)]),
        (
            "a city a world east",
            at(&hull((37.6609, 237.6796), (37.8271, 237.4256)), 13),
            vec![c(13, 1310, 3166)],
            vec![c(13, 1316, 3100)],
        ),
        ("across the antimeridian", at(&across_the_antimeridian, 1), vec![c(1, 1, 1), c(1, 0, 0)], vec![]),
        (
            "across the antimeridian",
            at(&across_the_antimeridian, 6),
            vec![c(6, 63, 28)],
            vec![c(6, 55, 34), c(6, 5, 28)],
        ),
        ("the world from the prime meridian", at(&hull((-90.0, 0.0), (90.0, 360.0)), 2), row(2, 1), vec![]),
        ("wider than the world", at(&hull((10.0, 10.0), (20.0, 375.0)), 2), row(2, 1), vec![]),
        // Less than a world wide, but from column 2 to column 2 a world east.
        ("reaching every column", at(&hull((10.0, 10.0), (20.0, 365.0)), 2), row(2, 1), vec![]),
        (
            "a world west",
            at(&hull((10.0, -360.0), (20.0, -350.0)), 2),
            vec![c(2, 2, 1)],
            vec![c(2, 0, 1), c(2, 1, 1), c(2, 3, 1)],
        ),
        // Both edges are finite, but the width isn't.
        ("a width that overflows", at(&hull((10.0, -1e308), (20.0, 1e308)), 2), row(2, 1), vec![]),
        ("a huge longitude", at(&hull((10.0, huge), (20.0, huge)), 3), vec![c(3, 4, 3)], vec![c(3, 5, 3)]),
        // Empty bounds are inverted: their east edge is west of their west edge.
        ("empty bounds", at(&LatLngBounds::empty(), 2), vec![], level(2)),
        ("ending at 180°", at(&hull((-10.0, 170.0), (10.0, 180.0)), 2), vec![c(2, 3, 1)], vec![c(2, 0, 1)]),
        ("ending at -180°", at(&hull((-10.0, -190.0), (10.0, -180.0)), 2), vec![c(2, 3, 1)], vec![c(2, 0, 1)]),
        // A west edge just under 180° projects to 180°, the column past the last.
        (
            "starting just under 180°",
            at(&hull((-10.0, just_under_180), (10.0, 180.0)), 2),
            vec![c(2, 3, 1)],
            vec![c(2, 0, 1), c(2, 1, 1), c(2, 2, 1)],
        ),
        (
            "starting just under 180°, from zoom 1",
            TileRange::from_lat_lng_bounds_range(&hull((-10.0, just_under_180), (10.0, 180.0)), 1, 2),
            vec![c(1, 1, 0)],
            vec![c(1, 0, 0)],
        ),
    ];
    for (what, range, inside, outside) in &cases {
        check(what, range, inside, outside);
    }
}

/// A CRS turned 30 degrees, whose world x and y both turn with longitude
/// and latitude.
fn rotated() -> WorldMapping {
    let (sin, cos) = (std::f64::consts::PI / 6.0).sin_cos();
    crate::tests::crs::mapping(crate::tests::crs::crs(
        "test:rotated",
        move |[lng, lat]| [lng * cos - lat * sin, lng * sin + lat * cos],
        move |[x, y]| [x * cos + y * sin, -x * sin + y * cos],
        [-150.0, 150.0],
        300.0,
    ))
}

/// The tile at zoom `z` of the place of `lat_lng` in `world`.
fn tile_of(world: &WorldMapping, lat_lng: LatLng, z: u8) -> CanonicalTileID {
    let point = world.world_from_lat_lng(&lat_lng);
    let tiles = 2.0f64.powi(i32::from(z));
    c(z, (point.x * tiles).floor() as u32, (point.y * tiles).floor() as u32)
}

/// The tiles at zoom `z` of the rectangle `bounds` makes in the world
/// square of `world`.
fn range_in(world: &WorldMapping, bounds: &LatLngBounds, z: u8) -> TileRange {
    TileRange::from_world_box(&world.lng_lat_box_to_world_box(bounds), z, z)
}

fn world_box(min_x: f64, min_y: f64, max_x: f64, max_y: f64) -> WorldBox {
    WorldBox { min: Point::new(min_x, min_y), max: Point::new(max_x, max_y) }
}

#[test]
fn a_range_of_a_world_box_holds_the_tiles_it_covers_part_of() {
    let polar = crate::tests::crs::mapping(crate::tests::crs::polar_stereographic("test:polar"));
    let rotated = rotated();
    let turned = hull((40.0, 100.0), (50.0, 110.0));
    let rotated_tile = tile_of(&rotated, ll(45.0, 105.0), 3);
    let mercator_tile = tile_of(&WorldMapping::default(), ll(45.0, 105.0), 3);
    assert_ne!(rotated_tile.x, mercator_tile.x, "the CRS moves the place to another column");
    let square = hull((-10.0, -10.0), (10.0, 10.0));
    let corners = [(-10.0, -10.0), (-10.0, 10.0), (10.0, 10.0), (10.0, -10.0)];
    // (what, range, tiles inside, tiles outside)
    let cases = [
        // Longitudes 0 to 90 and latitudes 45 to 90 of the identity.
        (
            "the top row of the north-east quarter",
            TileRange::from_world_box(&world_box(0.5, 0.0, 1.0, 0.25), 0, 2),
            vec![c(2, 2, 0), c(2, 3, 0), c(1, 1, 0), c(0, 0, 0)],
            vec![c(2, 2, 1), c(2, 1, 0), c(1, 0, 0)],
        ),
        (
            "a box outside the square",
            TileRange::from_world_box(&world_box(1.5, 0.0, 2.0, 1.0), 0, 2),
            vec![],
            vec![c(0, 0, 0), c(2, 3, 0)],
        ),
        // On the edge between columns 1 and 2, it lies in column 2.
        (
            "a box as thin as a line",
            TileRange::from_world_box(&world_box(0.5, 0.25, 0.5, 0.75), 2, 2),
            vec![c(2, 2, 1), c(2, 2, 2)],
            vec![c(2, 1, 1), c(2, 1, 2), c(2, 0, 1), c(2, 0, 2)],
        ),
        ("the polar cap north of 60°", range_in(&polar, &hull((60.0, -180.0), (90.0, 180.0)), 2), level(2), vec![]),
        // 45° E is the rightmost point of the 60° N circle, between two of the
        // box's longitude samples.
        (
            "a polar cap that no sample reaches the edge of",
            range_in(&polar, &hull((60.0, -170.0), (90.0, 170.0)), 13),
            vec![tile_of(&polar, ll(60.0, 45.0), 13)],
            vec![],
        ),
        ("a box in a turned CRS", range_in(&rotated, &turned, 3), vec![rotated_tile], vec![mercator_tile]),
        (
            "the same bounds in Mercator",
            TileRange::from_lat_lng_bounds(&turned, 3),
            vec![mercator_tile],
            vec![rotated_tile],
        ),
        (
            "the corners of a box that a CRS turns",
            range_in(&rotated, &square, 5),
            corners.map(|(lat, lng)| tile_of(&rotated, ll(lat, lng), 5)).to_vec(),
            vec![],
        ),
    ];
    for (what, range, inside, outside) in &cases {
        check(what, range, inside, outside);
    }
}