1 use crate::{CoordinateType, Point};
2 
3 /// A lightweight struct used to store coordinates on the 2-dimensional
4 /// Cartesian plane.
5 ///
6 /// Unlike `Point` (which in the future may contain additional information such
7 /// as an envelope, a precision model, and spatial reference system
8 /// information), a `Coordinate` only contains ordinate values and accessor
9 /// methods.
10 #[derive(PartialEq, Clone, Copy, Debug)]
11 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12 pub struct Coordinate<T>
13 where
14     T: CoordinateType,
15 {
16     pub x: T,
17     pub y: T,
18 }
19 
20 impl<T: CoordinateType> From<(T, T)> for Coordinate<T> {
from(coords: (T, T)) -> Self21     fn from(coords: (T, T)) -> Self {
22         Coordinate {
23             x: coords.0,
24             y: coords.1,
25         }
26     }
27 }
28 
29 impl<T: CoordinateType> From<[T; 2]> for Coordinate<T> {
from(coords: [T; 2]) -> Self30     fn from(coords: [T; 2]) -> Self {
31         Coordinate {
32             x: coords[0],
33             y: coords[1],
34         }
35     }
36 }
37 
38 impl<T: CoordinateType> From<Point<T>> for Coordinate<T> {
from(point: Point<T>) -> Self39     fn from(point: Point<T>) -> Self {
40         Coordinate {
41             x: point.x(),
42             y: point.y(),
43         }
44     }
45 }
46 
47 impl<T> Coordinate<T>
48 where
49     T: CoordinateType,
50 {
51     /// Returns a tuple that contains the x/horizontal & y/vertical component of the coordinate.
52     ///
53     /// # Examples
54     ///
55     /// ```
56     /// use geo_types::Coordinate;
57     ///
58     /// let c = Coordinate {
59     ///     x: 40.02f64,
60     ///     y: 116.34,
61     /// };
62     /// let (x, y) = c.x_y();
63     ///
64     /// assert_eq!(y, 116.34);
65     /// assert_eq!(x, 40.02f64);
66     /// ```
x_y(&self) -> (T, T)67     pub fn x_y(&self) -> (T, T) {
68         (self.x, self.y)
69     }
70 }
71