geoarrow_array/scalar/
point.rs

1use geo_traits::PointTrait;
2use geoarrow_schema::Dimension;
3
4use crate::array::CoordBuffer;
5use crate::eq::point_eq;
6use crate::scalar::Coord;
7
8/// An Arrow equivalent of a Point
9///
10/// This implements [PointTrait], which you can use to extract data.
11#[derive(Debug, Clone)]
12pub struct Point<'a> {
13    coords: &'a CoordBuffer,
14    geom_index: usize,
15}
16
17impl<'a> Point<'a> {
18    pub(crate) fn new(coords: &'a CoordBuffer, geom_index: usize) -> Self {
19        Point { coords, geom_index }
20    }
21
22    pub(crate) fn native_dim(&self) -> Dimension {
23        self.coords.dim()
24    }
25}
26
27impl<'a> PointTrait for Point<'a> {
28    type CoordType<'b>
29        = Coord<'a>
30    where
31        Self: 'b;
32
33    fn coord(&self) -> Option<Self::CoordType<'_>> {
34        let coord = self.coords.value(self.geom_index);
35        if coord.is_nan() { None } else { Some(coord) }
36    }
37}
38
39impl<'a> PointTrait for &Point<'a> {
40    type CoordType<'b>
41        = Coord<'a>
42    where
43        Self: 'b;
44
45    fn coord(&self) -> Option<Self::CoordType<'_>> {
46        let coord = self.coords.value(self.geom_index);
47        if coord.is_nan() { None } else { Some(coord) }
48    }
49}
50
51impl<G: PointTrait<T = f64>> PartialEq<G> for Point<'_> {
52    fn eq(&self, other: &G) -> bool {
53        point_eq(self, other)
54    }
55}
56
57// #[cfg(test)]
58// mod test {
59//     use crate::array::{CoordBuffer, PointArray};
60//     use crate::trait_::ArrayAccessor;
61
62//     /// Test Eq where the current index is true but another index is false
63//     #[test]
64//     fn test_eq_other_index_false() {
65//         let x1 = vec![0., 1., 2.];
66//         let y1 = vec![3., 4., 5.];
67//         let buf1 = CoordBuffer::Separated((x1, y1).try_into().unwrap());
68//         let arr1 = PointArray::new(buf1, None, Default::default());
69
70//         let x2 = vec![0., 100., 2.];
71//         let y2 = vec![3., 400., 5.];
72//         let buf2 = CoordBuffer::Separated((x2, y2).try_into().unwrap());
73//         let arr2 = PointArray::new(buf2, None, Default::default());
74
75//         assert_eq!(arr1.value(0), arr2.value(0));
76//     }
77// }