Skip to main content

egml_io/geometry/primitives/
surface_patch_array_property.rs

1use crate::Error;
2use crate::primitives::GmlSurfacePatchKind;
3use egml_core::model::geometry::primitives::{SurfacePatchArrayProperty, SurfacePatchKind};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
7pub struct GmlSurfacePatchArrayProperty {
8    #[serde(rename = "$value")]
9    pub patches: Vec<GmlSurfacePatchKind>,
10}
11
12impl TryFrom<GmlSurfacePatchArrayProperty> for SurfacePatchArrayProperty {
13    type Error = Error;
14
15    fn try_from(item: GmlSurfacePatchArrayProperty) -> Result<Self, Self::Error> {
16        let patches: Vec<SurfacePatchKind> = item
17            .patches
18            .into_iter()
19            .map(|p| p.try_into())
20            .collect::<Result<Vec<_>, _>>()?;
21
22        Ok(Self::new(patches))
23    }
24}
25
26impl From<&SurfacePatchArrayProperty> for GmlSurfacePatchArrayProperty {
27    fn from(property: &SurfacePatchArrayProperty) -> Self {
28        let patches = property
29            .patches()
30            .iter()
31            .map(GmlSurfacePatchKind::from)
32            .collect();
33        Self { patches }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use crate::primitives::surface_patch_array_property::GmlSurfacePatchArrayProperty;
40    use egml_core::model::geometry::primitives::SurfacePatchArrayProperty;
41    use quick_xml::{DeError, de};
42
43    #[test]
44    fn deserialize_surface_patch_array_property_with_polygon_patches() {
45        let xml_document = b"<gml:patches>
46                <gml:PolygonPatch>
47                    <gml:exterior>
48                        <gml:LinearRing>
49                            <gml:posList>350.54400634765625 972.9130249023438 0.11999999731779099 350.5414201635045 968.6025425887852 0.11999999731779099 350.54400634765625 968.6025096366793 0.11999999731779099 350.54400634765625 972.9130249023438 0.11999999731779099</gml:posList>
50                        </gml:LinearRing>
51                    </gml:exterior>
52                </gml:PolygonPatch>
53                <gml:PolygonPatch>
54                    <gml:exterior>
55                        <gml:LinearRing>
56                            <gml:posList>350.54400634765625 972.9130249023438 0.11999999731779099 350.5414201635045 968.6025425887852 0.11999999731779099 350.54400634765625 968.6025096366793 0.11999999731779099 350.54400634765625 972.9130249023438 0.11999999731779099</gml:posList>
57                        </gml:LinearRing>
58                    </gml:exterior>
59                </gml:PolygonPatch>
60            </gml:patches>";
61
62        let parsed_result: Result<GmlSurfacePatchArrayProperty, DeError> =
63            de::from_reader(xml_document.as_ref());
64        let parsed_gml = parsed_result.expect("parsing should work");
65        let surface_patch_property_array: SurfacePatchArrayProperty =
66            parsed_gml.try_into().unwrap();
67
68        assert_eq!(surface_patch_property_array.patches().len(), 2);
69    }
70
71    #[test]
72    fn deserialize_surface_patch_array_property_with_triangles() {
73        let xml_document = b"<gml:patches>
74    <gml:Triangle>
75        <gml:exterior>
76            <gml:LinearRing>
77                <gml:posList>354.0249938964844 978.864990234375 2.388849973678589 355.39898681640625 978.8480224609375 2.388849973678589 355.3919982910156 978.8480224609375 2.1084799766540527 354.0249938964844 978.864990234375 2.388849973678589</gml:posList>
78            </gml:LinearRing>
79        </gml:exterior>
80    </gml:Triangle>
81    <gml:Triangle>
82        <gml:exterior>
83            <gml:LinearRing>
84                <gml:posList>354.0249938964844 978.864990234375 2.388849973678589 355.3919982910156 978.8480224609375 2.1084799766540527 354.01800537109375 978.864990234375 2.1084799766540527 354.0249938964844 978.864990234375 2.388849973678589</gml:posList>
85            </gml:LinearRing>
86        </gml:exterior>
87    </gml:Triangle>
88    <gml:Triangle>
89        <gml:exterior>
90            <gml:LinearRing>
91                <gml:posList>354.0249938964844 978.864990234375 2.388849973678589 354.01800537109375 978.864990234375 2.1084799766540527 353.9599914550781 978.8660278320312 3.0346200466156006 354.0249938964844 978.864990234375 2.388849973678589</gml:posList>
92            </gml:LinearRing>
93        </gml:exterior>
94    </gml:Triangle>
95</gml:patches>";
96
97        let parsed_result: Result<GmlSurfacePatchArrayProperty, DeError> =
98            de::from_reader(xml_document.as_ref());
99        let parsed_gml = parsed_result.expect("parsing should work");
100        let surface_patch_property_array: SurfacePatchArrayProperty =
101            parsed_gml.try_into().unwrap();
102
103        assert_eq!(surface_patch_property_array.patches().len(), 3);
104    }
105}