• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geo-engine / geoengine / 11911118784

19 Nov 2024 10:06AM UTC coverage: 90.448% (-0.2%) from 90.687%
11911118784

push

github

web-flow
Merge pull request #994 from geo-engine/workspace-dependencies

use workspace dependencies, update toolchain, use global lock in expression

9 of 11 new or added lines in 6 files covered. (81.82%)

369 existing lines in 74 files now uncovered.

132871 of 146904 relevant lines covered (90.45%)

54798.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.14
/operators/src/plot/histogram.rs
1
use crate::engine::{
2
    CanonicOperatorName, ExecutionContext, InitializedPlotOperator, InitializedRasterOperator,
3
    InitializedVectorOperator, Operator, OperatorName, PlotOperator, PlotQueryProcessor,
4
    PlotResultDescriptor, QueryContext, SingleRasterOrVectorSource, TypedPlotQueryProcessor,
5
    TypedRasterQueryProcessor, TypedVectorQueryProcessor,
6
};
7
use crate::engine::{QueryProcessor, WorkflowOperatorPath};
8
use crate::error;
9
use crate::error::Error;
10
use crate::string_token;
11
use crate::util::input::RasterOrVectorOperator;
12
use crate::util::Result;
13
use async_trait::async_trait;
14
use float_cmp::approx_eq;
15
use futures::stream::BoxStream;
16
use futures::{StreamExt, TryFutureExt};
17
use geoengine_datatypes::plots::{Plot, PlotData};
18
use geoengine_datatypes::primitives::{
19
    AxisAlignedRectangle, BandSelection, BoundingBox2D, DataRef, FeatureDataRef, FeatureDataType,
20
    Geometry, Measurement, PlotQueryRectangle, RasterQueryRectangle,
21
};
22
use geoengine_datatypes::raster::{Pixel, RasterTile2D};
23
use geoengine_datatypes::{
24
    collections::{FeatureCollection, FeatureCollectionInfos},
25
    raster::GridSize,
26
};
27
use serde::{Deserialize, Serialize};
28
use snafu::ensure;
29
use std::convert::TryFrom;
30

31
pub const HISTOGRAM_OPERATOR_NAME: &str = "Histogram";
32

33
/// A histogram plot about either a raster or a vector input.
34
///
35
/// For vector inputs, it calculates the histogram on one of its attributes.
36
///
37
pub type Histogram = Operator<HistogramParams, SingleRasterOrVectorSource>;
38

39
impl OperatorName for Histogram {
40
    const TYPE_NAME: &'static str = "Histogram";
41
}
42

43
/// The parameter spec for `Histogram`
44
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18✔
45
#[serde(rename_all = "camelCase")]
46
pub struct HistogramParams {
47
    /// Name of the (numeric) vector attribute or raster band to compute the histogram on.
48
    pub attribute_name: String,
49
    /// The bounds (min/max) of the histogram.
50
    pub bounds: HistogramBounds,
51
    /// Specify the number of buckets or how it should be derived.
52
    pub buckets: HistogramBuckets,
53
    /// Whether to create an interactive output (`false` by default)
54
    #[serde(default)]
55
    pub interactive: bool,
56
}
57

58
/// Options for how to derive the histogram's number of buckets.
59
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12✔
60
#[serde(rename_all = "camelCase", tag = "type")]
61
pub enum HistogramBuckets {
62
    #[serde(rename_all = "camelCase")]
63
    Number { value: u8 },
64
    #[serde(rename_all = "camelCase")]
65
    SquareRootChoiceRule {
66
        #[serde(default = "default_max_number_of_buckets")]
67
        max_number_of_buckets: u8,
68
    },
69
}
70

71
fn default_max_number_of_buckets() -> u8 {
×
72
    100
×
73
}
×
74

75
string_token!(Data, "data");
76

77
/// Let the bounds either be computed or given.
78
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6✔
79
#[serde(untagged)]
80
pub enum HistogramBounds {
81
    Data(Data),
82
    Values { min: f64, max: f64 },
83
    // TODO: use bounds in measurement if they are available
84
}
85

86
#[typetag::serde]
1✔
87
#[async_trait]
88
impl PlotOperator for Histogram {
89
    async fn _initialize(
90
        self: Box<Self>,
91
        path: WorkflowOperatorPath,
92
        context: &dyn ExecutionContext,
93
    ) -> Result<Box<dyn InitializedPlotOperator>> {
11✔
94
        let name = CanonicOperatorName::from(&self);
11✔
95

11✔
96
        Ok(match self.sources.source {
11✔
97
            RasterOrVectorOperator::Raster(raster_source) => {
6✔
98
                let raster_source = raster_source
6✔
99
                    .initialize(path.clone_and_append(0), context)
6✔
100
                    .await?;
1✔
101

102
                let in_desc = raster_source.result_descriptor();
5✔
103

5✔
104
                ensure!(
5✔
105
                    in_desc
5✔
106
                        .bands
5✔
107
                        .iter()
5✔
108
                        .any(|b| b.name == self.params.attribute_name),
5✔
UNCOV
109
                    error::InvalidOperatorSpec {
×
110
                        reason: "Band with given `attribute_name` does not exist".to_string(),
×
111
                    }
×
112
                );
113

114
                InitializedHistogram::new(
5✔
115
                    name,
5✔
116
                    PlotResultDescriptor {
5✔
117
                        spatial_reference: in_desc.spatial_reference,
5✔
118
                        time: in_desc.time,
5✔
119
                        // converting `SpatialPartition2D` to `BoundingBox2D` is ok here, because is makes the covered area only larger
5✔
120
                        bbox: in_desc
5✔
121
                            .bbox
5✔
122
                            .and_then(|p| BoundingBox2D::new(p.lower_left(), p.upper_right()).ok()),
5✔
123
                    },
5✔
124
                    self.params,
5✔
125
                    raster_source,
5✔
126
                )
5✔
127
                .boxed()
5✔
128
            }
129
            RasterOrVectorOperator::Vector(vector_source) => {
5✔
130
                let column_name = &self.params.attribute_name;
5✔
131
                let vector_source = vector_source
5✔
132
                    .initialize(path.clone_and_append(0), context)
5✔
UNCOV
133
                    .await?;
×
134

135
                match vector_source
5✔
136
                    .result_descriptor()
5✔
137
                    .column_data_type(column_name)
5✔
138
                {
139
                    None => {
UNCOV
140
                        return Err(Error::ColumnDoesNotExist {
×
141
                            column: column_name.to_string(),
×
142
                        });
×
143
                    }
144
                    Some(FeatureDataType::Category | FeatureDataType::Text) => {
145
                        // TODO: incorporate category data
146
                        return Err(Error::InvalidOperatorSpec {
1✔
147
                            reason: format!("column `{column_name}` must be numerical"),
1✔
148
                        });
1✔
149
                    }
150
                    Some(
151
                        FeatureDataType::Int
152
                        | FeatureDataType::Float
153
                        | FeatureDataType::Bool
154
                        | FeatureDataType::DateTime,
155
                    ) => {
4✔
156
                        // okay
4✔
157
                    }
4✔
158
                }
4✔
159

4✔
160
                let in_desc = vector_source.result_descriptor().clone();
4✔
161

4✔
162
                InitializedHistogram::new(name, in_desc.into(), self.params, vector_source).boxed()
4✔
163
            }
164
        })
165
    }
22✔
166

167
    span_fn!(Histogram);
168
}
169

170
/// The initialization of `Histogram`
171
pub struct InitializedHistogram<Op> {
172
    name: CanonicOperatorName,
173
    result_descriptor: PlotResultDescriptor,
174
    metadata: HistogramMetadataOptions,
175
    source: Op,
176
    interactive: bool,
177
    attribute_name: String,
178
}
179

180
impl<Op> InitializedHistogram<Op> {
181
    pub fn new(
9✔
182
        name: CanonicOperatorName,
9✔
183
        result_descriptor: PlotResultDescriptor,
9✔
184
        params: HistogramParams,
9✔
185
        source: Op,
9✔
186
    ) -> Self {
9✔
187
        let (min, max) = if let HistogramBounds::Values { min, max } = params.bounds {
9✔
188
            (Some(min), Some(max))
3✔
189
        } else {
190
            (None, None)
6✔
191
        };
192

193
        let (number_of_buckets, max_number_of_buckets) = match params.buckets {
9✔
194
            HistogramBuckets::Number {
195
                value: number_of_buckets,
3✔
196
            } => (Some(number_of_buckets as usize), None),
3✔
197
            HistogramBuckets::SquareRootChoiceRule {
198
                max_number_of_buckets,
6✔
199
            } => (None, Some(max_number_of_buckets as usize)),
6✔
200
        };
201

202
        Self {
9✔
203
            name,
9✔
204
            result_descriptor,
9✔
205
            metadata: HistogramMetadataOptions {
9✔
206
                number_of_buckets,
9✔
207
                max_number_of_buckets,
9✔
208
                min,
9✔
209
                max,
9✔
210
            },
9✔
211
            source,
9✔
212
            interactive: params.interactive,
9✔
213
            attribute_name: params.attribute_name,
9✔
214
        }
9✔
215
    }
9✔
216
}
217

218
impl InitializedPlotOperator for InitializedHistogram<Box<dyn InitializedRasterOperator>> {
219
    fn query_processor(&self) -> Result<TypedPlotQueryProcessor> {
5✔
220
        let (band_idx, band) = self
5✔
221
            .source
5✔
222
            .result_descriptor()
5✔
223
            .bands
5✔
224
            .iter().enumerate()
5✔
225
            .find(|(_, b)| b.name == self.attribute_name).expect("the band with the attribute name should exist in the source because it was checked during initialization of the operator.");
5✔
226

227
        let processor = HistogramRasterQueryProcessor {
5✔
228
            input: self.source.query_processor()?,
5✔
229
            band_idx: band_idx as u32,
5✔
230
            measurement: band.measurement.clone(),
5✔
231
            metadata: self.metadata,
5✔
232
            interactive: self.interactive,
5✔
233
        };
5✔
234

5✔
235
        Ok(TypedPlotQueryProcessor::JsonVega(processor.boxed()))
5✔
236
    }
5✔
237

238
    fn result_descriptor(&self) -> &PlotResultDescriptor {
1✔
239
        &self.result_descriptor
1✔
240
    }
1✔
241

242
    fn canonic_name(&self) -> CanonicOperatorName {
×
243
        self.name.clone()
×
244
    }
×
245
}
246

247
impl InitializedPlotOperator for InitializedHistogram<Box<dyn InitializedVectorOperator>> {
248
    fn query_processor(&self) -> Result<TypedPlotQueryProcessor> {
4✔
249
        let processor = HistogramVectorQueryProcessor {
4✔
250
            input: self.source.query_processor()?,
4✔
251
            column_name: self.attribute_name.clone(),
4✔
252
            measurement: self
4✔
253
                .source
4✔
254
                .result_descriptor()
4✔
255
                .column_measurement(&self.attribute_name)
4✔
256
                .cloned()
4✔
257
                .into(),
4✔
258
            metadata: self.metadata,
4✔
259
            interactive: self.interactive,
4✔
260
        };
4✔
261

4✔
262
        Ok(TypedPlotQueryProcessor::JsonVega(processor.boxed()))
4✔
263
    }
4✔
264

265
    fn result_descriptor(&self) -> &PlotResultDescriptor {
×
266
        &self.result_descriptor
×
267
    }
×
268

269
    fn canonic_name(&self) -> CanonicOperatorName {
×
270
        self.name.clone()
×
271
    }
×
272
}
273

274
/// A query processor that calculates the Histogram about its raster inputs.
275
pub struct HistogramRasterQueryProcessor {
276
    input: TypedRasterQueryProcessor,
277
    band_idx: u32,
278
    measurement: Measurement,
279
    metadata: HistogramMetadataOptions,
280
    interactive: bool,
281
}
282

283
/// A query processor that calculates the Histogram about its vector inputs.
284
pub struct HistogramVectorQueryProcessor {
285
    input: TypedVectorQueryProcessor,
286
    column_name: String,
287
    measurement: Measurement,
288
    metadata: HistogramMetadataOptions,
289
    interactive: bool,
290
}
291

292
#[async_trait]
293
impl PlotQueryProcessor for HistogramRasterQueryProcessor {
294
    type OutputFormat = PlotData;
295

296
    fn plot_type(&self) -> &'static str {
1✔
297
        HISTOGRAM_OPERATOR_NAME
1✔
298
    }
1✔
299

300
    async fn plot_query<'p>(
301
        &'p self,
302
        query: PlotQueryRectangle,
303
        ctx: &'p dyn QueryContext,
304
    ) -> Result<Self::OutputFormat> {
5✔
305
        self.preprocess(query.clone(), ctx)
5✔
306
            .and_then(move |mut histogram_metadata| async move {
5✔
307
                histogram_metadata.sanitize();
5✔
308
                if histogram_metadata.has_invalid_parameters() {
5✔
309
                    // early return of empty histogram
310
                    return self.empty_histogram();
1✔
311
                }
4✔
312

4✔
313
                self.process(histogram_metadata, query, ctx).await
4✔
314
            })
10✔
UNCOV
315
            .await
×
316
    }
10✔
317
}
318

319
#[async_trait]
320
impl PlotQueryProcessor for HistogramVectorQueryProcessor {
321
    type OutputFormat = PlotData;
322

323
    fn plot_type(&self) -> &'static str {
×
324
        HISTOGRAM_OPERATOR_NAME
×
325
    }
×
326

327
    async fn plot_query<'p>(
328
        &'p self,
329
        query: PlotQueryRectangle,
330
        ctx: &'p dyn QueryContext,
331
    ) -> Result<Self::OutputFormat> {
4✔
332
        self.preprocess(query.clone(), ctx)
4✔
333
            .and_then(move |mut histogram_metadata| async move {
4✔
334
                histogram_metadata.sanitize();
4✔
335
                if histogram_metadata.has_invalid_parameters() {
4✔
336
                    // early return of empty histogram
337
                    return self.empty_histogram();
1✔
338
                }
3✔
339

3✔
340
                self.process(histogram_metadata, query, ctx).await
3✔
341
            })
8✔
UNCOV
342
            .await
×
343
    }
8✔
344
}
345

346
impl HistogramRasterQueryProcessor {
347
    async fn preprocess<'p>(
5✔
348
        &'p self,
5✔
349
        query: PlotQueryRectangle,
5✔
350
        ctx: &'p dyn QueryContext,
5✔
351
    ) -> Result<HistogramMetadata> {
5✔
352
        async fn process_metadata<T: Pixel>(
3✔
353
            mut input: BoxStream<'_, Result<RasterTile2D<T>>>,
3✔
354
            metadata: HistogramMetadataOptions,
3✔
355
        ) -> Result<HistogramMetadata> {
3✔
356
            let mut computed_metadata = HistogramMetadataInProgress::default();
3✔
357

358
            while let Some(tile) = input.next().await {
15✔
359
                match tile?.grid_array {
12✔
360
                    geoengine_datatypes::raster::GridOrEmpty::Grid(g) => {
2✔
361
                        computed_metadata.add_raster_batch(g.masked_element_deref_iterator());
2✔
362
                    }
2✔
363
                    geoengine_datatypes::raster::GridOrEmpty::Empty(_) => {} // TODO: find out if we really do nothing for empty tiles?
10✔
364
                }
365
            }
366

367
            Ok(metadata.merge_with(computed_metadata.into()))
3✔
368
        }
3✔
369

370
        if let Ok(metadata) = HistogramMetadata::try_from(self.metadata) {
5✔
371
            return Ok(metadata);
2✔
372
        }
3✔
373

374
        // TODO: compute only number of buckets if possible
375

376
        call_on_generic_raster_processor!(&self.input, processor => {
3✔
377
            process_metadata(processor.query(RasterQueryRectangle::from_qrect_and_bands(&query, BandSelection::new_single(self.band_idx)), ctx).await?, self.metadata).await
1✔
378
        })
379
    }
5✔
380

381
    async fn process<'p>(
4✔
382
        &'p self,
4✔
383
        metadata: HistogramMetadata,
4✔
384
        query: PlotQueryRectangle,
4✔
385
        ctx: &'p dyn QueryContext,
4✔
386
    ) -> Result<<HistogramRasterQueryProcessor as PlotQueryProcessor>::OutputFormat> {
4✔
387
        let mut histogram = geoengine_datatypes::plots::Histogram::builder(
4✔
388
            metadata.number_of_buckets,
4✔
389
            metadata.min,
4✔
390
            metadata.max,
4✔
391
            self.measurement.clone(),
4✔
392
        )
4✔
393
        .build()
4✔
394
        .map_err(Error::from)?;
4✔
395

396
        call_on_generic_raster_processor!(&self.input, processor => {
4✔
397
            let mut query = processor.query(RasterQueryRectangle::from_qrect_and_bands(&query, BandSelection::new_single(self.band_idx)), ctx).await?;
×
398

399
            while let Some(tile) = query.next().await {
×
400

401

402
                match tile?.grid_array {
×
403
                    geoengine_datatypes::raster::GridOrEmpty::Grid(g) => histogram.add_raster_data(g.masked_element_deref_iterator()),
×
404
                    geoengine_datatypes::raster::GridOrEmpty::Empty(n) => histogram.add_nodata_batch(n.number_of_elements() as u64) // TODO: why u64?
×
405
                }
406
            }
407
        });
408

409
        let chart = histogram.to_vega_embeddable(self.interactive)?;
4✔
410

411
        Ok(chart)
4✔
412
    }
4✔
413

414
    fn empty_histogram(
1✔
415
        &self,
1✔
416
    ) -> Result<<HistogramRasterQueryProcessor as PlotQueryProcessor>::OutputFormat> {
1✔
417
        let histogram =
1✔
418
            geoengine_datatypes::plots::Histogram::builder(1, 0., 0., self.measurement.clone())
1✔
419
                .build()
1✔
420
                .map_err(Error::from)?;
1✔
421

422
        let chart = histogram.to_vega_embeddable(self.interactive)?;
1✔
423

424
        Ok(chart)
1✔
425
    }
1✔
426
}
427

428
impl HistogramVectorQueryProcessor {
429
    async fn preprocess<'p>(
4✔
430
        &'p self,
4✔
431
        query: PlotQueryRectangle,
4✔
432
        ctx: &'p dyn QueryContext,
4✔
433
    ) -> Result<HistogramMetadata> {
4✔
434
        async fn process_metadata<'m, G>(
3✔
435
            mut input: BoxStream<'m, Result<FeatureCollection<G>>>,
3✔
436
            column_name: &'m str,
3✔
437
            metadata: HistogramMetadataOptions,
3✔
438
        ) -> Result<HistogramMetadata>
3✔
439
        where
3✔
440
            G: Geometry + 'static,
3✔
441
            FeatureCollection<G>: FeatureCollectionInfos,
3✔
442
        {
3✔
443
            let mut computed_metadata = HistogramMetadataInProgress::default();
3✔
444

445
            while let Some(collection) = input.next().await {
6✔
446
                let collection = collection?;
3✔
447

448
                let feature_data = collection.data(column_name).expect("check in param");
3✔
449
                computed_metadata.add_vector_batch(feature_data);
3✔
450
            }
451

452
            Ok(metadata.merge_with(computed_metadata.into()))
3✔
453
        }
3✔
454

455
        if let Ok(metadata) = HistogramMetadata::try_from(self.metadata) {
4✔
456
            return Ok(metadata);
1✔
457
        }
3✔
458

459
        // TODO: compute only number of buckets if possible
460

461
        call_on_generic_vector_processor!(&self.input, processor => {
3✔
462
            process_metadata(processor.query(query.into(), ctx).await?, &self.column_name, self.metadata).await
3✔
463
        })
464
    }
4✔
465

466
    async fn process<'p>(
3✔
467
        &'p self,
3✔
468
        metadata: HistogramMetadata,
3✔
469
        query: PlotQueryRectangle,
3✔
470
        ctx: &'p dyn QueryContext,
3✔
471
    ) -> Result<<HistogramRasterQueryProcessor as PlotQueryProcessor>::OutputFormat> {
3✔
472
        let mut histogram = geoengine_datatypes::plots::Histogram::builder(
3✔
473
            metadata.number_of_buckets,
3✔
474
            metadata.min,
3✔
475
            metadata.max,
3✔
476
            self.measurement.clone(),
3✔
477
        )
3✔
478
        .build()
3✔
479
        .map_err(Error::from)?;
3✔
480

481
        call_on_generic_vector_processor!(&self.input, processor => {
3✔
482
            let mut query = processor.query(query.into(), ctx).await?;
3✔
483

484
            while let Some(collection) = query.next().await {
7✔
485
                let collection = collection?;
4✔
486

487
                let feature_data = collection.data(&self.column_name).expect("checked in param");
4✔
488

4✔
489
                histogram.add_feature_data(feature_data)?;
4✔
490
            }
491
        });
492

493
        let chart = histogram.to_vega_embeddable(self.interactive)?;
3✔
494

495
        Ok(chart)
3✔
496
    }
3✔
497

498
    fn empty_histogram(
1✔
499
        &self,
1✔
500
    ) -> Result<<HistogramRasterQueryProcessor as PlotQueryProcessor>::OutputFormat> {
1✔
501
        let histogram =
1✔
502
            geoengine_datatypes::plots::Histogram::builder(1, 0., 0., self.measurement.clone())
1✔
503
                .build()
1✔
504
                .map_err(Error::from)?;
1✔
505

506
        let chart = histogram.to_vega_embeddable(self.interactive)?;
1✔
507

508
        Ok(chart)
1✔
509
    }
1✔
510
}
511

512
#[derive(Debug, Copy, Clone, PartialEq)]
513
struct HistogramMetadata {
514
    pub number_of_buckets: usize,
515
    pub min: f64,
516
    pub max: f64,
517
}
518

519
impl HistogramMetadata {
520
    /// Fix invalid configurations if they are fixeable
521
    fn sanitize(&mut self) {
9✔
522
        // prevent the rare case that min=max and you have more than one bucket
9✔
523
        if approx_eq!(f64, self.min, self.max) && self.number_of_buckets > 1 {
9✔
524
            self.number_of_buckets = 1;
1✔
525
        }
8✔
526
    }
9✔
527

528
    fn has_invalid_parameters(&self) -> bool {
9✔
529
        self.number_of_buckets == 0 || self.min > self.max
9✔
530
    }
9✔
531
}
532

533
#[derive(Debug, Copy, Clone, PartialEq)]
534
struct HistogramMetadataOptions {
535
    pub number_of_buckets: Option<usize>,
536
    pub max_number_of_buckets: Option<usize>,
537
    pub min: Option<f64>,
538
    pub max: Option<f64>,
539
}
540

541
impl TryFrom<HistogramMetadataOptions> for HistogramMetadata {
542
    type Error = ();
543

544
    fn try_from(options: HistogramMetadataOptions) -> Result<Self, Self::Error> {
9✔
545
        match (options.number_of_buckets, options.min, options.max) {
9✔
546
            (Some(number_of_buckets), Some(min), Some(max)) => Ok(Self {
3✔
547
                number_of_buckets,
3✔
548
                min,
3✔
549
                max,
3✔
550
            }),
3✔
551
            _ => Err(()),
6✔
552
        }
553
    }
9✔
554
}
555

556
impl HistogramMetadataOptions {
557
    fn merge_with(self, metadata: HistogramMetadata) -> HistogramMetadata {
6✔
558
        let number_of_buckets = if let Some(number_of_buckets) = self.number_of_buckets {
6✔
559
            number_of_buckets
×
560
        } else if let Some(max_number_of_buckets) = self.max_number_of_buckets {
6✔
561
            metadata.number_of_buckets.min(max_number_of_buckets)
6✔
562
        } else {
563
            metadata.number_of_buckets
×
564
        };
565

566
        HistogramMetadata {
6✔
567
            number_of_buckets,
6✔
568
            min: self.min.unwrap_or(metadata.min),
6✔
569
            max: self.max.unwrap_or(metadata.max),
6✔
570
        }
6✔
571
    }
6✔
572
}
573

574
#[derive(Debug, Copy, Clone, PartialEq)]
575
struct HistogramMetadataInProgress {
576
    pub n: usize,
577
    pub min: f64,
578
    pub max: f64,
579
}
580

581
impl Default for HistogramMetadataInProgress {
582
    fn default() -> Self {
6✔
583
        Self {
6✔
584
            n: 0,
6✔
585
            min: f64::MAX,
6✔
586
            max: f64::MIN,
6✔
587
        }
6✔
588
    }
6✔
589
}
590

591
impl HistogramMetadataInProgress {
592
    #[inline]
593
    fn add_raster_batch<T: Pixel, I: Iterator<Item = Option<T>>>(&mut self, values: I) {
2✔
594
        values.for_each(|pixel_option| {
12✔
595
            if let Some(p) = pixel_option {
12✔
596
                self.n += 1;
12✔
597
                self.update_minmax(p.as_());
12✔
598
            }
12✔
599
        });
12✔
600
    }
2✔
601

602
    #[inline]
603
    fn add_vector_batch(&mut self, values: FeatureDataRef) {
3✔
604
        fn add_data_ref<'d, D, T>(metadata: &mut HistogramMetadataInProgress, data_ref: &'d D)
3✔
605
        where
3✔
606
            D: DataRef<'d, T>,
3✔
607
            T: 'static,
3✔
608
        {
3✔
609
            for v in data_ref.float_options_iter().flatten() {
5✔
610
                metadata.n += 1;
5✔
611
                metadata.update_minmax(v);
5✔
612
            }
5✔
613
        }
3✔
614

615
        match values {
3✔
616
            FeatureDataRef::Int(values) => {
×
617
                add_data_ref(self, &values);
×
618
            }
×
619
            FeatureDataRef::Float(values) => {
3✔
620
                add_data_ref(self, &values);
3✔
621
            }
3✔
622
            FeatureDataRef::Bool(values) => {
×
623
                add_data_ref(self, &values);
×
624
            }
×
625
            FeatureDataRef::DateTime(values) => {
×
626
                add_data_ref(self, &values);
×
627
            }
×
628
            FeatureDataRef::Category(_) | FeatureDataRef::Text(_) => {
×
629
                // do nothing since we don't support them
×
630
                // TODO: fill with live once we support category and text types
×
631
            }
×
632
        }
633
    }
3✔
634

635
    #[inline]
636
    fn update_minmax(&mut self, value: f64) {
17✔
637
        self.min = f64::min(self.min, value);
17✔
638
        self.max = f64::max(self.max, value);
17✔
639
    }
17✔
640
}
641

642
impl From<HistogramMetadataInProgress> for HistogramMetadata {
643
    fn from(metadata: HistogramMetadataInProgress) -> Self {
6✔
644
        Self {
6✔
645
            number_of_buckets: f64::sqrt(metadata.n as f64) as usize,
6✔
646
            min: metadata.min,
6✔
647
            max: metadata.max,
6✔
648
        }
6✔
649
    }
6✔
650
}
651

652
#[cfg(test)]
653
mod tests {
654
    use super::*;
655

656
    use crate::engine::{
657
        ChunkByteSize, MockExecutionContext, MockQueryContext, RasterBandDescriptors,
658
        RasterOperator, RasterResultDescriptor, StaticMetaData, VectorColumnInfo, VectorOperator,
659
        VectorResultDescriptor,
660
    };
661
    use crate::mock::{MockFeatureCollectionSource, MockRasterSource, MockRasterSourceParams};
662
    use crate::source::{
663
        OgrSourceColumnSpec, OgrSourceDataset, OgrSourceDatasetTimeType, OgrSourceErrorSpec,
664
    };
665
    use crate::test_data;
666
    use geoengine_datatypes::dataset::{DataId, DatasetId, NamedData};
667
    use geoengine_datatypes::primitives::{
668
        BoundingBox2D, DateTime, FeatureData, NoGeometry, PlotSeriesSelection, SpatialResolution,
669
        TimeInterval, VectorQueryRectangle,
670
    };
671
    use geoengine_datatypes::primitives::{CacheHint, CacheTtlSeconds};
672
    use geoengine_datatypes::raster::{
673
        EmptyGrid2D, Grid2D, RasterDataType, RasterTile2D, TileInformation, TilingSpecification,
674
    };
675
    use geoengine_datatypes::spatial_reference::SpatialReference;
676
    use geoengine_datatypes::util::test::TestDefault;
677
    use geoengine_datatypes::util::Identifier;
678
    use geoengine_datatypes::{
679
        collections::{DataCollection, VectorDataType},
680
        primitives::MultiPoint,
681
    };
682
    use serde_json::json;
683

684
    #[test]
685
    fn serialization() {
1✔
686
        let histogram = Histogram {
1✔
687
            params: HistogramParams {
1✔
688
                attribute_name: "foobar".to_string(),
1✔
689
                bounds: HistogramBounds::Values {
1✔
690
                    min: 5.0,
1✔
691
                    max: 10.0,
1✔
692
                },
1✔
693
                buckets: HistogramBuckets::Number { value: 15 },
1✔
694
                interactive: false,
1✔
695
            },
1✔
696
            sources: MockFeatureCollectionSource::<MultiPoint>::multiple(vec![])
1✔
697
                .boxed()
1✔
698
                .into(),
1✔
699
        };
1✔
700

1✔
701
        let serialized = json!({
1✔
702
            "type": "Histogram",
1✔
703
            "params": {
1✔
704
                "attributeName": "foobar",
1✔
705
                "bounds": {
1✔
706
                    "min": 5.0,
1✔
707
                    "max": 10.0,
1✔
708
                },
1✔
709
                "buckets": {
1✔
710
                    "type": "number",
1✔
711
                    "value": 15,
1✔
712
                },
1✔
713
                "interactivity": false,
1✔
714
            },
1✔
715
            "sources": {
1✔
716
                "source": {
1✔
717
                    "type": "MockFeatureCollectionSourceMultiPoint",
1✔
718
                    "params": {
1✔
719
                        "collections": [],
1✔
720
                        "spatialReference": "EPSG:4326",
1✔
721
                        "measurements": {},
1✔
722
                    }
1✔
723
                }
1✔
724
            }
1✔
725
        })
1✔
726
        .to_string();
1✔
727

1✔
728
        let deserialized: Histogram = serde_json::from_str(&serialized).unwrap();
1✔
729

1✔
730
        assert_eq!(deserialized.params, histogram.params);
1✔
731
    }
1✔
732

733
    #[test]
734
    fn serialization_alt() {
1✔
735
        let histogram = Histogram {
1✔
736
            params: HistogramParams {
1✔
737
                attribute_name: "band".to_string(),
1✔
738
                bounds: HistogramBounds::Data(Default::default()),
1✔
739
                buckets: HistogramBuckets::SquareRootChoiceRule {
1✔
740
                    max_number_of_buckets: 100,
1✔
741
                },
1✔
742
                interactive: false,
1✔
743
            },
1✔
744
            sources: MockFeatureCollectionSource::<MultiPoint>::multiple(vec![])
1✔
745
                .boxed()
1✔
746
                .into(),
1✔
747
        };
1✔
748

1✔
749
        let serialized = json!({
1✔
750
            "type": "Histogram",
1✔
751
            "params": {
1✔
752
                "attributeName": "band",
1✔
753
                "bounds": "data",
1✔
754
                "buckets": {
1✔
755
                    "type": "squareRootChoiceRule",
1✔
756
                    "maxNumberOfBuckets": 100,
1✔
757
                },
1✔
758
            },
1✔
759
            "sources": {
1✔
760
                "source": {
1✔
761
                    "type": "MockFeatureCollectionSourceMultiPoint",
1✔
762
                    "params": {
1✔
763
                        "collections": [],
1✔
764
                        "spatialReference": "EPSG:4326",
1✔
765
                        "measurements": {},
1✔
766
                    }
1✔
767
                }
1✔
768
            }
1✔
769
        })
1✔
770
        .to_string();
1✔
771

1✔
772
        let deserialized: Histogram = serde_json::from_str(&serialized).unwrap();
1✔
773

1✔
774
        assert_eq!(deserialized.params, histogram.params);
1✔
775
    }
1✔
776

777
    #[tokio::test]
778
    async fn column_name_for_raster_source() {
1✔
779
        let histogram = Histogram {
1✔
780
            params: HistogramParams {
1✔
781
                attribute_name: "foo".to_string(),
1✔
782
                bounds: HistogramBounds::Values { min: 0.0, max: 8.0 },
1✔
783
                buckets: HistogramBuckets::Number { value: 3 },
1✔
784
                interactive: false,
1✔
785
            },
1✔
786
            sources: mock_raster_source().into(),
1✔
787
        };
1✔
788

1✔
789
        let execution_context = MockExecutionContext::test_default();
1✔
790

1✔
791
        assert!(histogram
1✔
792
            .boxed()
1✔
793
            .initialize(WorkflowOperatorPath::initialize_root(), &execution_context)
1✔
794
            .await
1✔
795
            .is_err());
1✔
796
    }
1✔
797

798
    fn mock_raster_source() -> Box<dyn RasterOperator> {
3✔
799
        MockRasterSource {
3✔
800
            params: MockRasterSourceParams {
3✔
801
                data: vec![RasterTile2D::new_with_tile_info(
3✔
802
                    TimeInterval::default(),
3✔
803
                    TileInformation {
3✔
804
                        global_geo_transform: TestDefault::test_default(),
3✔
805
                        global_tile_position: [0, 0].into(),
3✔
806
                        tile_size_in_pixels: [3, 2].into(),
3✔
807
                    },
3✔
808
                    0,
3✔
809
                    Grid2D::new([3, 2].into(), vec![1, 2, 3, 4, 5, 6])
3✔
810
                        .unwrap()
3✔
811
                        .into(),
3✔
812
                    CacheHint::default(),
3✔
813
                )],
3✔
814
                result_descriptor: RasterResultDescriptor {
3✔
815
                    data_type: RasterDataType::U8,
3✔
816
                    spatial_reference: SpatialReference::epsg_4326().into(),
3✔
817
                    time: None,
3✔
818
                    bbox: None,
3✔
819
                    resolution: None,
3✔
820
                    bands: RasterBandDescriptors::new_single_band(),
3✔
821
                },
3✔
822
            },
3✔
823
        }
3✔
824
        .boxed()
3✔
825
    }
3✔
826

827
    #[tokio::test]
828
    async fn simple_raster() {
1✔
829
        let tile_size_in_pixels = [3, 2].into();
1✔
830
        let tiling_specification = TilingSpecification {
1✔
831
            origin_coordinate: [0.0, 0.0].into(),
1✔
832
            tile_size_in_pixels,
1✔
833
        };
1✔
834
        let execution_context = MockExecutionContext::new_with_tiling_spec(tiling_specification);
1✔
835

1✔
836
        let histogram = Histogram {
1✔
837
            params: HistogramParams {
1✔
838
                attribute_name: "band".to_string(),
1✔
839
                bounds: HistogramBounds::Values { min: 0.0, max: 8.0 },
1✔
840
                buckets: HistogramBuckets::Number { value: 3 },
1✔
841
                interactive: false,
1✔
842
            },
1✔
843
            sources: mock_raster_source().into(),
1✔
844
        };
1✔
845

1✔
846
        let query_processor = histogram
1✔
847
            .boxed()
1✔
848
            .initialize(WorkflowOperatorPath::initialize_root(), &execution_context)
1✔
849
            .await
1✔
850
            .unwrap()
1✔
851
            .query_processor()
1✔
852
            .unwrap()
1✔
853
            .json_vega()
1✔
854
            .unwrap();
1✔
855

1✔
856
        let result = query_processor
1✔
857
            .plot_query(
1✔
858
                PlotQueryRectangle {
1✔
859
                    spatial_bounds: BoundingBox2D::new((0., -3.).into(), (2., 0.).into()).unwrap(),
1✔
860
                    time_interval: TimeInterval::default(),
1✔
861
                    spatial_resolution: SpatialResolution::one(),
1✔
862
                    attributes: PlotSeriesSelection::all(),
1✔
863
                },
1✔
864
                &MockQueryContext::new(ChunkByteSize::MIN),
1✔
865
            )
1✔
866
            .await
1✔
867
            .unwrap();
1✔
868

1✔
869
        assert_eq!(
1✔
870
            result,
1✔
871
            geoengine_datatypes::plots::Histogram::builder(3, 0., 8., Measurement::Unitless)
1✔
872
                .counts(vec![2, 3, 1])
1✔
873
                .build()
1✔
874
                .unwrap()
1✔
875
                .to_vega_embeddable(false)
1✔
876
                .unwrap()
1✔
877
        );
1✔
878
    }
1✔
879

880
    #[tokio::test]
881
    async fn simple_raster_without_spec() {
1✔
882
        let tile_size_in_pixels = [3, 2].into();
1✔
883
        let tiling_specification = TilingSpecification {
1✔
884
            origin_coordinate: [0.0, 0.0].into(),
1✔
885
            tile_size_in_pixels,
1✔
886
        };
1✔
887
        let execution_context = MockExecutionContext::new_with_tiling_spec(tiling_specification);
1✔
888

1✔
889
        let histogram = Histogram {
1✔
890
            params: HistogramParams {
1✔
891
                attribute_name: "band".to_string(),
1✔
892
                bounds: HistogramBounds::Data(Default::default()),
1✔
893
                buckets: HistogramBuckets::SquareRootChoiceRule {
1✔
894
                    max_number_of_buckets: 100,
1✔
895
                },
1✔
896
                interactive: false,
1✔
897
            },
1✔
898
            sources: mock_raster_source().into(),
1✔
899
        };
1✔
900

1✔
901
        let query_processor = histogram
1✔
902
            .boxed()
1✔
903
            .initialize(WorkflowOperatorPath::initialize_root(), &execution_context)
1✔
904
            .await
1✔
905
            .unwrap()
1✔
906
            .query_processor()
1✔
907
            .unwrap()
1✔
908
            .json_vega()
1✔
909
            .unwrap();
1✔
910

1✔
911
        let result = query_processor
1✔
912
            .plot_query(
1✔
913
                PlotQueryRectangle {
1✔
914
                    spatial_bounds: BoundingBox2D::new((0., -3.).into(), (2., 0.).into()).unwrap(),
1✔
915
                    time_interval: TimeInterval::default(),
1✔
916
                    spatial_resolution: SpatialResolution::one(),
1✔
917
                    attributes: PlotSeriesSelection::all(),
1✔
918
                },
1✔
919
                &MockQueryContext::new(ChunkByteSize::MIN),
1✔
920
            )
1✔
921
            .await
1✔
922
            .unwrap();
1✔
923

1✔
924
        assert_eq!(
1✔
925
            result,
1✔
926
            geoengine_datatypes::plots::Histogram::builder(2, 1., 6., Measurement::Unitless)
1✔
927
                .counts(vec![3, 3])
1✔
928
                .build()
1✔
929
                .unwrap()
1✔
930
                .to_vega_embeddable(false)
1✔
931
                .unwrap()
1✔
932
        );
1✔
933
    }
1✔
934

935
    #[tokio::test]
936
    async fn vector_data() {
1✔
937
        let vector_source = MockFeatureCollectionSource::multiple(vec![
1✔
938
            DataCollection::from_slices(
1✔
939
                &[] as &[NoGeometry],
1✔
940
                &[TimeInterval::default(); 8],
1✔
941
                &[("foo", FeatureData::Int(vec![1, 1, 2, 2, 3, 3, 4, 4]))],
1✔
942
            )
1✔
943
            .unwrap(),
1✔
944
            DataCollection::from_slices(
1✔
945
                &[] as &[NoGeometry],
1✔
946
                &[TimeInterval::default(); 4],
1✔
947
                &[("foo", FeatureData::Int(vec![5, 6, 7, 8]))],
1✔
948
            )
1✔
949
            .unwrap(),
1✔
950
        ])
1✔
951
        .boxed();
1✔
952

1✔
953
        let histogram = Histogram {
1✔
954
            params: HistogramParams {
1✔
955
                attribute_name: "foo".to_string(),
1✔
956
                bounds: HistogramBounds::Values { min: 0.0, max: 8.0 },
1✔
957
                buckets: HistogramBuckets::Number { value: 3 },
1✔
958
                interactive: true,
1✔
959
            },
1✔
960
            sources: vector_source.into(),
1✔
961
        };
1✔
962

1✔
963
        let execution_context = MockExecutionContext::test_default();
1✔
964

1✔
965
        let query_processor = histogram
1✔
966
            .boxed()
1✔
967
            .initialize(WorkflowOperatorPath::initialize_root(), &execution_context)
1✔
968
            .await
1✔
969
            .unwrap()
1✔
970
            .query_processor()
1✔
971
            .unwrap()
1✔
972
            .json_vega()
1✔
973
            .unwrap();
1✔
974

1✔
975
        let result = query_processor
1✔
976
            .plot_query(
1✔
977
                PlotQueryRectangle {
1✔
978
                    spatial_bounds: BoundingBox2D::new((-180., -90.).into(), (180., 90.).into())
1✔
979
                        .unwrap(),
1✔
980
                    time_interval: TimeInterval::default(),
1✔
981
                    spatial_resolution: SpatialResolution::one(),
1✔
982
                    attributes: PlotSeriesSelection::all(),
1✔
983
                },
1✔
984
                &MockQueryContext::new(ChunkByteSize::MIN),
1✔
985
            )
1✔
986
            .await
1✔
987
            .unwrap();
1✔
988

1✔
989
        assert_eq!(
1✔
990
            result,
1✔
991
            geoengine_datatypes::plots::Histogram::builder(3, 0., 8., Measurement::Unitless)
1✔
992
                .counts(vec![4, 5, 3])
1✔
993
                .build()
1✔
994
                .unwrap()
1✔
995
                .to_vega_embeddable(true)
1✔
996
                .unwrap()
1✔
997
        );
1✔
998
    }
1✔
999

1000
    #[tokio::test]
1001
    async fn vector_data_with_nulls() {
1✔
1002
        let vector_source = MockFeatureCollectionSource::single(
1✔
1003
            DataCollection::from_slices(
1✔
1004
                &[] as &[NoGeometry],
1✔
1005
                &[TimeInterval::default(); 6],
1✔
1006
                &[(
1✔
1007
                    "foo",
1✔
1008
                    FeatureData::NullableFloat(vec![
1✔
1009
                        Some(1.),
1✔
1010
                        Some(2.),
1✔
1011
                        None,
1✔
1012
                        Some(4.),
1✔
1013
                        None,
1✔
1014
                        Some(5.),
1✔
1015
                    ]),
1✔
1016
                )],
1✔
1017
            )
1✔
1018
            .unwrap(),
1✔
1019
        )
1✔
1020
        .boxed();
1✔
1021

1✔
1022
        let histogram = Histogram {
1✔
1023
            params: HistogramParams {
1✔
1024
                attribute_name: "foo".to_string(),
1✔
1025
                bounds: HistogramBounds::Data(Default::default()),
1✔
1026
                buckets: HistogramBuckets::SquareRootChoiceRule {
1✔
1027
                    max_number_of_buckets: 100,
1✔
1028
                },
1✔
1029
                interactive: false,
1✔
1030
            },
1✔
1031
            sources: vector_source.into(),
1✔
1032
        };
1✔
1033

1✔
1034
        let execution_context = MockExecutionContext::test_default();
1✔
1035

1✔
1036
        let query_processor = histogram
1✔
1037
            .boxed()
1✔
1038
            .initialize(WorkflowOperatorPath::initialize_root(), &execution_context)
1✔
1039
            .await
1✔
1040
            .unwrap()
1✔
1041
            .query_processor()
1✔
1042
            .unwrap()
1✔
1043
            .json_vega()
1✔
1044
            .unwrap();
1✔
1045

1✔
1046
        let result = query_processor
1✔
1047
            .plot_query(
1✔
1048
                PlotQueryRectangle {
1✔
1049
                    spatial_bounds: BoundingBox2D::new((-180., -90.).into(), (180., 90.).into())
1✔
1050
                        .unwrap(),
1✔
1051
                    time_interval: TimeInterval::default(),
1✔
1052
                    spatial_resolution: SpatialResolution::one(),
1✔
1053
                    attributes: PlotSeriesSelection::all(),
1✔
1054
                },
1✔
1055
                &MockQueryContext::new(ChunkByteSize::MIN),
1✔
1056
            )
1✔
1057
            .await
1✔
1058
            .unwrap();
1✔
1059

1✔
1060
        assert_eq!(
1✔
1061
            result,
1✔
1062
            geoengine_datatypes::plots::Histogram::builder(2, 1., 5., Measurement::Unitless)
1✔
1063
                .counts(vec![2, 2])
1✔
1064
                .build()
1✔
1065
                .unwrap()
1✔
1066
                .to_vega_embeddable(false)
1✔
1067
                .unwrap()
1✔
1068
        );
1✔
1069
    }
1✔
1070

1071
    #[tokio::test]
1072
    #[allow(clippy::too_many_lines)]
1073
    async fn text_attribute() {
1✔
1074
        let dataset_id = DatasetId::new();
1✔
1075
        let dataset_name = NamedData::with_system_name("ne_10m_ports");
1✔
1076

1✔
1077
        let workflow = serde_json::json!({
1✔
1078
            "type": "Histogram",
1✔
1079
            "params": {
1✔
1080
                "attributeName": "featurecla",
1✔
1081
                "bounds": "data",
1✔
1082
                "buckets": {
1✔
1083
                    "type": "squareRootChoiceRule",
1✔
1084
                    "maxNumberOfBuckets": 100,
1✔
1085
                }
1✔
1086
            },
1✔
1087
            "sources": {
1✔
1088
                "source": {
1✔
1089
                    "type": "OgrSource",
1✔
1090
                    "params": {
1✔
1091
                        "data": dataset_name.clone(),
1✔
1092
                        "attributeProjection": null
1✔
1093
                    },
1✔
1094
                }
1✔
1095
            }
1✔
1096
        });
1✔
1097
        let histogram: Histogram = serde_json::from_value(workflow).unwrap();
1✔
1098

1✔
1099
        let mut execution_context = MockExecutionContext::test_default();
1✔
1100
        execution_context.add_meta_data::<_, _, VectorQueryRectangle>(
1✔
1101
            DataId::Internal { dataset_id },
1✔
1102
            dataset_name,
1✔
1103
            Box::new(StaticMetaData {
1✔
1104
                loading_info: OgrSourceDataset {
1✔
1105
                    file_name: test_data!("vector/data/ne_10m_ports/ne_10m_ports.shp").into(),
1✔
1106
                    layer_name: "ne_10m_ports".to_string(),
1✔
1107
                    data_type: Some(VectorDataType::MultiPoint),
1✔
1108
                    time: OgrSourceDatasetTimeType::None,
1✔
1109
                    default_geometry: None,
1✔
1110
                    columns: Some(OgrSourceColumnSpec {
1✔
1111
                        format_specifics: None,
1✔
1112
                        x: String::new(),
1✔
1113
                        y: None,
1✔
1114
                        int: vec!["natlscale".to_string()],
1✔
1115
                        float: vec!["scalerank".to_string()],
1✔
1116
                        text: vec![
1✔
1117
                            "featurecla".to_string(),
1✔
1118
                            "name".to_string(),
1✔
1119
                            "website".to_string(),
1✔
1120
                        ],
1✔
1121
                        bool: vec![],
1✔
1122
                        datetime: vec![],
1✔
1123
                        rename: None,
1✔
1124
                    }),
1✔
1125
                    force_ogr_time_filter: false,
1✔
1126
                    force_ogr_spatial_filter: false,
1✔
1127
                    on_error: OgrSourceErrorSpec::Ignore,
1✔
1128
                    sql_query: None,
1✔
1129
                    attribute_query: None,
1✔
1130
                    cache_ttl: CacheTtlSeconds::default(),
1✔
1131
                },
1✔
1132
                result_descriptor: VectorResultDescriptor {
1✔
1133
                    data_type: VectorDataType::MultiPoint,
1✔
1134
                    spatial_reference: SpatialReference::epsg_4326().into(),
1✔
1135
                    columns: [
1✔
1136
                        (
1✔
1137
                            "natlscale".to_string(),
1✔
1138
                            VectorColumnInfo {
1✔
1139
                                data_type: FeatureDataType::Float,
1✔
1140
                                measurement: Measurement::Unitless,
1✔
1141
                            },
1✔
1142
                        ),
1✔
1143
                        (
1✔
1144
                            "scalerank".to_string(),
1✔
1145
                            VectorColumnInfo {
1✔
1146
                                data_type: FeatureDataType::Int,
1✔
1147
                                measurement: Measurement::Unitless,
1✔
1148
                            },
1✔
1149
                        ),
1✔
1150
                        (
1✔
1151
                            "featurecla".to_string(),
1✔
1152
                            VectorColumnInfo {
1✔
1153
                                data_type: FeatureDataType::Text,
1✔
1154
                                measurement: Measurement::Unitless,
1✔
1155
                            },
1✔
1156
                        ),
1✔
1157
                        (
1✔
1158
                            "name".to_string(),
1✔
1159
                            VectorColumnInfo {
1✔
1160
                                data_type: FeatureDataType::Text,
1✔
1161
                                measurement: Measurement::Unitless,
1✔
1162
                            },
1✔
1163
                        ),
1✔
1164
                        (
1✔
1165
                            "website".to_string(),
1✔
1166
                            VectorColumnInfo {
1✔
1167
                                data_type: FeatureDataType::Text,
1✔
1168
                                measurement: Measurement::Unitless,
1✔
1169
                            },
1✔
1170
                        ),
1✔
1171
                    ]
1✔
1172
                    .iter()
1✔
1173
                    .cloned()
1✔
1174
                    .collect(),
1✔
1175
                    time: None,
1✔
1176
                    bbox: None,
1✔
1177
                },
1✔
1178
                phantom: Default::default(),
1✔
1179
            }),
1✔
1180
        );
1✔
1181

1✔
1182
        if let Err(Error::InvalidOperatorSpec { reason }) = histogram
1✔
1183
            .boxed()
1✔
1184
            .initialize(WorkflowOperatorPath::initialize_root(), &execution_context)
1✔
1185
            .await
1✔
1186
        {
1✔
1187
            assert_eq!(reason, "column `featurecla` must be numerical");
1✔
1188
        } else {
1✔
1189
            panic!("we currently don't support text features, but this went through");
1✔
1190
        }
1✔
1191
    }
1✔
1192

1193
    #[tokio::test]
1194
    async fn no_data_raster() {
1✔
1195
        let tile_size_in_pixels = [3, 2].into();
1✔
1196
        let tiling_specification = TilingSpecification {
1✔
1197
            origin_coordinate: [0.0, 0.0].into(),
1✔
1198
            tile_size_in_pixels,
1✔
1199
        };
1✔
1200
        let execution_context = MockExecutionContext::new_with_tiling_spec(tiling_specification);
1✔
1201
        let histogram = Histogram {
1✔
1202
            params: HistogramParams {
1✔
1203
                attribute_name: "band".to_string(),
1✔
1204
                bounds: HistogramBounds::Data(Data),
1✔
1205
                buckets: HistogramBuckets::SquareRootChoiceRule {
1✔
1206
                    max_number_of_buckets: 100,
1✔
1207
                },
1✔
1208
                interactive: false,
1✔
1209
            },
1✔
1210
            sources: MockRasterSource {
1✔
1211
                params: MockRasterSourceParams {
1✔
1212
                    data: vec![RasterTile2D::new_with_tile_info(
1✔
1213
                        TimeInterval::default(),
1✔
1214
                        TileInformation {
1✔
1215
                            global_geo_transform: TestDefault::test_default(),
1✔
1216
                            global_tile_position: [0, 0].into(),
1✔
1217
                            tile_size_in_pixels,
1✔
1218
                        },
1✔
1219
                        0,
1✔
1220
                        EmptyGrid2D::<u8>::new(tile_size_in_pixels).into(),
1✔
1221
                        CacheHint::default(),
1✔
1222
                    )],
1✔
1223
                    result_descriptor: RasterResultDescriptor {
1✔
1224
                        data_type: RasterDataType::U8,
1✔
1225
                        spatial_reference: SpatialReference::epsg_4326().into(),
1✔
1226
                        time: None,
1✔
1227
                        bbox: None,
1✔
1228
                        resolution: None,
1✔
1229
                        bands: RasterBandDescriptors::new_single_band(),
1✔
1230
                    },
1✔
1231
                },
1✔
1232
            }
1✔
1233
            .boxed()
1✔
1234
            .into(),
1✔
1235
        };
1✔
1236

1✔
1237
        let query_processor = histogram
1✔
1238
            .boxed()
1✔
1239
            .initialize(WorkflowOperatorPath::initialize_root(), &execution_context)
1✔
1240
            .await
1✔
1241
            .unwrap()
1✔
1242
            .query_processor()
1✔
1243
            .unwrap()
1✔
1244
            .json_vega()
1✔
1245
            .unwrap();
1✔
1246

1✔
1247
        let result = query_processor
1✔
1248
            .plot_query(
1✔
1249
                PlotQueryRectangle {
1✔
1250
                    spatial_bounds: BoundingBox2D::new((0., -3.).into(), (2., 0.).into()).unwrap(),
1✔
1251
                    time_interval: TimeInterval::default(),
1✔
1252
                    spatial_resolution: SpatialResolution::one(),
1✔
1253
                    attributes: PlotSeriesSelection::all(),
1✔
1254
                },
1✔
1255
                &MockQueryContext::new(ChunkByteSize::MIN),
1✔
1256
            )
1✔
1257
            .await
1✔
1258
            .unwrap();
1✔
1259

1✔
1260
        assert_eq!(
1✔
1261
            result,
1✔
1262
            geoengine_datatypes::plots::Histogram::builder(1, 0., 0., Measurement::Unitless)
1✔
1263
                .build()
1✔
1264
                .unwrap()
1✔
1265
                .to_vega_embeddable(false)
1✔
1266
                .unwrap()
1✔
1267
        );
1✔
1268
    }
1✔
1269

1270
    #[tokio::test]
1271
    async fn empty_feature_collection() {
1✔
1272
        let vector_source = MockFeatureCollectionSource::single(
1✔
1273
            DataCollection::from_slices(
1✔
1274
                &[] as &[NoGeometry],
1✔
1275
                &[] as &[TimeInterval],
1✔
1276
                &[("foo", FeatureData::Float(vec![]))],
1✔
1277
            )
1✔
1278
            .unwrap(),
1✔
1279
        )
1✔
1280
        .boxed();
1✔
1281

1✔
1282
        let histogram = Histogram {
1✔
1283
            params: HistogramParams {
1✔
1284
                attribute_name: "foo".to_string(),
1✔
1285
                bounds: HistogramBounds::Data(Default::default()),
1✔
1286
                buckets: HistogramBuckets::SquareRootChoiceRule {
1✔
1287
                    max_number_of_buckets: 100,
1✔
1288
                },
1✔
1289
                interactive: false,
1✔
1290
            },
1✔
1291
            sources: vector_source.into(),
1✔
1292
        };
1✔
1293

1✔
1294
        let execution_context = MockExecutionContext::test_default();
1✔
1295

1✔
1296
        let query_processor = histogram
1✔
1297
            .boxed()
1✔
1298
            .initialize(WorkflowOperatorPath::initialize_root(), &execution_context)
1✔
1299
            .await
1✔
1300
            .unwrap()
1✔
1301
            .query_processor()
1✔
1302
            .unwrap()
1✔
1303
            .json_vega()
1✔
1304
            .unwrap();
1✔
1305

1✔
1306
        let result = query_processor
1✔
1307
            .plot_query(
1✔
1308
                PlotQueryRectangle {
1✔
1309
                    spatial_bounds: BoundingBox2D::new((-180., -90.).into(), (180., 90.).into())
1✔
1310
                        .unwrap(),
1✔
1311
                    time_interval: TimeInterval::default(),
1✔
1312
                    spatial_resolution: SpatialResolution::one(),
1✔
1313
                    attributes: PlotSeriesSelection::all(),
1✔
1314
                },
1✔
1315
                &MockQueryContext::new(ChunkByteSize::MIN),
1✔
1316
            )
1✔
1317
            .await
1✔
1318
            .unwrap();
1✔
1319

1✔
1320
        assert_eq!(
1✔
1321
            result,
1✔
1322
            geoengine_datatypes::plots::Histogram::builder(1, 0., 0., Measurement::Unitless)
1✔
1323
                .build()
1✔
1324
                .unwrap()
1✔
1325
                .to_vega_embeddable(false)
1✔
1326
                .unwrap()
1✔
1327
        );
1✔
1328
    }
1✔
1329

1330
    #[tokio::test]
1331
    async fn feature_collection_with_one_feature() {
1✔
1332
        let vector_source = MockFeatureCollectionSource::with_collections_and_measurements(
1✔
1333
            vec![DataCollection::from_slices(
1✔
1334
                &[] as &[NoGeometry],
1✔
1335
                &[TimeInterval::default()],
1✔
1336
                &[("foo", FeatureData::Float(vec![5.0]))],
1✔
1337
            )
1✔
1338
            .unwrap()],
1✔
1339
            [(
1✔
1340
                "foo".to_string(),
1✔
1341
                Measurement::continuous("bar".to_string(), None),
1✔
1342
            )]
1✔
1343
            .into_iter()
1✔
1344
            .collect(),
1✔
1345
        )
1✔
1346
        .boxed();
1✔
1347

1✔
1348
        let histogram = Histogram {
1✔
1349
            params: HistogramParams {
1✔
1350
                attribute_name: "foo".to_string(),
1✔
1351
                bounds: HistogramBounds::Data(Default::default()),
1✔
1352
                buckets: HistogramBuckets::SquareRootChoiceRule {
1✔
1353
                    max_number_of_buckets: 100,
1✔
1354
                },
1✔
1355
                interactive: false,
1✔
1356
            },
1✔
1357
            sources: vector_source.into(),
1✔
1358
        };
1✔
1359

1✔
1360
        let execution_context = MockExecutionContext::test_default();
1✔
1361

1✔
1362
        let query_processor = histogram
1✔
1363
            .boxed()
1✔
1364
            .initialize(WorkflowOperatorPath::initialize_root(), &execution_context)
1✔
1365
            .await
1✔
1366
            .unwrap()
1✔
1367
            .query_processor()
1✔
1368
            .unwrap()
1✔
1369
            .json_vega()
1✔
1370
            .unwrap();
1✔
1371

1✔
1372
        let result = query_processor
1✔
1373
            .plot_query(
1✔
1374
                PlotQueryRectangle {
1✔
1375
                    spatial_bounds: BoundingBox2D::new((-180., -90.).into(), (180., 90.).into())
1✔
1376
                        .unwrap(),
1✔
1377
                    time_interval: TimeInterval::default(),
1✔
1378
                    spatial_resolution: SpatialResolution::one(),
1✔
1379
                    attributes: PlotSeriesSelection::all(),
1✔
1380
                },
1✔
1381
                &MockQueryContext::new(ChunkByteSize::MIN),
1✔
1382
            )
1✔
1383
            .await
1✔
1384
            .unwrap();
1✔
1385

1✔
1386
        assert_eq!(
1✔
1387
            result,
1✔
1388
            geoengine_datatypes::plots::Histogram::builder(
1✔
1389
                1,
1✔
1390
                5.,
1✔
1391
                5.,
1✔
1392
                Measurement::continuous("bar".to_string(), None)
1✔
1393
            )
1✔
1394
            .counts(vec![1])
1✔
1395
            .build()
1✔
1396
            .unwrap()
1✔
1397
            .to_vega_embeddable(false)
1✔
1398
            .unwrap()
1✔
1399
        );
1✔
1400
    }
1✔
1401

1402
    #[tokio::test]
1403
    async fn single_value_raster_stream() {
1✔
1404
        let tile_size_in_pixels = [3, 2].into();
1✔
1405
        let tiling_specification = TilingSpecification {
1✔
1406
            origin_coordinate: [0.0, 0.0].into(),
1✔
1407
            tile_size_in_pixels,
1✔
1408
        };
1✔
1409
        let execution_context = MockExecutionContext::new_with_tiling_spec(tiling_specification);
1✔
1410
        let histogram = Histogram {
1✔
1411
            params: HistogramParams {
1✔
1412
                attribute_name: "band".to_string(),
1✔
1413
                bounds: HistogramBounds::Data(Data),
1✔
1414
                buckets: HistogramBuckets::SquareRootChoiceRule {
1✔
1415
                    max_number_of_buckets: 100,
1✔
1416
                },
1✔
1417
                interactive: false,
1✔
1418
            },
1✔
1419
            sources: MockRasterSource {
1✔
1420
                params: MockRasterSourceParams {
1✔
1421
                    data: vec![RasterTile2D::new_with_tile_info(
1✔
1422
                        TimeInterval::default(),
1✔
1423
                        TileInformation {
1✔
1424
                            global_geo_transform: TestDefault::test_default(),
1✔
1425
                            global_tile_position: [0, 0].into(),
1✔
1426
                            tile_size_in_pixels,
1✔
1427
                        },
1✔
1428
                        0,
1✔
1429
                        Grid2D::new(tile_size_in_pixels, vec![4; 6]).unwrap().into(),
1✔
1430
                        CacheHint::default(),
1✔
1431
                    )],
1✔
1432
                    result_descriptor: RasterResultDescriptor {
1✔
1433
                        data_type: RasterDataType::U8,
1✔
1434
                        spatial_reference: SpatialReference::epsg_4326().into(),
1✔
1435
                        time: None,
1✔
1436
                        bbox: None,
1✔
1437
                        resolution: None,
1✔
1438
                        bands: RasterBandDescriptors::new_single_band(),
1✔
1439
                    },
1✔
1440
                },
1✔
1441
            }
1✔
1442
            .boxed()
1✔
1443
            .into(),
1✔
1444
        };
1✔
1445

1✔
1446
        let query_processor = histogram
1✔
1447
            .boxed()
1✔
1448
            .initialize(WorkflowOperatorPath::initialize_root(), &execution_context)
1✔
1449
            .await
1✔
1450
            .unwrap()
1✔
1451
            .query_processor()
1✔
1452
            .unwrap()
1✔
1453
            .json_vega()
1✔
1454
            .unwrap();
1✔
1455

1✔
1456
        let result = query_processor
1✔
1457
            .plot_query(
1✔
1458
                PlotQueryRectangle {
1✔
1459
                    spatial_bounds: BoundingBox2D::new((0., -3.).into(), (2., 0.).into()).unwrap(),
1✔
1460
                    time_interval: TimeInterval::new_instant(DateTime::new_utc(
1✔
1461
                        2013, 12, 1, 12, 0, 0,
1✔
1462
                    ))
1✔
1463
                    .unwrap(),
1✔
1464
                    spatial_resolution: SpatialResolution::one(),
1✔
1465
                    attributes: PlotSeriesSelection::all(),
1✔
1466
                },
1✔
1467
                &MockQueryContext::new(ChunkByteSize::MIN),
1✔
1468
            )
1✔
1469
            .await
1✔
1470
            .unwrap();
1✔
1471

1✔
1472
        assert_eq!(
1✔
1473
            result,
1✔
1474
            geoengine_datatypes::plots::Histogram::builder(1, 4., 4., Measurement::Unitless)
1✔
1475
                .counts(vec![6])
1✔
1476
                .build()
1✔
1477
                .unwrap()
1✔
1478
                .to_vega_embeddable(false)
1✔
1479
                .unwrap()
1✔
1480
        );
1✔
1481
    }
1✔
1482
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc