mln-rs-engine 0.0.19

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
use std::f64::consts::PI;

use geometry::Point;
use mln_style::constants::{DEGREES_MAX, LATITUDE_MAX, LONGITUDE_MAX, TILE_SIZE_D};
use mln_style::error::Error;
use mln_style::geo::{LatLng, WrapMode};
use mln_style::math::{clamp_f64, deg2rad, rad2deg};

use crate::camera::constants::{MAX_ZOOM, MIN_ZOOM};
use crate::geo::constants::{EARTH_RADIUS_M, M2PI};

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ProjectedMeters {
    /// Distance measured northwards.
    northing: f64,
    /// Distance measured eastwards.
    easting: f64,
}

// The derived PartialEq is the C++ operator==:
// `a._northing == b._northing && a._easting == b._easting`.

impl ProjectedMeters {
    /// Fails with [`ErrorKind::InvalidArgument`](mln_style::error::ErrorKind) for a NaN
    /// northing or easting.
    pub fn new(n: f64, e: f64) -> Result<Self, Error> {
        if n.is_nan() {
            return Err(Error::invalid_argument("northing must not be NaN"));
        }
        if e.is_nan() {
            return Err(Error::invalid_argument("easting must not be NaN"));
        }
        Ok(ProjectedMeters { northing: n, easting: e })
    }

    #[must_use]
    pub fn northing(&self) -> f64 {
        self.northing
    }

    #[must_use]
    pub fn easting(&self) -> f64 {
        self.easting
    }
}

/// Spherical Mercator projection
/// <https://github.com/openlayers/docs/blob/c7ab69156e17bedc1059d64d7a864b7868e354c1/library/spherical_mercator.rst>
pub struct Projection;

impl Projection {
    /// Map pixel width at given scale.
    #[must_use]
    pub(crate) fn world_size(scale: f64) -> f64 {
        scale * TILE_SIZE_D
    }

    #[must_use]
    pub fn meters_per_pixel_at_latitude(lat: f64, zoom: f64) -> f64 {
        let constrained_zoom = clamp_f64(zoom, MIN_ZOOM, MAX_ZOOM);
        let constrained_scale = 2.0f64.powf(constrained_zoom);
        let constrained_latitude = clamp_f64(lat, -LATITUDE_MAX, LATITUDE_MAX);
        deg2rad(constrained_latitude).cos() * M2PI * EARTH_RADIUS_M / Self::world_size(constrained_scale)
    }

    #[must_use]
    pub fn projected_meters_for_lat_lng(lat_lng: &LatLng) -> ProjectedMeters {
        let constrained_latitude = clamp_f64(lat_lng.latitude(), -LATITUDE_MAX, LATITUDE_MAX);
        let constrained_longitude = clamp_f64(lat_lng.longitude(), -LONGITUDE_MAX, LONGITUDE_MAX);

        let m = 1.0 - 1e-15;
        let f = clamp_f64(deg2rad(constrained_latitude).sin(), -m, m);

        let easting = deg2rad(EARTH_RADIUS_M * constrained_longitude);
        let northing = 0.5 * EARTH_RADIUS_M * ((1.0 + f) / (1.0 - f)).ln();

        // A valid LatLng, clamped, gives values that aren't NaN.
        ProjectedMeters { northing, easting }
    }

    /// The clamps keep both values in range, a NaN clamping to the upper
    /// bound.
    #[must_use]
    pub fn lat_lng_for_projected_meters(projected_meters: &ProjectedMeters) -> LatLng {
        let mut latitude = rad2deg(2.0 * (projected_meters.northing() / EARTH_RADIUS_M).exp().atan() - (PI / 2.0));
        let mut longitude = rad2deg(projected_meters.easting()) / EARTH_RADIUS_M;

        latitude = clamp_f64(latitude, -LATITUDE_MAX, LATITUDE_MAX);
        longitude = clamp_f64(longitude, -LONGITUDE_MAX, LONGITUDE_MAX);

        LatLng::saturating(latitude, longitude, WrapMode::Unwrapped)
    }

    #[must_use]
    pub fn project(lat_lng: &LatLng, scale: f64) -> Point<f64> {
        Self::project_(lat_lng, Self::world_size(scale))
    }

    /// The point of `lat_lng` on a world `2^zoom` tiles wide.
    #[must_use]
    pub(crate) fn project_zoom(lat_lng: &LatLng, zoom: i32) -> Point<f64> {
        Self::project_(lat_lng, 2f64.powi(zoom))
    }

    /// Fails with the [`LatLng::new`] error for a NaN or infinite result.
    pub fn unproject(p: &Point<f64>, scale: f64, wrap_mode: WrapMode) -> Result<LatLng, Error> {
        // `p * util::DEGREES_MAX / worldSize(scale)` (mapbox point operators)
        let world_size = Self::world_size(scale);
        let p2 = Point::new(p.x * DEGREES_MAX / world_size, p.y * DEGREES_MAX / world_size);
        LatLng::with_mode(
            deg2rad(LONGITUDE_MAX - p2.y).exp().atan() * DEGREES_MAX / PI - 90.0,
            p2.x - LONGITUDE_MAX,
            wrap_mode,
        )
    }

    fn project_(lat_lng: &LatLng, world_size: f64) -> Point<f64> {
        let latitude = clamp_f64(lat_lng.latitude(), -LATITUDE_MAX, LATITUDE_MAX);
        // `Point{...} * (worldSize / util::DEGREES_MAX)`
        let k = world_size / DEGREES_MAX;
        Point::new(
            (LONGITUDE_MAX + lat_lng.longitude()) * k,
            (LONGITUDE_MAX - rad2deg((PI / 4.0 + latitude * PI / DEGREES_MAX).tan().ln())) * k,
        )
    }
}

#[cfg(test)]
#[path = "projection_test.rs"]
mod projection_test;