1use geo_types::{Coord, CoordNum, Geometry};
7
8#[derive(Clone, Copy, Debug, Default, PartialEq, mln_wire::Wire)]
12pub struct Point<T> {
13 pub x: T,
14 pub y: T,
15}
16
17impl<T> Point<T> {
18 #[must_use]
19 pub const fn new(x: T, y: T) -> Self {
20 Point { x, y }
21 }
22}
23
24impl<T: CoordNum> From<Coord<T>> for Point<T> {
25 fn from(c: Coord<T>) -> Self {
26 Point { x: c.x, y: c.y }
27 }
28}
29
30impl<T: CoordNum> From<Point<T>> for Coord<T> {
31 fn from(p: Point<T>) -> Self {
32 Coord { x: p.x, y: p.y }
33 }
34}
35
36#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
37#[repr(u8)]
38pub enum FeatureType {
39 #[default]
40 Unknown = 0,
41 Point = 1,
42 LineString = 2,
43 Polygon = 3,
44}
45
46#[must_use]
49pub fn to_feature_type<T: CoordNum>(geometry: Option<&Geometry<T>>) -> FeatureType {
50 match geometry {
51 Some(Geometry::Point(_) | Geometry::MultiPoint(_)) => FeatureType::Point,
52 Some(Geometry::Line(_) | Geometry::LineString(_) | Geometry::MultiLineString(_)) => FeatureType::LineString,
53 Some(Geometry::Polygon(_) | Geometry::MultiPolygon(_) | Geometry::Rect(_) | Geometry::Triangle(_)) => {
54 FeatureType::Polygon
55 }
56 Some(Geometry::GeometryCollection(_)) | None => FeatureType::Unknown,
57 }
58}
59
60pub fn for_each_point<T: CoordNum>(geometry: &Geometry<T>, f: &mut impl FnMut(&Coord<T>)) {
64 let mut each = |coords: &[Coord<T>]| coords.iter().for_each(&mut *f);
65 match geometry {
66 Geometry::Point(point) => each(std::slice::from_ref(&point.0)),
67 Geometry::Line(line) => each(&[line.start, line.end]),
68 Geometry::LineString(line) => each(&line.0),
69 Geometry::MultiPoint(points) => points.iter().for_each(|p| each(std::slice::from_ref(&p.0))),
70 Geometry::MultiLineString(lines) => lines.iter().for_each(|line| each(&line.0)),
71 Geometry::Polygon(polygon) => {
72 each(&polygon.exterior().0);
73 polygon.interiors().iter().for_each(|ring| each(&ring.0));
74 }
75 Geometry::MultiPolygon(polygons) => polygons.iter().for_each(|polygon| {
76 each(&polygon.exterior().0);
77 polygon.interiors().iter().for_each(|ring| each(&ring.0));
78 }),
79 Geometry::GeometryCollection(geometries) => geometries.iter().for_each(|g| for_each_point(g, f)),
80 Geometry::Rect(rect) => each(&rect.to_polygon().exterior().0),
81 Geometry::Triangle(triangle) => each(&triangle.to_array()),
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use geo_types::{GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Polygon};
88
89 use super::*;
90
91 #[test]
92 fn for_each_point_visits_every_point_in_order() {
93 let p = |x: f64| Coord { x, y: -x };
94 let line = |xs: &[f64]| LineString(xs.iter().map(|&x| p(x)).collect());
95 let geometry = Geometry::GeometryCollection(GeometryCollection(vec![
96 Geometry::GeometryCollection(GeometryCollection(vec![])),
97 Geometry::Point(p(1.0).into()),
98 Geometry::MultiPoint(MultiPoint(vec![p(2.0).into(), p(3.0).into()])),
99 Geometry::LineString(line(&[4.0])),
100 Geometry::MultiLineString(MultiLineString(vec![line(&[5.0]), line(&[6.0])])),
101 Geometry::Polygon(Polygon::new(line(&[7.0, 8.0]), vec![line(&[9.0])])),
103 Geometry::MultiPolygon(MultiPolygon(vec![Polygon::new(line(&[10.0]), vec![])])),
104 Geometry::GeometryCollection(GeometryCollection(vec![Geometry::Point(p(11.0).into())])),
105 ]));
106 let mut xs = Vec::new();
107 for_each_point(&geometry, &mut |point| xs.push(point.x));
108 assert_eq!(xs, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 7.0, 9.0, 10.0, 11.0]);
109 }
110}