gistools/geometry/tools/points/
clamp.rs

1use s2json::{GetXY, SetXY};
2
3/// Trait to ensure a WGS84 point is valid
4pub trait ClampWGS84Point {
5    /// Updates the WGS84 point's x and y values as needed to be valid WGS84
6    fn clamp_wgs84(&mut self);
7}
8impl<T: GetXY + SetXY> ClampWGS84Point for T {
9    fn clamp_wgs84(&mut self) {
10        clamp_wgs84_point(self);
11    }
12}
13
14/// Updates the WGS84 point's x and y values as needed to be valid WGS84
15///
16/// ## Parameters
17/// `point`: the WGS 84 point to clamp/wrap
18///
19/// ## Returns
20/// The point itself post update
21pub fn clamp_wgs84_point<P: GetXY + SetXY>(point: &mut P) {
22    let x = point.x();
23    let y = point.y();
24    // Don't touch the point if it's already in bounds
25    if !(-180. ..180.).contains(&x) {
26        point.set_x(((((x + 180.) % 360.) + 360.) % 360.) - 180.);
27    }
28    point.set_y(y.clamp(-90., 90.));
29}