Skip to main content

lat_long/
long.rs

1//! This module provides the [`Longitude`] type, [`crate::long!`] macro, and associated constants.
2
3use crate::{
4    Angle, Error,
5    fmt::{FormatOptions, Formatter, formatter_impl},
6    inner,
7    parse::{self, Parsed, Value},
8};
9use core::{
10    fmt::{Debug, Display, Write},
11    str::FromStr,
12};
13use ordered_float::OrderedFloat;
14
15#[cfg(feature = "serde")]
16use serde::{Deserialize, Serialize};
17
18// ---------------------------------------------------------------------------
19// Public Types
20// ---------------------------------------------------------------------------
21
22/// A geographic longitude value, constrained to **−180 ≤ degrees ≤ 180**.
23///
24/// Positive values are east of the international reference meridian; negative
25/// values are west.
26///
27/// # Examples
28///
29/// ```rust
30/// use lat_long::{Angle, Longitude};
31///
32/// let lon = Longitude::new(-73, 56, 0.0).unwrap();
33/// assert!(lon.is_western());
34/// ```
35#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
36#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
37pub struct Longitude(OrderedFloat<f64>);
38
39// ---------------------------------------------------------------------------
40// Public Constants
41// ---------------------------------------------------------------------------
42
43/// IERS International Reference Meridian (IRM), or Prime Meridian, at 0° longitude.
44pub const INTERNATIONAL_REFERENCE_MERIDIAN: Longitude = Longitude(inner::ZERO);
45
46/// Antimeridian, the basis for the International Date Line (IDL), at 180° longitude.
47pub const ANTI_MERIDIAN: Longitude = Longitude(OrderedFloat(LONGITUDE_LIMIT));
48
49// ---------------------------------------------------------------------------
50// Public Macros
51// ---------------------------------------------------------------------------
52
53#[macro_export]
54macro_rules! long {
55    (E $degrees:expr, $minutes:expr, $seconds:expr) => {
56        long!($degrees.abs(), $minutes, $seconds).unwrap()
57    };
58    (W $degrees:expr, $minutes:expr, $seconds:expr) => {
59        long!(-$degrees.abs(), $minutes, $seconds).unwrap()
60    };
61    ($degrees:expr, $minutes:expr, $seconds:expr) => {
62        Longitude::new($degrees, $minutes, $seconds).unwrap()
63    };
64    (E $degrees:expr, $minutes:expr) => {
65        long!($degrees.abs(), $minutes).unwrap()
66    };
67    (W $degrees:expr, $minutes:expr) => {
68        long!(-$degrees.abs(), $minutes).unwrap()
69    };
70    ($degrees:expr, $minutes:expr) => {
71        long!($degrees, $minutes, 0.0).unwrap()
72    };
73    (E $degrees:expr) => {
74        long!($degrees.abs()).unwrap()
75    };
76    (W $degrees:expr) => {
77        long!(-$degrees.abs()).unwrap()
78    };
79    ($degrees:expr) => {
80        long!($degrees, 0, 0.0).unwrap()
81    };
82}
83
84// ---------------------------------------------------------------------------
85// Implementations
86// ---------------------------------------------------------------------------
87
88const LONGITUDE_LIMIT: f64 = 180.0;
89
90impl Default for Longitude {
91    fn default() -> Self {
92        INTERNATIONAL_REFERENCE_MERIDIAN
93    }
94}
95
96impl TryFrom<f64> for Longitude {
97    type Error = Error;
98
99    fn try_from(value: f64) -> Result<Self, Self::Error> {
100        Self::try_from(OrderedFloat(value))
101    }
102}
103
104impl TryFrom<OrderedFloat<f64>> for Longitude {
105    type Error = Error;
106
107    fn try_from(value: OrderedFloat<f64>) -> Result<Self, Self::Error> {
108        if value.0 < -LONGITUDE_LIMIT || value.0 > LONGITUDE_LIMIT {
109            return Err(Error::InvalidAngle(value.into_inner(), LONGITUDE_LIMIT));
110        }
111        Ok(Self(value))
112    }
113}
114
115impl From<Longitude> for OrderedFloat<f64> {
116    fn from(value: Longitude) -> Self {
117        value.0.into()
118    }
119}
120
121impl From<Longitude> for f64 {
122    fn from(value: Longitude) -> Self {
123        value.0.into()
124    }
125}
126
127impl FromStr for Longitude {
128    type Err = Error;
129
130    fn from_str(s: &str) -> Result<Self, Self::Err> {
131        match parse::parse_str(s)? {
132            Parsed::Angle(Value::Unknown(decimal)) => Self::try_from(decimal),
133            Parsed::Angle(Value::Longitude(lon)) => Ok(lon),
134            _ => Err(Error::InvalidAngle(0.0, 0.0)),
135        }
136    }
137}
138
139impl Display for Longitude {
140    /// Formats the longitude as decimal degrees by default, or as
141    /// degrees–minutes–seconds when the alternate flag (`{:#}`) is used.
142    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
143        if f.alternate() {
144            let mut buf = String::new();
145            self.format(&mut buf, &FormatOptions::dms_signed())?;
146            f.write_str(&buf)
147        } else {
148            Display::fmt(&(self.0), f)
149        }
150    }
151}
152
153impl Formatter for Longitude {
154    fn format<W: Write>(&self, f: &mut W, fmt: &FormatOptions) -> std::fmt::Result {
155        let fmt = (*fmt).with_labels(('E', 'W'));
156        formatter_impl(self.0, f, &fmt)
157    }
158}
159
160impl Angle for Longitude {
161    const MIN: Self = Self(OrderedFloat(-LONGITUDE_LIMIT));
162    const MAX: Self = Self(OrderedFloat(LONGITUDE_LIMIT));
163
164    fn new(degrees: i32, minutes: u32, seconds: f32) -> Result<Self, Error> {
165        if degrees < Self::MIN.as_float().0 as i32 || degrees > Self::MAX.as_float().0 as i32 {
166            return Err(Error::InvalidLongitudeDegrees(degrees));
167        }
168        let float = inner::from_degrees_minutes_seconds(degrees, minutes, seconds)?;
169        Self::try_from(float).map_err(|_| Error::InvalidLongitudeDegrees(degrees))
170    }
171
172    fn as_float(&self) -> OrderedFloat<f64> {
173        self.0
174    }
175}
176
177impl Longitude {
178    /// Returns `true` if this longitude is exactly on the IERS International Reference Meridian (IRM), or 0°.
179    #[must_use]
180    pub fn is_on_international_reference_meridian(&self) -> bool {
181        self.is_zero()
182    }
183
184    /// Returns `true` if this longitude is in the western hemisphere (< 0°).
185    #[must_use]
186    pub fn is_western(&self) -> bool {
187        self.is_nonzero_negative()
188    }
189
190    /// Returns `true` if this longitude is in the eastern hemisphere (> 0°).
191    #[must_use]
192    pub fn is_eastern(&self) -> bool {
193        self.is_nonzero_positive()
194    }
195}