Skip to main content

mln_style/
crs.rs

1//! Planar coordinate reference systems that a map can draw in: an app
2//! registers each with its registry, and a style's `projection.type` names
3//! one. A [`WorldMapping`] maps lng/lat to and from the world square that a
4//! map draws in: Web Mercator's, or a registered projection's.
5
6mod mapping;
7mod world;
8
9use std::f64::consts::PI;
10use std::fmt;
11
12pub use mapping::{BoxSamples, WorldBox};
13pub use world::{MERCATOR, WorldMapping};
14
15use crate::constants::EARTH_RADIUS_M;
16use crate::projection::IDENTITY_PROJECTION_NAME;
17
18/// A position that a mapping or a converter has no answer for.
19const NO_ANSWER: [f64; 2] = [f64::NAN, f64::NAN];
20
21/// Whether `position` is an answer: both of its coordinates finite.
22fn is_answer(position: &[f64; 2]) -> bool {
23    position[0].is_finite() && position[1].is_finite()
24}
25
26/// Converts positions between lng/lat and a coordinate reference system's
27/// coordinates, a batch at a time. A position that has no answer comes out
28/// NaN. The map calls it from any thread, several at once.
29pub trait CrsConverter: Send + Sync {
30    /// Each lng/lat in degrees of `lng_lats` to the CRS's coordinates, into
31    /// `positions`, which is as long.
32    fn forward(&self, lng_lats: &[[f64; 2]], positions: &mut [[f64; 2]]);
33
34    /// Each position of the CRS in `positions` to lng/lat in degrees, into
35    /// `lng_lats`, which is as long.
36    fn inverse(&self, positions: &[[f64; 2]], lng_lats: &mut [[f64; 2]]);
37}
38
39/// The square, power-of-two quad tile grid over a coordinate reference
40/// system's plane, what the OGC Two Dimensional Tile Matrix Set standard
41/// calls a quad tree tile matrix set: tile 0/0/0 is the square of side
42/// `extent_at_zoom0` whose top-left corner is `origin`, and each zoom level
43/// splits every tile into four.
44#[derive(Clone, Copy, Debug, PartialEq)]
45pub struct TileMatrixSet {
46    /// The CRS coordinates of the top-left corner of tile 0/0/0, its least x
47    /// and greatest y, in the order that the converter's `forward` gives
48    /// them.
49    pub origin: [f64; 2],
50    /// The side of tile 0/0/0, in the CRS's units.
51    pub extent_at_zoom0: f64,
52}
53
54impl TileMatrixSet {
55    /// Web Mercator's, the grid of the `mercator`, `globe` and
56    /// `vertical-perspective` projections, in EPSG:3857 meters: tile 0/0/0
57    /// spans half the equator each way from the origin of the plane.
58    pub const WEB_MERCATOR: TileMatrixSet = TileMatrixSet {
59        origin: [-PI * EARTH_RADIUS_M, PI * EARTH_RADIUS_M],
60        extent_at_zoom0: 2.0 * PI * EARTH_RADIUS_M,
61    };
62}
63
64/// A planar coordinate reference system and the tile grid over it, which a
65/// map draws in when its style's `projection.type` names it. Every tile
66/// source of the map is taken to be cut in that grid, and its tiles draw as
67/// they are. The CRS's units count as meters wherever the map converts
68/// meters.
69pub struct Crs {
70    /// The name that `projection.type` gives, such as `EPSG:2193`. An app's
71    /// projections have a colon in their names: names without one are kept
72    /// for built-in projections.
73    pub name: String,
74    pub converter: Box<dyn CrsConverter>,
75    pub tile_matrix_set: TileMatrixSet,
76}
77
78impl Crs {
79    /// The built-in `identity` projection, for maps of a plane, such as a
80    /// floor plan or a scanned image: its coordinates are lng/lat degrees
81    /// unchanged, and tile 0/0/0 spans -90 to 90 on both axes.
82    #[must_use]
83    pub fn identity() -> Crs {
84        Crs {
85            name: IDENTITY_PROJECTION_NAME.to_owned(),
86            converter: Box::new(Identity),
87            tile_matrix_set: TileMatrixSet { origin: [-90.0, 90.0], extent_at_zoom0: 180.0 },
88        }
89    }
90}
91
92impl fmt::Debug for Crs {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        f.debug_struct("Crs")
95            .field("name", &self.name)
96            .field("tile_matrix_set", &self.tile_matrix_set)
97            .finish_non_exhaustive()
98    }
99}
100
101/// The converter of [`Crs::identity`], whose coordinates are lng/lat.
102struct Identity;
103
104impl CrsConverter for Identity {
105    fn forward(&self, lng_lats: &[[f64; 2]], positions: &mut [[f64; 2]]) {
106        for (position, lng_lat) in positions.iter_mut().zip(lng_lats) {
107            *position = *lng_lat;
108        }
109    }
110
111    fn inverse(&self, positions: &[[f64; 2]], lng_lats: &mut [[f64; 2]]) {
112        self.forward(positions, lng_lats);
113    }
114}