contour/
contour.rs

1use crate::Float;
2use geo_types::MultiPolygon;
3
4/// A contour has the geometry and threshold of a contour ring, built by [`ContourBuilder`].
5#[derive(Debug, Clone)]
6pub struct Contour {
7    pub(crate) geometry: MultiPolygon<Float>,
8    pub(crate) threshold: Float,
9}
10
11impl Contour {
12    /// Borrow the [`MultiPolygon`](geo_types::MultiPolygon) geometry of this contour.
13    pub fn geometry(&self) -> &MultiPolygon<Float> {
14        &self.geometry
15    }
16
17    /// Get the owned polygons and threshold of this contour.
18    pub fn into_inner(self) -> (MultiPolygon<Float>, Float) {
19        (self.geometry, self.threshold)
20    }
21
22    /// Get the threshold used to construct this contour.
23    pub fn threshold(&self) -> Float {
24        self.threshold
25    }
26
27    #[cfg(feature = "geojson")]
28    /// Convert the contour to a struct from the `geojson` crate.
29    ///
30    /// To get a string representation, call to_geojson().to_string().
31    /// ```
32    /// use contour::ContourBuilder;
33    ///
34    /// let builder = ContourBuilder::new(10, 10, false);
35    /// # #[rustfmt::skip]
36    /// let contours = builder.contours(&[
37    /// // ...ellided for brevity
38    /// #     0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
39    /// #     0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
40    /// #     0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
41    /// #     0., 0., 0., 2., 1., 2., 0., 0., 0., 0.,
42    /// #     0., 0., 0., 2., 2., 2., 0., 0., 0., 0.,
43    /// #     0., 0., 0., 1., 2., 1., 0., 0., 0., 0.,
44    /// #     0., 0., 0., 2., 2., 2., 0., 0., 0., 0.,
45    /// #     0., 0., 0., 2., 1., 2., 0., 0., 0., 0.,
46    /// #     0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
47    /// #     0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
48    /// ], &[0.5]).unwrap();
49    ///
50    /// let geojson_string = contours[0].to_geojson().to_string();
51    ///
52    /// assert_eq!(&geojson_string[0..27], r#"{"geometry":{"coordinates":"#);
53    /// ```
54    pub fn to_geojson(&self) -> geojson::Feature {
55        let mut properties = geojson::JsonObject::with_capacity(1);
56        properties.insert("threshold".to_string(), self.threshold.into());
57
58        geojson::Feature {
59            bbox: None,
60            geometry: Some(geojson::Geometry::from(self.geometry())),
61            id: None,
62            properties: Some(properties),
63            foreign_members: None,
64        }
65    }
66}