1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use crate::algorithm::native::eq::point_eq;
use crate::array::{CoordBuffer, PointArray};
use crate::geo_traits::{CoordTrait, PointTrait};
use crate::io::geo::point_to_geo;
use crate::scalar::{Coord, Point};
use crate::trait_::GeometryArrayAccessor;

#[derive(Clone, Debug)]
pub struct OwnedPoint {
    coords: CoordBuffer,
    geom_index: usize,
}

impl OwnedPoint {
    pub fn new(coords: CoordBuffer, geom_index: usize) -> Self {
        Self { coords, geom_index }
    }

    pub fn coord(&self) -> Coord {
        self.coords.value(self.geom_index)
    }
}

impl<'a> From<OwnedPoint> for Point<'a> {
    fn from(value: OwnedPoint) -> Self {
        Self::new_owned(value.coords, value.geom_index)
    }
}

impl<'a> From<&'a OwnedPoint> for Point<'a> {
    fn from(value: &'a OwnedPoint) -> Self {
        Self::new_borrowed(&value.coords, value.geom_index)
    }
}

impl<'a> From<Point<'a>> for OwnedPoint {
    fn from(value: Point<'a>) -> Self {
        let (coords, geom_index) = value.into_owned_inner();
        Self::new(coords, geom_index)
    }
}

impl From<OwnedPoint> for PointArray {
    fn from(value: OwnedPoint) -> Self {
        Self::new(value.coords, None, Default::default())
    }
}

impl PointTrait for OwnedPoint {
    type T = f64;

    fn x(&self) -> f64 {
        self.coords.get_x(self.geom_index)
    }

    fn y(&self) -> f64 {
        self.coords.get_y(self.geom_index)
    }
}

impl CoordTrait for OwnedPoint {
    type T = f64;

    fn x(&self) -> Self::T {
        self.coords.get_x(self.geom_index)
    }

    fn y(&self) -> Self::T {
        self.coords.get_y(self.geom_index)
    }
}

impl From<OwnedPoint> for geo::Point {
    fn from(value: OwnedPoint) -> Self {
        value.into()
    }
}

impl From<&OwnedPoint> for geo::Point {
    fn from(value: &OwnedPoint) -> Self {
        point_to_geo(value)
    }
}

impl PartialEq for OwnedPoint {
    fn eq(&self, other: &Self) -> bool {
        point_eq(self, other, true)
    }
}