Skip to main content

relate_model/
point.rs

1//! The point axis vocabulary: the two spellings of each coordinate
2//! (`x`/`longitude`, `y`/`latitude`, `z`/`height`) and the geographic-SRID
3//! predicate. One home for the alias fact, consulted by the `point()` input
4//! readers, the component accessors and the wire encoder.
5
6/// One point axis with both of its spellings: the cartesian name and the
7/// geographic name denote the same coordinate (`x` is `longitude`). The one
8/// alias vocabulary the `point()` input readers, the component accessors and
9/// the wire encoder consult, so the pairs are never spelled at a fourth site.
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub struct PointAxis {
12    /// The axis position: 0 = x/longitude, 1 = y/latitude, 2 = z/height.
13    pub index: usize,
14    /// The cartesian spelling.
15    pub cartesian: &'static str,
16    /// The geographic spelling.
17    pub geographic: &'static str,
18}
19
20impl PointAxis {
21    /// Whether `key` is either spelling of this axis.
22    pub fn names(self, key: &str) -> bool {
23        key == self.cartesian || key == self.geographic
24    }
25
26    /// The spelling this axis uses on a point of the given family.
27    pub fn key(self, geographic: bool) -> &'static str {
28        if geographic {
29            self.geographic
30        } else {
31            self.cartesian
32        }
33    }
34}
35
36/// The three point axes in x/y/z order.
37pub const POINT_AXES: [PointAxis; 3] = [
38    PointAxis {
39        index: 0,
40        cartesian: "x",
41        geographic: "longitude",
42    },
43    PointAxis {
44        index: 1,
45        cartesian: "y",
46        geographic: "latitude",
47    },
48    PointAxis {
49        index: 2,
50        cartesian: "z",
51        geographic: "height",
52    },
53];
54
55/// The axis a coordinate key names, or `None` for a non-coordinate key.
56pub fn point_axis(key: &str) -> Option<PointAxis> {
57    POINT_AXES.into_iter().find(|axis| axis.names(key))
58}
59
60/// The three coordinate key spellings a point of the given family uses.
61pub fn point_axis_keys(geographic: bool) -> [&'static str; 3] {
62    [
63        POINT_AXES[0].key(geographic),
64        POINT_AXES[1].key(geographic),
65        POINT_AXES[2].key(geographic),
66    ]
67}
68
69/// Whether an SRID is geographic (longitude/latitude keys, `wgs-84*` CRS).
70pub fn is_geographic(srid: i32) -> bool {
71    matches!(srid, 4326 | 4979)
72}