mln-rs-style 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
//! Planar coordinate reference systems that a map can draw in: an app
//! registers each with its registry, and a style's `projection.type` names
//! one. A [`WorldMapping`] maps lng/lat to and from the world square that a
//! map draws in: Web Mercator's, or a registered projection's.

mod mapping;
mod world;

use std::f64::consts::PI;
use std::fmt;

pub use mapping::{BoxSamples, WorldBox};
pub use world::{MERCATOR, WorldMapping};

use crate::constants::EARTH_RADIUS_M;
use crate::projection::IDENTITY_PROJECTION_NAME;

/// A position that a mapping or a converter has no answer for.
const NO_ANSWER: [f64; 2] = [f64::NAN, f64::NAN];

/// Whether `position` is an answer: both of its coordinates finite.
fn is_answer(position: &[f64; 2]) -> bool {
    position[0].is_finite() && position[1].is_finite()
}

/// Converts positions between lng/lat and a coordinate reference system's
/// coordinates, a batch at a time. A position that has no answer comes out
/// NaN. The map calls it from any thread, several at once.
pub trait CrsConverter: Send + Sync {
    /// Each lng/lat in degrees of `lng_lats` to the CRS's coordinates, into
    /// `positions`, which is as long.
    fn forward(&self, lng_lats: &[[f64; 2]], positions: &mut [[f64; 2]]);

    /// Each position of the CRS in `positions` to lng/lat in degrees, into
    /// `lng_lats`, which is as long.
    fn inverse(&self, positions: &[[f64; 2]], lng_lats: &mut [[f64; 2]]);
}

/// The square, power-of-two quad tile grid over a coordinate reference
/// system's plane, what the OGC Two Dimensional Tile Matrix Set standard
/// calls a quad tree tile matrix set: tile 0/0/0 is the square of side
/// `extent_at_zoom0` whose top-left corner is `origin`, and each zoom level
/// splits every tile into four.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TileMatrixSet {
    /// The CRS coordinates of the top-left corner of tile 0/0/0, its least x
    /// and greatest y, in the order that the converter's `forward` gives
    /// them.
    pub origin: [f64; 2],
    /// The side of tile 0/0/0, in the CRS's units.
    pub extent_at_zoom0: f64,
}

impl TileMatrixSet {
    /// Web Mercator's, the grid of the `mercator`, `globe` and
    /// `vertical-perspective` projections, in EPSG:3857 meters: tile 0/0/0
    /// spans half the equator each way from the origin of the plane.
    pub const WEB_MERCATOR: TileMatrixSet = TileMatrixSet {
        origin: [-PI * EARTH_RADIUS_M, PI * EARTH_RADIUS_M],
        extent_at_zoom0: 2.0 * PI * EARTH_RADIUS_M,
    };
}

/// A planar coordinate reference system and the tile grid over it, which a
/// map draws in when its style's `projection.type` names it. Every tile
/// source of the map is taken to be cut in that grid, and its tiles draw as
/// they are. The CRS's units count as meters wherever the map converts
/// meters.
pub struct Crs {
    /// The name that `projection.type` gives, such as `EPSG:2193`. An app's
    /// projections have a colon in their names: names without one are kept
    /// for built-in projections.
    pub name: String,
    pub converter: Box<dyn CrsConverter>,
    pub tile_matrix_set: TileMatrixSet,
}

impl Crs {
    /// The built-in `identity` projection, for maps of a plane, such as a
    /// floor plan or a scanned image: its coordinates are lng/lat degrees
    /// unchanged, and tile 0/0/0 spans -90 to 90 on both axes.
    #[must_use]
    pub fn identity() -> Crs {
        Crs {
            name: IDENTITY_PROJECTION_NAME.to_owned(),
            converter: Box::new(Identity),
            tile_matrix_set: TileMatrixSet { origin: [-90.0, 90.0], extent_at_zoom0: 180.0 },
        }
    }
}

impl fmt::Debug for Crs {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Crs")
            .field("name", &self.name)
            .field("tile_matrix_set", &self.tile_matrix_set)
            .finish_non_exhaustive()
    }
}

/// The converter of [`Crs::identity`], whose coordinates are lng/lat.
struct Identity;

impl CrsConverter for Identity {
    fn forward(&self, lng_lats: &[[f64; 2]], positions: &mut [[f64; 2]]) {
        for (position, lng_lat) in positions.iter_mut().zip(lng_lats) {
            *position = *lng_lat;
        }
    }

    fn inverse(&self, positions: &[[f64; 2]], lng_lats: &mut [[f64; 2]]) {
        self.forward(positions, lng_lats);
    }
}