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