stac_extensions/raster.rs
1//! The [Raster](https://github.com/stac-extensions/raster) extnesion.
2//!
3//! An item can describe assets that are rasters of one or multiple bands with
4//! some information common to them all (raster size, projection) and also
5//! specific to each of them (data type, unit, number of bits used, nodata). A
6//! raster is often strongly linked with the georeferencing transform and
7//! coordinate system definition of all bands (using the
8//! [projection](https://github.com/stac-extensions/projection) extension). In
9//! many applications, it is interesting to have some metadata about the rasters
10//! in the asset (values statistics, value interpretation, transforms).
11
12use super::Extension;
13use serde::{Deserialize, Serialize};
14pub use stac::{DataType, Statistics};
15
16/// The raster extension.
17#[derive(Debug, Serialize, Deserialize, Default)]
18pub struct Raster {
19 /// An array of available bands where each object is a [Band].
20 ///
21 /// If given, requires at least one band.
22 pub bands: Vec<Band>,
23}
24
25/// The bands of a raster asset.
26#[derive(Debug, Serialize, Deserialize, Default)]
27pub struct Band {
28 /// Pixel values used to identify pixels that are nodata in the band either
29 /// by the pixel value as a number or nan, inf or -inf (all strings).
30 ///
31 /// The extension specifies that this can be a number or a string, but we
32 /// just use a f64 with a custom (de)serializer.
33 ///
34 /// TODO write custom (de)serializer.
35 #[serde(skip_serializing_if = "Option::is_none")]
36 pub nodata: Option<f64>,
37
38 /// One of area or point.
39 ///
40 /// Indicates whether a pixel value should be assumed to represent a
41 /// sampling over the region of the pixel or a point sample at the center of
42 /// the pixel.
43 #[serde(skip_serializing_if = "Option::is_none")]
44 pub sampling: Option<Sampling>,
45
46 /// The data type of the pixels in the band.
47 #[serde(skip_serializing_if = "Option::is_none")]
48 pub data_type: Option<DataType>,
49
50 /// The actual number of bits used for this band.
51 ///
52 /// Normally only present when the number of bits is non-standard for the
53 /// datatype, such as when a 1 bit TIFF is represented as byte.
54 #[serde(skip_serializing_if = "Option::is_none")]
55 pub bits_per_sample: Option<u64>,
56
57 /// Average spatial resolution (in meters) of the pixels in the band.
58 #[serde(skip_serializing_if = "Option::is_none")]
59 pub spatial_resolution: Option<f64>,
60
61 /// Statistics of all the pixels in the band.
62 #[serde(skip_serializing_if = "Option::is_none")]
63 pub statistics: Option<Statistics>,
64
65 /// Unit denomination of the pixel value.
66 #[serde(skip_serializing_if = "Option::is_none")]
67 pub unit: Option<String>,
68
69 /// Multiplicator factor of the pixel value to transform into the value
70 /// (i.e. translate digital number to reflectance).
71 #[serde(skip_serializing_if = "Option::is_none")]
72 pub scale: Option<f64>,
73
74 /// Number to be added to the pixel value (after scaling) to transform into
75 /// the value (i.e. translate digital number to reflectance).
76 #[serde(skip_serializing_if = "Option::is_none")]
77 pub offset: Option<f64>,
78
79 /// Histogram distribution information of the pixels values in the band.
80 #[serde(skip_serializing_if = "Option::is_none")]
81 pub histogram: Option<Histogram>,
82}
83
84/// Indicates whether a pixel value should be assumed
85/// to represent a sampling over the region of the pixel or a point sample
86/// at the center of the pixel.
87#[derive(Debug, Serialize, Deserialize, PartialEq)]
88#[serde(rename_all = "lowercase")]
89pub enum Sampling {
90 /// The pixel value is a sampling over the region.
91 Area,
92
93 /// The pixel value is a point sample at the center of the pixel.
94 Point,
95}
96
97/// The distribution of pixel values of a band can be provided with a histogram
98/// object.
99///
100/// Those values are sampled in buckets. A histogram object is atomic and all
101/// fields are REQUIRED.
102#[derive(Debug, Serialize, Deserialize)]
103pub struct Histogram {
104 /// Number of buckets of the distribution.
105 pub count: u64,
106
107 /// Minimum value of the distribution. Also the mean value of the first bucket.
108 pub min: f64,
109
110 /// Maximum value of the distribution. Also the mean value of the last bucket.
111 pub max: f64,
112
113 /// Array of integer indicating the number of pixels included in the bucket.
114 pub buckets: Vec<u64>,
115}
116
117impl Extension for Raster {
118 const IDENTIFIER: &'static str = "https://stac-extensions.github.io/raster/v1.1.0/schema.json";
119 const PREFIX: &'static str = "raster";
120}
121
122impl Raster {
123 /// Returns true if this raster structure is empty.
124 ///
125 /// # Examples
126 ///
127 /// ```
128 /// use stac_extensions::Raster;
129 ///
130 /// let raster = Raster::default();
131 /// assert!(raster.is_empty());
132 /// ```
133 pub fn is_empty(&self) -> bool {
134 self.bands.is_empty()
135 }
136}