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

geo-engine / geoengine / 5006008836

pending completion
5006008836

push

github

GitHub
Merge #785 #787

936 of 936 new or added lines in 50 files covered. (100.0%)

96010 of 107707 relevant lines covered (89.14%)

72676.46 hits per line

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

88.63
/operators/src/processing/reprojection.rs
1
use std::marker::PhantomData;
2

3
use super::map_query::MapQueryProcessor;
4
use crate::{
5
    adapters::{
6
        fold_by_coordinate_lookup_future, RasterSubQueryAdapter, SparseTilesFillAdapter,
7
        TileReprojectionSubQuery,
8
    },
9
    engine::{
10
        CanonicOperatorName, ExecutionContext, InitializedRasterOperator, InitializedSources,
11
        InitializedVectorOperator, Operator, OperatorName, QueryContext, QueryProcessor,
12
        RasterOperator, RasterQueryProcessor, RasterResultDescriptor, SingleRasterOrVectorSource,
13
        TypedRasterQueryProcessor, TypedVectorQueryProcessor, VectorOperator, VectorQueryProcessor,
14
        VectorResultDescriptor, WorkflowOperatorPath,
15
    },
16
    error::{self, Error},
17
    util::Result,
18
};
19
use async_trait::async_trait;
20
use futures::stream::BoxStream;
21
use futures::{stream, StreamExt};
22
use geoengine_datatypes::{
23
    collections::FeatureCollection,
24
    operations::reproject::{
25
        reproject_and_unify_bbox, reproject_query, suggest_pixel_size_from_diag_cross_projected,
26
        CoordinateProjection, CoordinateProjector, Reproject, ReprojectClipped,
27
    },
28
    primitives::{
29
        BoundingBox2D, Geometry, RasterQueryRectangle, SpatialPartition2D, SpatialPartitioned,
30
        SpatialResolution, VectorQueryRectangle,
31
    },
32
    raster::{Pixel, RasterTile2D, TilingSpecification},
33
    spatial_reference::SpatialReference,
34
    util::arrow::ArrowTyped,
35
};
36
use serde::{Deserialize, Serialize};
37

38
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
13✔
39
#[serde(rename_all = "camelCase")]
40
pub struct ReprojectionParams {
41
    pub target_spatial_reference: SpatialReference,
42
}
43

44
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
×
45
pub struct ReprojectionBounds {
46
    valid_in_bounds: SpatialPartition2D,
47
    valid_out_bounds: SpatialPartition2D,
48
}
49

50
pub type Reprojection = Operator<ReprojectionParams, SingleRasterOrVectorSource>;
51

52
impl Reprojection {}
53

54
impl OperatorName for Reprojection {
55
    const TYPE_NAME: &'static str = "Reprojection";
56
}
57

58
pub struct InitializedVectorReprojection {
59
    name: CanonicOperatorName,
60
    result_descriptor: VectorResultDescriptor,
61
    source: Box<dyn InitializedVectorOperator>,
62
    source_srs: SpatialReference,
63
    target_srs: SpatialReference,
64
}
65

66
pub struct InitializedRasterReprojection {
67
    name: CanonicOperatorName,
68
    result_descriptor: RasterResultDescriptor,
69
    source: Box<dyn InitializedRasterOperator>,
70
    state: Option<ReprojectionBounds>,
71
    source_srs: SpatialReference,
72
    target_srs: SpatialReference,
73
    tiling_spec: TilingSpecification,
74
}
75

76
impl InitializedVectorReprojection {
77
    /// Create a new `InitializedVectorReprojection` instance.
78
    /// The `source` must have the same `SpatialReference` as `source_srs`.
79
    ///
80
    /// # Errors
81
    /// This function errors if the `source`'s `SpatialReference` is `None`.
82
    /// This function errors if the source's bounding box cannot be reprojected to the target's `SpatialReference`.
83
    pub fn try_new_with_input(
6✔
84
        name: CanonicOperatorName,
6✔
85
        params: ReprojectionParams,
6✔
86
        source_vector_operator: Box<dyn InitializedVectorOperator>,
6✔
87
    ) -> Result<Self> {
6✔
88
        let in_desc: VectorResultDescriptor = source_vector_operator.result_descriptor().clone();
6✔
89

90
        let in_srs = Into::<Option<SpatialReference>>::into(in_desc.spatial_reference)
6✔
91
            .ok_or(Error::AllSourcesMustHaveSameSpatialReference)?;
6✔
92

93
        let bbox = if let Some(bbox) = in_desc.bbox {
6✔
94
            let projector =
×
95
                CoordinateProjector::from_known_srs(in_srs, params.target_spatial_reference)?;
×
96

97
            bbox.reproject_clipped(&projector)? // TODO: if this is none then we could skip the whole reprojection similar to raster?
×
98
        } else {
99
            None
6✔
100
        };
101

102
        let out_desc = VectorResultDescriptor {
6✔
103
            spatial_reference: params.target_spatial_reference.into(),
6✔
104
            data_type: in_desc.data_type,
6✔
105
            columns: in_desc.columns.clone(),
6✔
106
            time: in_desc.time,
6✔
107
            bbox,
6✔
108
        };
6✔
109

6✔
110
        Ok(InitializedVectorReprojection {
6✔
111
            name,
6✔
112
            result_descriptor: out_desc,
6✔
113
            source: source_vector_operator,
6✔
114
            source_srs: in_srs,
6✔
115
            target_srs: params.target_spatial_reference,
6✔
116
        })
6✔
117
    }
6✔
118
}
119

120
impl InitializedRasterReprojection {
121
    pub fn try_new_with_input(
6✔
122
        name: CanonicOperatorName,
6✔
123
        params: ReprojectionParams,
6✔
124
        source_raster_operator: Box<dyn InitializedRasterOperator>,
6✔
125
        tiling_spec: TilingSpecification,
6✔
126
    ) -> Result<Self> {
6✔
127
        let in_desc: RasterResultDescriptor = source_raster_operator.result_descriptor().clone();
6✔
128

129
        let in_srs = Into::<Option<SpatialReference>>::into(in_desc.spatial_reference)
6✔
130
            .ok_or(Error::AllSourcesMustHaveSameSpatialReference)?;
6✔
131

132
        // calculate the intersection of input and output srs in both coordinate systems
133
        let (in_bounds, out_bounds, out_res) = Self::derive_raster_in_bounds_out_bounds_out_res(
6✔
134
            in_srs,
6✔
135
            params.target_spatial_reference,
6✔
136
            in_desc.resolution,
6✔
137
            in_desc.bbox,
6✔
138
        )?;
6✔
139

140
        let result_descriptor = RasterResultDescriptor {
4✔
141
            spatial_reference: params.target_spatial_reference.into(),
4✔
142
            data_type: in_desc.data_type,
4✔
143
            measurement: in_desc.measurement.clone(),
4✔
144
            time: in_desc.time,
4✔
145
            bbox: out_bounds,
4✔
146
            resolution: out_res,
4✔
147
        };
4✔
148

149
        let state = match (in_bounds, out_bounds) {
4✔
150
            (Some(in_bounds), Some(out_bounds)) => Some(ReprojectionBounds {
4✔
151
                valid_in_bounds: in_bounds,
4✔
152
                valid_out_bounds: out_bounds,
4✔
153
            }),
4✔
154
            _ => None,
×
155
        };
156

157
        Ok(InitializedRasterReprojection {
4✔
158
            name,
4✔
159
            result_descriptor,
4✔
160
            source: source_raster_operator,
4✔
161
            state,
4✔
162
            source_srs: in_srs,
4✔
163
            target_srs: params.target_spatial_reference,
4✔
164
            tiling_spec,
4✔
165
        })
4✔
166
    }
6✔
167

168
    fn derive_raster_in_bounds_out_bounds_out_res(
7✔
169
        source_srs: SpatialReference,
7✔
170
        target_srs: SpatialReference,
7✔
171
        source_spatial_resolution: Option<SpatialResolution>,
7✔
172
        source_bbox: Option<SpatialPartition2D>,
7✔
173
    ) -> Result<(
7✔
174
        Option<SpatialPartition2D>,
7✔
175
        Option<SpatialPartition2D>,
7✔
176
        Option<SpatialResolution>,
7✔
177
    )> {
7✔
178
        let (in_bbox, out_bbox) = if let Some(bbox) = source_bbox {
7✔
179
            reproject_and_unify_bbox(bbox, source_srs, target_srs)?
4✔
180
        } else {
181
            // use the parts of the area of use that are valid in both spatial references
182
            let valid_bounds_in = source_srs.area_of_use_intersection(&target_srs)?;
3✔
183
            let valid_bounds_out = target_srs.area_of_use_intersection(&source_srs)?;
3✔
184

185
            (valid_bounds_in, valid_bounds_out)
3✔
186
        };
187

188
        let out_res = match (source_spatial_resolution, in_bbox, out_bbox) {
5✔
189
            (Some(in_res), Some(in_bbox), Some(out_bbox)) => {
3✔
190
                suggest_pixel_size_from_diag_cross_projected(in_bbox, out_bbox, in_res).ok()
3✔
191
            }
192
            _ => None,
2✔
193
        };
194

195
        Ok((in_bbox, out_bbox, out_res))
5✔
196
    }
7✔
197
}
198

199
#[typetag::serde]
1✔
200
#[async_trait]
201
impl VectorOperator for Reprojection {
202
    async fn _initialize(
7✔
203
        self: Box<Self>,
7✔
204
        path: WorkflowOperatorPath,
7✔
205
        context: &dyn ExecutionContext,
7✔
206
    ) -> Result<Box<dyn InitializedVectorOperator>> {
7✔
207
        let name = CanonicOperatorName::from(&self);
7✔
208

209
        let vector_source =
6✔
210
            self.sources
7✔
211
                .vector()
7✔
212
                .ok_or_else(|| error::Error::InvalidOperatorType {
7✔
213
                    expected: "Vector".to_owned(),
1✔
214
                    found: "Raster".to_owned(),
1✔
215
                })?;
7✔
216

217
        let initilaized_source = vector_source.initialize_sources(path, context).await?;
6✔
218

219
        let initialized_operator = InitializedVectorReprojection::try_new_with_input(
6✔
220
            name,
6✔
221
            self.params,
6✔
222
            initilaized_source.vector,
6✔
223
        )?;
6✔
224

225
        Ok(initialized_operator.boxed())
6✔
226
    }
14✔
227

228
    span_fn!(Reprojection);
×
229
}
230

231
impl InitializedVectorOperator for InitializedVectorReprojection {
232
    fn result_descriptor(&self) -> &VectorResultDescriptor {
×
233
        &self.result_descriptor
×
234
    }
×
235

236
    fn query_processor(&self) -> Result<TypedVectorQueryProcessor> {
6✔
237
        let source_srs = self.source_srs;
6✔
238
        let target_srs = self.target_srs;
6✔
239
        match self.source.query_processor()? {
6✔
240
            TypedVectorQueryProcessor::Data(source) => Ok(TypedVectorQueryProcessor::Data(
×
241
                MapQueryProcessor::new(
×
242
                    source,
×
243
                    move |query| reproject_query(query, source_srs, target_srs).map_err(From::from),
×
244
                    (),
×
245
                )
×
246
                .boxed(),
×
247
            )),
×
248
            TypedVectorQueryProcessor::MultiPoint(source) => {
4✔
249
                Ok(TypedVectorQueryProcessor::MultiPoint(
4✔
250
                    VectorReprojectionProcessor::new(source, source_srs, target_srs).boxed(),
4✔
251
                ))
4✔
252
            }
253
            TypedVectorQueryProcessor::MultiLineString(source) => {
1✔
254
                Ok(TypedVectorQueryProcessor::MultiLineString(
1✔
255
                    VectorReprojectionProcessor::new(source, source_srs, target_srs).boxed(),
1✔
256
                ))
1✔
257
            }
258
            TypedVectorQueryProcessor::MultiPolygon(source) => {
1✔
259
                Ok(TypedVectorQueryProcessor::MultiPolygon(
1✔
260
                    VectorReprojectionProcessor::new(source, source_srs, target_srs).boxed(),
1✔
261
                ))
1✔
262
            }
263
        }
264
    }
6✔
265

266
    fn canonic_name(&self) -> CanonicOperatorName {
×
267
        self.name.clone()
×
268
    }
×
269
}
270

271
struct VectorReprojectionProcessor<Q, G>
272
where
273
    Q: VectorQueryProcessor<VectorType = FeatureCollection<G>>,
274
{
275
    source: Q,
276
    from: SpatialReference,
277
    to: SpatialReference,
278
}
279

280
impl<Q, G> VectorReprojectionProcessor<Q, G>
281
where
282
    Q: VectorQueryProcessor<VectorType = FeatureCollection<G>>,
283
{
284
    pub fn new(source: Q, from: SpatialReference, to: SpatialReference) -> Self {
6✔
285
        Self { source, from, to }
6✔
286
    }
6✔
287
}
288

289
#[async_trait]
290
impl<Q, G> QueryProcessor for VectorReprojectionProcessor<Q, G>
291
where
292
    Q: QueryProcessor<Output = FeatureCollection<G>, SpatialBounds = BoundingBox2D>,
293
    FeatureCollection<G>: Reproject<CoordinateProjector, Out = FeatureCollection<G>>,
294
    G: Geometry + ArrowTyped,
295
{
296
    type Output = FeatureCollection<G>;
297
    type SpatialBounds = BoundingBox2D;
298

299
    async fn _query<'a>(
6✔
300
        &'a self,
6✔
301
        query: VectorQueryRectangle,
6✔
302
        ctx: &'a dyn QueryContext,
6✔
303
    ) -> Result<BoxStream<'a, Result<Self::Output>>> {
6✔
304
        let rewritten_query = reproject_query(query, self.from, self.to)?;
6✔
305

306
        if let Some(rewritten_query) = rewritten_query {
6✔
307
            Ok(self
5✔
308
                .source
5✔
309
                .query(rewritten_query, ctx)
5✔
310
                .await?
×
311
                .map(move |collection_result| {
5✔
312
                    collection_result.and_then(|collection| {
5✔
313
                        CoordinateProjector::from_known_srs(self.from, self.to)
5✔
314
                            .and_then(|projector| collection.reproject(projector.as_ref()))
5✔
315
                            .map_err(Into::into)
5✔
316
                    })
5✔
317
                })
5✔
318
                .boxed())
5✔
319
        } else {
320
            let res = Ok(FeatureCollection::empty());
1✔
321
            Ok(Box::pin(stream::once(async { res })))
1✔
322
        }
323
    }
12✔
324
}
325

326
#[typetag::serde]
×
327
#[async_trait]
328
impl RasterOperator for Reprojection {
329
    async fn _initialize(
4✔
330
        self: Box<Self>,
4✔
331
        path: WorkflowOperatorPath,
4✔
332
        context: &dyn ExecutionContext,
4✔
333
    ) -> Result<Box<dyn InitializedRasterOperator>> {
4✔
334
        let name = CanonicOperatorName::from(&self);
4✔
335

336
        let raster_source =
4✔
337
            self.sources
4✔
338
                .raster()
4✔
339
                .ok_or_else(|| error::Error::InvalidOperatorType {
4✔
340
                    expected: "Raster".to_owned(),
×
341
                    found: "Vector".to_owned(),
×
342
                })?;
4✔
343

344
        let initialized_source = raster_source.initialize_sources(path, context).await?;
4✔
345

346
        let initialized_operator = InitializedRasterReprojection::try_new_with_input(
4✔
347
            name,
4✔
348
            self.params,
4✔
349
            initialized_source.raster,
4✔
350
            context.tiling_specification(),
4✔
351
        )?;
4✔
352

353
        Ok(initialized_operator.boxed())
4✔
354
    }
8✔
355

356
    span_fn!(Reprojection);
×
357
}
358

359
impl InitializedRasterOperator for InitializedRasterReprojection {
360
    fn result_descriptor(&self) -> &RasterResultDescriptor {
×
361
        &self.result_descriptor
×
362
    }
×
363

364
    // i know there is a macro somewhere. we need to re-work this when we have the no-data value anyway.
365
    #[allow(clippy::too_many_lines)]
366
    fn query_processor(&self) -> Result<TypedRasterQueryProcessor> {
4✔
367
        let q = self.source.query_processor()?;
4✔
368

369
        Ok(match self.result_descriptor.data_type {
4✔
370
            geoengine_datatypes::raster::RasterDataType::U8 => {
371
                let qt = q.get_u8().unwrap();
4✔
372
                TypedRasterQueryProcessor::U8(Box::new(RasterReprojectionProcessor::new(
4✔
373
                    qt,
4✔
374
                    self.source_srs,
4✔
375
                    self.target_srs,
4✔
376
                    self.tiling_spec,
4✔
377
                    self.state,
4✔
378
                )))
4✔
379
            }
380
            geoengine_datatypes::raster::RasterDataType::U16 => {
381
                let qt = q.get_u16().unwrap();
×
382
                TypedRasterQueryProcessor::U16(Box::new(RasterReprojectionProcessor::new(
×
383
                    qt,
×
384
                    self.source_srs,
×
385
                    self.target_srs,
×
386
                    self.tiling_spec,
×
387
                    self.state,
×
388
                )))
×
389
            }
390

391
            geoengine_datatypes::raster::RasterDataType::U32 => {
392
                let qt = q.get_u32().unwrap();
×
393
                TypedRasterQueryProcessor::U32(Box::new(RasterReprojectionProcessor::new(
×
394
                    qt,
×
395
                    self.source_srs,
×
396
                    self.target_srs,
×
397
                    self.tiling_spec,
×
398
                    self.state,
×
399
                )))
×
400
            }
401
            geoengine_datatypes::raster::RasterDataType::U64 => {
402
                let qt = q.get_u64().unwrap();
×
403
                TypedRasterQueryProcessor::U64(Box::new(RasterReprojectionProcessor::new(
×
404
                    qt,
×
405
                    self.source_srs,
×
406
                    self.target_srs,
×
407
                    self.tiling_spec,
×
408
                    self.state,
×
409
                )))
×
410
            }
411
            geoengine_datatypes::raster::RasterDataType::I8 => {
412
                let qt = q.get_i8().unwrap();
×
413
                TypedRasterQueryProcessor::I8(Box::new(RasterReprojectionProcessor::new(
×
414
                    qt,
×
415
                    self.source_srs,
×
416
                    self.target_srs,
×
417
                    self.tiling_spec,
×
418
                    self.state,
×
419
                )))
×
420
            }
421
            geoengine_datatypes::raster::RasterDataType::I16 => {
422
                let qt = q.get_i16().unwrap();
×
423
                TypedRasterQueryProcessor::I16(Box::new(RasterReprojectionProcessor::new(
×
424
                    qt,
×
425
                    self.source_srs,
×
426
                    self.target_srs,
×
427
                    self.tiling_spec,
×
428
                    self.state,
×
429
                )))
×
430
            }
431
            geoengine_datatypes::raster::RasterDataType::I32 => {
432
                let qt = q.get_i32().unwrap();
×
433
                TypedRasterQueryProcessor::I32(Box::new(RasterReprojectionProcessor::new(
×
434
                    qt,
×
435
                    self.source_srs,
×
436
                    self.target_srs,
×
437
                    self.tiling_spec,
×
438
                    self.state,
×
439
                )))
×
440
            }
441
            geoengine_datatypes::raster::RasterDataType::I64 => {
442
                let qt = q.get_i64().unwrap();
×
443
                TypedRasterQueryProcessor::I64(Box::new(RasterReprojectionProcessor::new(
×
444
                    qt,
×
445
                    self.source_srs,
×
446
                    self.target_srs,
×
447
                    self.tiling_spec,
×
448
                    self.state,
×
449
                )))
×
450
            }
451
            geoengine_datatypes::raster::RasterDataType::F32 => {
452
                let qt = q.get_f32().unwrap();
×
453
                TypedRasterQueryProcessor::F32(Box::new(RasterReprojectionProcessor::new(
×
454
                    qt,
×
455
                    self.source_srs,
×
456
                    self.target_srs,
×
457
                    self.tiling_spec,
×
458
                    self.state,
×
459
                )))
×
460
            }
461
            geoengine_datatypes::raster::RasterDataType::F64 => {
462
                let qt = q.get_f64().unwrap();
×
463
                TypedRasterQueryProcessor::F64(Box::new(RasterReprojectionProcessor::new(
×
464
                    qt,
×
465
                    self.source_srs,
×
466
                    self.target_srs,
×
467
                    self.tiling_spec,
×
468
                    self.state,
×
469
                )))
×
470
            }
471
        })
472
    }
4✔
473

474
    fn canonic_name(&self) -> CanonicOperatorName {
×
475
        self.name.clone()
×
476
    }
×
477
}
478

479
pub struct RasterReprojectionProcessor<Q, P>
480
where
481
    Q: RasterQueryProcessor<RasterType = P>,
482
{
483
    source: Q,
484
    from: SpatialReference,
485
    to: SpatialReference,
486
    tiling_spec: TilingSpecification,
487
    state: Option<ReprojectionBounds>,
488
    _phantom_data: PhantomData<P>,
489
}
490

491
impl<Q, P> RasterReprojectionProcessor<Q, P>
492
where
493
    Q: RasterQueryProcessor<RasterType = P>,
494
    P: Pixel,
495
{
496
    pub fn new(
4✔
497
        source: Q,
4✔
498
        from: SpatialReference,
4✔
499
        to: SpatialReference,
4✔
500
        tiling_spec: TilingSpecification,
4✔
501
        state: Option<ReprojectionBounds>,
4✔
502
    ) -> Self {
4✔
503
        Self {
4✔
504
            source,
4✔
505
            from,
4✔
506
            to,
4✔
507
            tiling_spec,
4✔
508
            state,
4✔
509
            _phantom_data: PhantomData,
4✔
510
        }
4✔
511
    }
4✔
512
}
513

514
#[async_trait]
515
impl<Q, P> QueryProcessor for RasterReprojectionProcessor<Q, P>
516
where
517
    Q: QueryProcessor<Output = RasterTile2D<P>, SpatialBounds = SpatialPartition2D>,
518
    P: Pixel,
519
{
520
    type Output = RasterTile2D<P>;
521
    type SpatialBounds = SpatialPartition2D;
522

523
    async fn _query<'a>(
4✔
524
        &'a self,
4✔
525
        query: RasterQueryRectangle,
4✔
526
        ctx: &'a dyn QueryContext,
4✔
527
    ) -> Result<BoxStream<'a, Result<Self::Output>>> {
4✔
528
        if let Some(state) = &self.state {
4✔
529
            let valid_bounds_in = state.valid_in_bounds;
4✔
530
            let valid_bounds_out = state.valid_out_bounds;
4✔
531

532
            // calculate the spatial resolution the input data should have using the intersection and the requested resolution
533
            let in_spatial_res = suggest_pixel_size_from_diag_cross_projected(
4✔
534
                valid_bounds_out,
4✔
535
                valid_bounds_in,
4✔
536
                query.spatial_resolution,
4✔
537
            )?;
4✔
538

539
            // setup the subquery
540
            let sub_query_spec = TileReprojectionSubQuery {
4✔
541
                in_srs: self.from,
4✔
542
                out_srs: self.to,
4✔
543
                fold_fn: fold_by_coordinate_lookup_future,
4✔
544
                in_spatial_res,
4✔
545
                valid_bounds_in,
4✔
546
                valid_bounds_out,
4✔
547
                _phantom_data: PhantomData,
4✔
548
            };
4✔
549

4✔
550
            // return the adapter which will reproject the tiles and uses the fill adapter to inject missing tiles
4✔
551
            Ok(RasterSubQueryAdapter::<'a, P, _, _>::new(
4✔
552
                &self.source,
4✔
553
                query,
4✔
554
                self.tiling_spec,
4✔
555
                ctx,
4✔
556
                sub_query_spec,
4✔
557
            )
4✔
558
            .filter_and_fill())
4✔
559
        } else {
560
            log::debug!("No intersection between source data / srs and target srs");
×
561

562
            let tiling_strat = self
×
563
                .tiling_spec
×
564
                .strategy(query.spatial_resolution.x, -query.spatial_resolution.y);
×
565

×
566
            let grid_bounds = tiling_strat.tile_grid_box(query.spatial_partition());
×
567
            Ok(Box::pin(SparseTilesFillAdapter::new(
×
568
                stream::empty(),
×
569
                grid_bounds,
×
570
                tiling_strat.geo_transform,
×
571
                self.tiling_spec.tile_size_in_pixels,
×
572
            )))
×
573
        }
574
    }
8✔
575
}
576

577
#[cfg(test)]
578
mod tests {
579
    use super::*;
580
    use crate::engine::{MockExecutionContext, MockQueryContext};
581
    use crate::mock::MockFeatureCollectionSource;
582
    use crate::mock::{MockRasterSource, MockRasterSourceParams};
583
    use crate::{
584
        engine::{ChunkByteSize, VectorOperator},
585
        source::{
586
            FileNotFoundHandling, GdalDatasetGeoTransform, GdalDatasetParameters,
587
            GdalMetaDataRegular, GdalMetaDataStatic, GdalSource, GdalSourceParameters,
588
            GdalSourceTimePlaceholder, TimeReference,
589
        },
590
        test_data,
591
        util::gdal::{add_ndvi_dataset, gdal_open_dataset},
592
    };
593
    use float_cmp::approx_eq;
594
    use futures::StreamExt;
595
    use geoengine_datatypes::collections::IntoGeometryIterator;
596
    use geoengine_datatypes::primitives::{
597
        AxisAlignedRectangle, Coordinate2D, DateTimeParseFormat,
598
    };
599
    use geoengine_datatypes::{
600
        collections::{
601
            GeometryCollection, MultiLineStringCollection, MultiPointCollection,
602
            MultiPolygonCollection,
603
        },
604
        dataset::{DataId, DatasetId},
605
        hashmap,
606
        primitives::{
607
            BoundingBox2D, Measurement, MultiLineString, MultiPoint, MultiPolygon, QueryRectangle,
608
            SpatialResolution, TimeGranularity, TimeInstance, TimeInterval, TimeStep,
609
        },
610
        raster::{Grid, GridShape, GridShape2D, GridSize, RasterDataType, RasterTile2D},
611
        spatial_reference::SpatialReferenceAuthority,
612
        util::{
613
            test::TestDefault,
614
            well_known_data::{
615
                COLOGNE_EPSG_4326, COLOGNE_EPSG_900_913, HAMBURG_EPSG_4326, HAMBURG_EPSG_900_913,
616
                MARBURG_EPSG_4326, MARBURG_EPSG_900_913,
617
            },
618
            Identifier,
619
        },
620
    };
621
    use std::collections::HashMap;
622
    use std::path::PathBuf;
623
    use std::str::FromStr;
624

625
    #[tokio::test]
1✔
626
    async fn multi_point() -> Result<()> {
1✔
627
        let points = MultiPointCollection::from_data(
1✔
628
            MultiPoint::many(vec![
1✔
629
                MARBURG_EPSG_4326,
1✔
630
                COLOGNE_EPSG_4326,
1✔
631
                HAMBURG_EPSG_4326,
1✔
632
            ])
1✔
633
            .unwrap(),
1✔
634
            vec![TimeInterval::new_unchecked(0, 1); 3],
1✔
635
            Default::default(),
1✔
636
        )?;
1✔
637

638
        let expected = MultiPoint::many(vec![
1✔
639
            MARBURG_EPSG_900_913,
1✔
640
            COLOGNE_EPSG_900_913,
1✔
641
            HAMBURG_EPSG_900_913,
1✔
642
        ])
1✔
643
        .unwrap();
1✔
644

1✔
645
        let point_source = MockFeatureCollectionSource::single(points.clone()).boxed();
1✔
646

1✔
647
        let target_spatial_reference =
1✔
648
            SpatialReference::new(SpatialReferenceAuthority::Epsg, 900_913);
1✔
649

650
        let initialized_operator = VectorOperator::boxed(Reprojection {
1✔
651
            params: ReprojectionParams {
1✔
652
                target_spatial_reference,
1✔
653
            },
1✔
654
            sources: SingleRasterOrVectorSource {
1✔
655
                source: point_source.into(),
1✔
656
            },
1✔
657
        })
1✔
658
        .initialize(
1✔
659
            WorkflowOperatorPath::initialize_root(),
1✔
660
            &MockExecutionContext::test_default(),
1✔
661
        )
1✔
662
        .await?;
×
663

664
        let query_processor = initialized_operator.query_processor()?;
1✔
665

666
        let query_processor = query_processor.multi_point().unwrap();
1✔
667

1✔
668
        let query_rectangle = VectorQueryRectangle {
1✔
669
            spatial_bounds: BoundingBox2D::new(
1✔
670
                (COLOGNE_EPSG_4326.x, MARBURG_EPSG_4326.y).into(),
1✔
671
                (MARBURG_EPSG_4326.x, HAMBURG_EPSG_4326.y).into(),
1✔
672
            )
1✔
673
            .unwrap(),
1✔
674
            time_interval: TimeInterval::default(),
1✔
675
            spatial_resolution: SpatialResolution::zero_point_one(),
1✔
676
        };
1✔
677
        let ctx = MockQueryContext::new(ChunkByteSize::MAX);
1✔
678

679
        let query = query_processor.query(query_rectangle, &ctx).await.unwrap();
1✔
680

681
        let result = query
1✔
682
            .map(Result::unwrap)
1✔
683
            .collect::<Vec<MultiPointCollection>>()
1✔
684
            .await;
×
685

686
        assert_eq!(result.len(), 1);
1✔
687

688
        result[0]
1✔
689
            .geometries()
1✔
690
            .into_iter()
1✔
691
            .zip(expected.iter())
1✔
692
            .for_each(|(a, e)| {
3✔
693
                assert!(approx_eq!(&MultiPoint, &a.into(), e, epsilon = 0.00001));
3✔
694
            });
3✔
695

1✔
696
        Ok(())
1✔
697
    }
698

699
    #[tokio::test]
1✔
700
    async fn multi_lines() -> Result<()> {
1✔
701
        let lines = MultiLineStringCollection::from_data(
1✔
702
            vec![MultiLineString::new(vec![vec![
1✔
703
                MARBURG_EPSG_4326,
1✔
704
                COLOGNE_EPSG_4326,
1✔
705
                HAMBURG_EPSG_4326,
1✔
706
            ]])
1✔
707
            .unwrap()],
1✔
708
            vec![TimeInterval::new_unchecked(0, 1); 1],
1✔
709
            Default::default(),
1✔
710
        )?;
1✔
711

712
        let expected = [MultiLineString::new(vec![vec![
1✔
713
            MARBURG_EPSG_900_913,
1✔
714
            COLOGNE_EPSG_900_913,
1✔
715
            HAMBURG_EPSG_900_913,
1✔
716
        ]])
1✔
717
        .unwrap()];
1✔
718

1✔
719
        let lines_source = MockFeatureCollectionSource::single(lines.clone()).boxed();
1✔
720

1✔
721
        let target_spatial_reference =
1✔
722
            SpatialReference::new(SpatialReferenceAuthority::Epsg, 900_913);
1✔
723

724
        let initialized_operator = VectorOperator::boxed(Reprojection {
1✔
725
            params: ReprojectionParams {
1✔
726
                target_spatial_reference,
1✔
727
            },
1✔
728
            sources: SingleRasterOrVectorSource {
1✔
729
                source: lines_source.into(),
1✔
730
            },
1✔
731
        })
1✔
732
        .initialize(
1✔
733
            WorkflowOperatorPath::initialize_root(),
1✔
734
            &MockExecutionContext::test_default(),
1✔
735
        )
1✔
736
        .await?;
×
737

738
        let query_processor = initialized_operator.query_processor()?;
1✔
739

740
        let query_processor = query_processor.multi_line_string().unwrap();
1✔
741

1✔
742
        let query_rectangle = VectorQueryRectangle {
1✔
743
            spatial_bounds: BoundingBox2D::new(
1✔
744
                (COLOGNE_EPSG_4326.x, MARBURG_EPSG_4326.y).into(),
1✔
745
                (MARBURG_EPSG_4326.x, HAMBURG_EPSG_4326.y).into(),
1✔
746
            )
1✔
747
            .unwrap(),
1✔
748
            time_interval: TimeInterval::default(),
1✔
749
            spatial_resolution: SpatialResolution::zero_point_one(),
1✔
750
        };
1✔
751
        let ctx = MockQueryContext::new(ChunkByteSize::MAX);
1✔
752

753
        let query = query_processor.query(query_rectangle, &ctx).await.unwrap();
1✔
754

755
        let result = query
1✔
756
            .map(Result::unwrap)
1✔
757
            .collect::<Vec<MultiLineStringCollection>>()
1✔
758
            .await;
×
759

760
        assert_eq!(result.len(), 1);
1✔
761

762
        result[0]
1✔
763
            .geometries()
1✔
764
            .into_iter()
1✔
765
            .zip(expected.iter())
1✔
766
            .for_each(|(a, e)| {
1✔
767
                assert!(approx_eq!(
1✔
768
                    &MultiLineString,
1✔
769
                    &a.into(),
1✔
770
                    e,
1✔
771
                    epsilon = 0.00001
1✔
772
                ));
1✔
773
            });
1✔
774

1✔
775
        Ok(())
1✔
776
    }
777

778
    #[tokio::test]
1✔
779
    async fn multi_polygons() -> Result<()> {
1✔
780
        let polygons = MultiPolygonCollection::from_data(
1✔
781
            vec![MultiPolygon::new(vec![vec![vec![
1✔
782
                MARBURG_EPSG_4326,
1✔
783
                COLOGNE_EPSG_4326,
1✔
784
                HAMBURG_EPSG_4326,
1✔
785
                MARBURG_EPSG_4326,
1✔
786
            ]]])
1✔
787
            .unwrap()],
1✔
788
            vec![TimeInterval::new_unchecked(0, 1); 1],
1✔
789
            Default::default(),
1✔
790
        )?;
1✔
791

792
        let expected = [MultiPolygon::new(vec![vec![vec![
1✔
793
            MARBURG_EPSG_900_913,
1✔
794
            COLOGNE_EPSG_900_913,
1✔
795
            HAMBURG_EPSG_900_913,
1✔
796
            MARBURG_EPSG_900_913,
1✔
797
        ]]])
1✔
798
        .unwrap()];
1✔
799

1✔
800
        let polygon_source = MockFeatureCollectionSource::single(polygons.clone()).boxed();
1✔
801

1✔
802
        let target_spatial_reference =
1✔
803
            SpatialReference::new(SpatialReferenceAuthority::Epsg, 900_913);
1✔
804

805
        let initialized_operator = VectorOperator::boxed(Reprojection {
1✔
806
            params: ReprojectionParams {
1✔
807
                target_spatial_reference,
1✔
808
            },
1✔
809
            sources: SingleRasterOrVectorSource {
1✔
810
                source: polygon_source.into(),
1✔
811
            },
1✔
812
        })
1✔
813
        .initialize(
1✔
814
            WorkflowOperatorPath::initialize_root(),
1✔
815
            &MockExecutionContext::test_default(),
1✔
816
        )
1✔
817
        .await?;
×
818

819
        let query_processor = initialized_operator.query_processor()?;
1✔
820

821
        let query_processor = query_processor.multi_polygon().unwrap();
1✔
822

1✔
823
        let query_rectangle = VectorQueryRectangle {
1✔
824
            spatial_bounds: BoundingBox2D::new(
1✔
825
                (COLOGNE_EPSG_4326.x, MARBURG_EPSG_4326.y).into(),
1✔
826
                (MARBURG_EPSG_4326.x, HAMBURG_EPSG_4326.y).into(),
1✔
827
            )
1✔
828
            .unwrap(),
1✔
829
            time_interval: TimeInterval::default(),
1✔
830
            spatial_resolution: SpatialResolution::zero_point_one(),
1✔
831
        };
1✔
832
        let ctx = MockQueryContext::new(ChunkByteSize::MAX);
1✔
833

834
        let query = query_processor.query(query_rectangle, &ctx).await.unwrap();
1✔
835

836
        let result = query
1✔
837
            .map(Result::unwrap)
1✔
838
            .collect::<Vec<MultiPolygonCollection>>()
1✔
839
            .await;
×
840

841
        assert_eq!(result.len(), 1);
1✔
842

843
        result[0]
1✔
844
            .geometries()
1✔
845
            .into_iter()
1✔
846
            .zip(expected.iter())
1✔
847
            .for_each(|(a, e)| {
1✔
848
                assert!(approx_eq!(&MultiPolygon, &a.into(), e, epsilon = 0.00001));
1✔
849
            });
1✔
850

1✔
851
        Ok(())
1✔
852
    }
853

854
    #[tokio::test]
1✔
855
    async fn raster_identity() -> Result<()> {
1✔
856
        let projection = SpatialReference::new(
1✔
857
            geoengine_datatypes::spatial_reference::SpatialReferenceAuthority::Epsg,
1✔
858
            4326,
1✔
859
        );
1✔
860

1✔
861
        let data = vec![
1✔
862
            RasterTile2D {
1✔
863
                time: TimeInterval::new_unchecked(0, 5),
1✔
864
                tile_position: [-1, 0].into(),
1✔
865
                global_geo_transform: TestDefault::test_default(),
1✔
866
                grid_array: Grid::new([2, 2].into(), vec![1, 2, 3, 4]).unwrap().into(),
1✔
867
                properties: Default::default(),
1✔
868
            },
1✔
869
            RasterTile2D {
1✔
870
                time: TimeInterval::new_unchecked(0, 5),
1✔
871
                tile_position: [-1, 1].into(),
1✔
872
                global_geo_transform: TestDefault::test_default(),
1✔
873
                grid_array: Grid::new([2, 2].into(), vec![7, 8, 9, 10]).unwrap().into(),
1✔
874
                properties: Default::default(),
1✔
875
            },
1✔
876
            RasterTile2D {
1✔
877
                time: TimeInterval::new_unchecked(5, 10),
1✔
878
                tile_position: [-1, 0].into(),
1✔
879
                global_geo_transform: TestDefault::test_default(),
1✔
880
                grid_array: Grid::new([2, 2].into(), vec![13, 14, 15, 16])
1✔
881
                    .unwrap()
1✔
882
                    .into(),
1✔
883
                properties: Default::default(),
1✔
884
            },
1✔
885
            RasterTile2D {
1✔
886
                time: TimeInterval::new_unchecked(5, 10),
1✔
887
                tile_position: [-1, 1].into(),
1✔
888
                global_geo_transform: TestDefault::test_default(),
1✔
889
                grid_array: Grid::new([2, 2].into(), vec![19, 20, 21, 22])
1✔
890
                    .unwrap()
1✔
891
                    .into(),
1✔
892
                properties: Default::default(),
1✔
893
            },
1✔
894
        ];
1✔
895

1✔
896
        let mrs1 = MockRasterSource {
1✔
897
            params: MockRasterSourceParams {
1✔
898
                data: data.clone(),
1✔
899
                result_descriptor: RasterResultDescriptor {
1✔
900
                    data_type: RasterDataType::U8,
1✔
901
                    spatial_reference: SpatialReference::epsg_4326().into(),
1✔
902
                    measurement: Measurement::Unitless,
1✔
903
                    time: None,
1✔
904
                    bbox: None,
1✔
905
                    resolution: Some(SpatialResolution::one()),
1✔
906
                },
1✔
907
            },
1✔
908
        }
1✔
909
        .boxed();
1✔
910

1✔
911
        let mut exe_ctx = MockExecutionContext::test_default();
1✔
912
        exe_ctx.tiling_specification.tile_size_in_pixels = GridShape {
1✔
913
            // we need a smaller tile size
1✔
914
            shape_array: [2, 2],
1✔
915
        };
1✔
916

1✔
917
        let query_ctx = MockQueryContext::test_default();
1✔
918

919
        let initialized_operator = RasterOperator::boxed(Reprojection {
1✔
920
            params: ReprojectionParams {
1✔
921
                target_spatial_reference: projection, // This test will do a identity reprojection
1✔
922
            },
1✔
923
            sources: SingleRasterOrVectorSource {
1✔
924
                source: mrs1.into(),
1✔
925
            },
1✔
926
        })
1✔
927
        .initialize(WorkflowOperatorPath::initialize_root(), &exe_ctx)
1✔
928
        .await?;
×
929

930
        let qp = initialized_operator
1✔
931
            .query_processor()
1✔
932
            .unwrap()
1✔
933
            .get_u8()
1✔
934
            .unwrap();
1✔
935

1✔
936
        let query_rect = RasterQueryRectangle {
1✔
937
            spatial_bounds: SpatialPartition2D::new_unchecked((0., 1.).into(), (3., 0.).into()),
1✔
938
            time_interval: TimeInterval::new_unchecked(0, 10),
1✔
939
            spatial_resolution: SpatialResolution::one(),
1✔
940
        };
1✔
941

942
        let a = qp.raster_query(query_rect, &query_ctx).await?;
1✔
943

944
        let res = a
1✔
945
            .map(Result::unwrap)
1✔
946
            .collect::<Vec<RasterTile2D<u8>>>()
1✔
947
            .await;
8✔
948
        assert_eq!(data, res);
1✔
949

950
        Ok(())
1✔
951
    }
952

953
    #[tokio::test]
1✔
954
    async fn raster_ndvi_3857() -> Result<()> {
1✔
955
        let mut exe_ctx = MockExecutionContext::test_default();
1✔
956
        let query_ctx = MockQueryContext::test_default();
1✔
957
        let id = add_ndvi_dataset(&mut exe_ctx);
1✔
958
        exe_ctx.tiling_specification =
1✔
959
            TilingSpecification::new((0.0, 0.0).into(), [450, 450].into());
1✔
960

1✔
961
        let output_shape: GridShape2D = [900, 1800].into();
1✔
962
        let output_bounds =
1✔
963
            SpatialPartition2D::new_unchecked((0., 20_000_000.).into(), (20_000_000., 0.).into());
1✔
964
        let time_interval = TimeInterval::new_unchecked(1_388_534_400_000, 1_388_534_400_001);
1✔
965
        // 2014-01-01
1✔
966

1✔
967
        let gdal_op = GdalSource {
1✔
968
            params: GdalSourceParameters { data: id.clone() },
1✔
969
        }
1✔
970
        .boxed();
1✔
971

1✔
972
        let projection = SpatialReference::new(
1✔
973
            geoengine_datatypes::spatial_reference::SpatialReferenceAuthority::Epsg,
1✔
974
            3857,
1✔
975
        );
1✔
976

977
        let initialized_operator = RasterOperator::boxed(Reprojection {
1✔
978
            params: ReprojectionParams {
1✔
979
                target_spatial_reference: projection,
1✔
980
            },
1✔
981
            sources: SingleRasterOrVectorSource {
1✔
982
                source: gdal_op.into(),
1✔
983
            },
1✔
984
        })
1✔
985
        .initialize(WorkflowOperatorPath::initialize_root(), &exe_ctx)
1✔
986
        .await?;
×
987

988
        let x_query_resolution = output_bounds.size_x() / output_shape.axis_size_x() as f64;
1✔
989
        let y_query_resolution = output_bounds.size_y() / (output_shape.axis_size_y() * 2) as f64; // *2 to account for the dataset aspect ratio 2:1
1✔
990
        let spatial_resolution =
1✔
991
            SpatialResolution::new_unchecked(x_query_resolution, y_query_resolution);
1✔
992

1✔
993
        let qp = initialized_operator
1✔
994
            .query_processor()
1✔
995
            .unwrap()
1✔
996
            .get_u8()
1✔
997
            .unwrap();
1✔
998

999
        let qs = qp
1✔
1000
            .raster_query(
1✔
1001
                RasterQueryRectangle {
1✔
1002
                    spatial_bounds: output_bounds,
1✔
1003
                    time_interval,
1✔
1004
                    spatial_resolution,
1✔
1005
                },
1✔
1006
                &query_ctx,
1✔
1007
            )
1✔
1008
            .await
×
1009
            .unwrap();
1✔
1010

1011
        let res = qs
1✔
1012
            .map(Result::unwrap)
1✔
1013
            .collect::<Vec<RasterTile2D<u8>>>()
1✔
1014
            .await;
131✔
1015

1016
        // Write the tiles to a file
1017
        // let mut buffer = File::create("MOD13A2_M_NDVI_2014-04-01_tile-20.rst")?;
1018
        // buffer.write(res[8].clone().into_materialized_tile().grid_array.data.as_slice())?;
1019

1020
        // This check is against a tile produced by the operator itself. It was visually validated. TODO: rebuild when open issues are solved.
1021
        // A perfect validation would be against a GDAL output generated like this:
1022
        // gdalwarp -t_srs EPSG:3857 -tr 11111.11111111 11111.11111111 -r near -te 0.0 5011111.111111112 5000000.0 10011111.111111112 -te_srs EPSG:3857 -of GTiff ./MOD13A2_M_NDVI_2014-04-01.TIFF ./MOD13A2_M_NDVI_2014-04-01_tile-20.rst
1023

1024
        assert_eq!(
1✔
1025
            include_bytes!(
1✔
1026
                "../../../test_data/raster/modis_ndvi/projected_3857/MOD13A2_M_NDVI_2014-04-01_tile-20.rst"
1✔
1027
            ) as &[u8],
1✔
1028
            res[8].clone().into_materialized_tile().grid_array.inner_grid.data.as_slice()
1✔
1029
        );
1✔
1030

1031
        Ok(())
1✔
1032
    }
1033

1034
    #[test]
1✔
1035
    fn query_rewrite_4326_3857() {
1✔
1036
        let query = VectorQueryRectangle {
1✔
1037
            spatial_bounds: BoundingBox2D::new_unchecked((-180., -90.).into(), (180., 90.).into()),
1✔
1038
            time_interval: TimeInterval::default(),
1✔
1039
            spatial_resolution: SpatialResolution::zero_point_one(),
1✔
1040
        };
1✔
1041

1✔
1042
        let expected = BoundingBox2D::new_unchecked(
1✔
1043
            (-20_037_508.342_789_244, -20_048_966.104_014_594).into(),
1✔
1044
            (20_037_508.342_789_244, 20_048_966.104_014_594).into(),
1✔
1045
        );
1✔
1046

1✔
1047
        let reprojected = reproject_query(
1✔
1048
            query,
1✔
1049
            SpatialReference::new(SpatialReferenceAuthority::Epsg, 3857),
1✔
1050
            SpatialReference::epsg_4326(),
1✔
1051
        )
1✔
1052
        .unwrap()
1✔
1053
        .unwrap();
1✔
1054

1✔
1055
        assert!(approx_eq!(
1✔
1056
            BoundingBox2D,
1✔
1057
            expected,
1✔
1058
            reprojected.spatial_bounds,
1✔
1059
            epsilon = 0.000_001
1✔
1060
        ));
1✔
1061
    }
1✔
1062

1063
    #[tokio::test]
1✔
1064
    async fn raster_ndvi_3857_to_4326() -> Result<()> {
1✔
1065
        let mut exe_ctx = MockExecutionContext::test_default();
1✔
1066
        let query_ctx = MockQueryContext::test_default();
1✔
1067

1✔
1068
        let m = GdalMetaDataRegular {
1✔
1069
            data_time: TimeInterval::new_unchecked(
1✔
1070
                TimeInstance::from_str("2014-01-01T00:00:00.000Z").unwrap(),
1✔
1071
                TimeInstance::from_str("2014-07-01T00:00:00.000Z").unwrap(),
1✔
1072
            ),
1✔
1073
            step: TimeStep {
1✔
1074
                granularity: TimeGranularity::Months,
1✔
1075
                step: 1,
1✔
1076
            },
1✔
1077
            time_placeholders: hashmap! {
1✔
1078
                "%_START_TIME_%".to_string() => GdalSourceTimePlaceholder {
1✔
1079
                    format: DateTimeParseFormat::custom("%Y-%m-%d".to_string()),
1✔
1080
                    reference: TimeReference::Start,
1✔
1081
                },
1✔
1082
            },
1✔
1083
            params: GdalDatasetParameters {
1✔
1084
                file_path: test_data!(
1✔
1085
                    "raster/modis_ndvi/projected_3857/MOD13A2_M_NDVI_%_START_TIME_%.TIFF"
1✔
1086
                )
1✔
1087
                .into(),
1✔
1088
                rasterband_channel: 1,
1✔
1089
                geo_transform: GdalDatasetGeoTransform {
1✔
1090
                    origin_coordinate: (-20_037_508.342_789_244, 19_971_868.880_408_563).into(),
1✔
1091
                    x_pixel_size: 14_052.950_258_048_739,
1✔
1092
                    y_pixel_size: -14_057.881_117_788_405,
1✔
1093
                },
1✔
1094
                width: 2851,
1✔
1095
                height: 2841,
1✔
1096
                file_not_found_handling: FileNotFoundHandling::Error,
1✔
1097
                no_data_value: Some(0.),
1✔
1098
                properties_mapping: None,
1✔
1099
                gdal_open_options: None,
1✔
1100
                gdal_config_options: None,
1✔
1101
                allow_alphaband_as_mask: true,
1✔
1102
                retry: None,
1✔
1103
            },
1✔
1104
            result_descriptor: RasterResultDescriptor {
1✔
1105
                data_type: RasterDataType::U8,
1✔
1106
                spatial_reference: SpatialReference::new(SpatialReferenceAuthority::Epsg, 3857)
1✔
1107
                    .into(),
1✔
1108
                measurement: Measurement::Unitless,
1✔
1109
                time: None,
1✔
1110
                bbox: None,
1✔
1111
                resolution: None,
1✔
1112
            },
1✔
1113
        };
1✔
1114

1✔
1115
        let id: DataId = DatasetId::new().into();
1✔
1116
        exe_ctx.add_meta_data(id.clone(), Box::new(m));
1✔
1117

1✔
1118
        exe_ctx.tiling_specification = TilingSpecification::new((0.0, 0.0).into(), [60, 60].into());
1✔
1119

1✔
1120
        let output_bounds =
1✔
1121
            SpatialPartition2D::new_unchecked((-180., 90.).into(), (180., -90.).into());
1✔
1122
        let time_interval = TimeInterval::new_unchecked(1_396_310_400_000, 1_396_310_400_000);
1✔
1123
        // 2014-04-01
1✔
1124

1✔
1125
        let gdal_op = GdalSource {
1✔
1126
            params: GdalSourceParameters { data: id.clone() },
1✔
1127
        }
1✔
1128
        .boxed();
1✔
1129

1130
        let initialized_operator = RasterOperator::boxed(Reprojection {
1✔
1131
            params: ReprojectionParams {
1✔
1132
                target_spatial_reference: SpatialReference::epsg_4326(),
1✔
1133
            },
1✔
1134
            sources: SingleRasterOrVectorSource {
1✔
1135
                source: gdal_op.into(),
1✔
1136
            },
1✔
1137
        })
1✔
1138
        .initialize(WorkflowOperatorPath::initialize_root(), &exe_ctx)
1✔
1139
        .await?;
×
1140

1141
        let x_query_resolution = output_bounds.size_x() / 480.; // since we request x -180 to 180 and y -90 to 90 with 60x60 tiles this will result in 8 x 4 tiles
1✔
1142
        let y_query_resolution = output_bounds.size_y() / 240.; // *2 to account for the dataset aspect ratio 2:1
1✔
1143
        let spatial_resolution =
1✔
1144
            SpatialResolution::new_unchecked(x_query_resolution, y_query_resolution);
1✔
1145

1✔
1146
        let qp = initialized_operator
1✔
1147
            .query_processor()
1✔
1148
            .unwrap()
1✔
1149
            .get_u8()
1✔
1150
            .unwrap();
1✔
1151

1152
        let qs = qp
1✔
1153
            .raster_query(
1✔
1154
                QueryRectangle {
1✔
1155
                    spatial_bounds: output_bounds,
1✔
1156
                    time_interval,
1✔
1157
                    spatial_resolution,
1✔
1158
                },
1✔
1159
                &query_ctx,
1✔
1160
            )
1✔
1161
            .await
×
1162
            .unwrap();
1✔
1163

1164
        let tiles = qs
1✔
1165
            .map(Result::unwrap)
1✔
1166
            .collect::<Vec<RasterTile2D<u8>>>()
1✔
1167
            .await;
242✔
1168

1169
        // the test must generate 8x4 tiles
1170
        assert_eq!(tiles.len(), 32);
1✔
1171

1172
        // none of the tiles should be empty
1173
        assert!(tiles.iter().all(|t| !t.is_empty()));
32✔
1174

1175
        Ok(())
1✔
1176
    }
1177

1178
    #[test]
1✔
1179
    fn source_resolution() {
1✔
1180
        let epsg_4326 = SpatialReference::epsg_4326();
1✔
1181
        let epsg_3857 = SpatialReference::new(SpatialReferenceAuthority::Epsg, 3857);
1✔
1182

1✔
1183
        // use ndvi dataset that was reprojected using gdal as ground truth
1✔
1184
        let dataset_4326 = gdal_open_dataset(test_data!(
1✔
1185
            "raster/modis_ndvi/MOD13A2_M_NDVI_2014-04-01.TIFF"
1✔
1186
        ))
1✔
1187
        .unwrap();
1✔
1188
        let geotransform_4326 = dataset_4326.geo_transform().unwrap();
1✔
1189
        let res_4326 = SpatialResolution::new(geotransform_4326[1], -geotransform_4326[5]).unwrap();
1✔
1190

1✔
1191
        let dataset_3857 = gdal_open_dataset(test_data!(
1✔
1192
            "raster/modis_ndvi/projected_3857/MOD13A2_M_NDVI_2014-04-01.TIFF"
1✔
1193
        ))
1✔
1194
        .unwrap();
1✔
1195
        let geotransform_3857 = dataset_3857.geo_transform().unwrap();
1✔
1196
        let res_3857 = SpatialResolution::new(geotransform_3857[1], -geotransform_3857[5]).unwrap();
1✔
1197

1✔
1198
        // ndvi was projected from 4326 to 3857. The calculated source_resolution for getting the raster in 3857 with `res_3857`
1✔
1199
        // should thus roughly be like the original `res_4326`
1✔
1200
        let result_res = suggest_pixel_size_from_diag_cross_projected::<SpatialPartition2D>(
1✔
1201
            epsg_3857.area_of_use_projected().unwrap(),
1✔
1202
            epsg_4326.area_of_use_projected().unwrap(),
1✔
1203
            res_3857,
1✔
1204
        )
1✔
1205
        .unwrap();
1✔
1206
        assert!(1. - (result_res.x / res_4326.x).abs() < 0.02);
1✔
1207
        assert!(1. - (result_res.y / res_4326.y).abs() < 0.02);
1✔
1208
    }
1✔
1209

1210
    #[tokio::test]
1✔
1211
    async fn query_outside_projection_area_of_use_produces_empty_tiles() {
1✔
1212
        let mut exe_ctx = MockExecutionContext::test_default();
1✔
1213
        let query_ctx = MockQueryContext::test_default();
1✔
1214

1✔
1215
        let m = GdalMetaDataStatic {
1✔
1216
            time: Some(TimeInterval::default()),
1✔
1217
            params: GdalDatasetParameters {
1✔
1218
                file_path: PathBuf::new(),
1✔
1219
                rasterband_channel: 1,
1✔
1220
                geo_transform: GdalDatasetGeoTransform {
1✔
1221
                    origin_coordinate: (166_021.44, 9_329_005.188).into(),
1✔
1222
                    x_pixel_size: (534_994.66 - 166_021.444) / 100.,
1✔
1223
                    y_pixel_size: -9_329_005.18 / 100.,
1✔
1224
                },
1✔
1225
                width: 100,
1✔
1226
                height: 100,
1✔
1227
                file_not_found_handling: FileNotFoundHandling::NoData,
1✔
1228
                no_data_value: Some(0.),
1✔
1229
                properties_mapping: None,
1✔
1230
                gdal_open_options: None,
1✔
1231
                gdal_config_options: None,
1✔
1232
                allow_alphaband_as_mask: true,
1✔
1233
                retry: None,
1✔
1234
            },
1✔
1235
            result_descriptor: RasterResultDescriptor {
1✔
1236
                data_type: RasterDataType::U8,
1✔
1237
                spatial_reference: SpatialReference::new(SpatialReferenceAuthority::Epsg, 32636)
1✔
1238
                    .into(),
1✔
1239
                measurement: Measurement::Unitless,
1✔
1240
                time: None,
1✔
1241
                bbox: None,
1✔
1242
                resolution: None,
1✔
1243
            },
1✔
1244
        };
1✔
1245

1✔
1246
        let id: DataId = DatasetId::new().into();
1✔
1247
        exe_ctx.add_meta_data(id.clone(), Box::new(m));
1✔
1248

1✔
1249
        exe_ctx.tiling_specification =
1✔
1250
            TilingSpecification::new((0.0, 0.0).into(), [600, 600].into());
1✔
1251

1✔
1252
        let output_shape: GridShape2D = [1000, 1000].into();
1✔
1253
        let output_bounds =
1✔
1254
            SpatialPartition2D::new_unchecked((-180., 0.).into(), (180., -90.).into());
1✔
1255
        let time_interval = TimeInterval::new_instant(1_388_534_400_000).unwrap(); // 2014-01-01
1✔
1256

1✔
1257
        let gdal_op = GdalSource {
1✔
1258
            params: GdalSourceParameters { data: id.clone() },
1✔
1259
        }
1✔
1260
        .boxed();
1✔
1261

1262
        let initialized_operator = RasterOperator::boxed(Reprojection {
1✔
1263
            params: ReprojectionParams {
1✔
1264
                target_spatial_reference: SpatialReference::epsg_4326(),
1✔
1265
            },
1✔
1266
            sources: SingleRasterOrVectorSource {
1✔
1267
                source: gdal_op.into(),
1✔
1268
            },
1✔
1269
        })
1✔
1270
        .initialize(WorkflowOperatorPath::initialize_root(), &exe_ctx)
1✔
1271
        .await
×
1272
        .unwrap();
1✔
1273

1✔
1274
        let x_query_resolution = output_bounds.size_x() / output_shape.axis_size_x() as f64;
1✔
1275
        let y_query_resolution = output_bounds.size_y() / (output_shape.axis_size_y()) as f64;
1✔
1276
        let spatial_resolution =
1✔
1277
            SpatialResolution::new_unchecked(x_query_resolution, y_query_resolution);
1✔
1278

1✔
1279
        let qp = initialized_operator
1✔
1280
            .query_processor()
1✔
1281
            .unwrap()
1✔
1282
            .get_u8()
1✔
1283
            .unwrap();
1✔
1284

1285
        let result = qp
1✔
1286
            .raster_query(
1✔
1287
                QueryRectangle {
1✔
1288
                    spatial_bounds: output_bounds,
1✔
1289
                    time_interval,
1✔
1290
                    spatial_resolution,
1✔
1291
                },
1✔
1292
                &query_ctx,
1✔
1293
            )
1✔
1294
            .await
×
1295
            .unwrap()
1✔
1296
            .map(Result::unwrap)
1✔
1297
            .collect::<Vec<_>>()
1✔
1298
            .await;
×
1299

1300
        assert_eq!(result.len(), 4);
1✔
1301

1302
        for r in result {
5✔
1303
            assert!(r.is_empty());
4✔
1304
        }
1305
    }
1306

1307
    #[tokio::test]
1✔
1308
    async fn points_from_wgs84_to_utm36n() {
1✔
1309
        let exe_ctx = MockExecutionContext::test_default();
1✔
1310
        let query_ctx = MockQueryContext::test_default();
1✔
1311

1✔
1312
        let point_source = MockFeatureCollectionSource::single(
1✔
1313
            MultiPointCollection::from_data(
1✔
1314
                MultiPoint::many(vec![
1✔
1315
                    vec![(30.0, 0.0)], // lower left of utm36n area of use
1✔
1316
                    vec![(36.0, 84.0)],
1✔
1317
                    vec![(33.0, 42.0)], // upper right of utm36n area of use
1✔
1318
                ])
1✔
1319
                .unwrap(),
1✔
1320
                vec![TimeInterval::default(); 3],
1✔
1321
                HashMap::default(),
1✔
1322
            )
1✔
1323
            .unwrap(),
1✔
1324
        )
1✔
1325
        .boxed();
1✔
1326

1327
        let initialized_operator = VectorOperator::boxed(Reprojection {
1✔
1328
            params: ReprojectionParams {
1✔
1329
                target_spatial_reference: SpatialReference::new(
1✔
1330
                    SpatialReferenceAuthority::Epsg,
1✔
1331
                    32636, // utm36n
1✔
1332
                ),
1✔
1333
            },
1✔
1334
            sources: SingleRasterOrVectorSource {
1✔
1335
                source: point_source.into(),
1✔
1336
            },
1✔
1337
        })
1✔
1338
        .initialize(WorkflowOperatorPath::initialize_root(), &exe_ctx)
1✔
1339
        .await
×
1340
        .unwrap();
1✔
1341

1✔
1342
        let qp = initialized_operator
1✔
1343
            .query_processor()
1✔
1344
            .unwrap()
1✔
1345
            .multi_point()
1✔
1346
            .unwrap();
1✔
1347

1✔
1348
        let spatial_bounds = BoundingBox2D::new(
1✔
1349
            (166_021.44, 0.00).into(), // lower left of projected utm36n area of use
1✔
1350
            (534_994.666_6, 9_329_005.18).into(), // upper right of projected utm36n area of use
1✔
1351
        )
1✔
1352
        .unwrap();
1✔
1353

1354
        let qs = qp
1✔
1355
            .vector_query(
1✔
1356
                QueryRectangle {
1✔
1357
                    spatial_bounds,
1✔
1358
                    time_interval: TimeInterval::default(),
1✔
1359
                    spatial_resolution: SpatialResolution::zero_point_one(),
1✔
1360
                },
1✔
1361
                &query_ctx,
1✔
1362
            )
1✔
1363
            .await
×
1364
            .unwrap();
1✔
1365

1366
        let points = qs.map(Result::unwrap).collect::<Vec<_>>().await;
1✔
1367

1368
        assert_eq!(points.len(), 1);
1✔
1369

1370
        let points = &points[0];
1✔
1371

1✔
1372
        assert_eq!(
1✔
1373
            points.coordinates(),
1✔
1374
            &[
1✔
1375
                (166_021.443_080_538_42, 0.0).into(),
1✔
1376
                (534_994.655_061_136_1, 9_329_005.182_447_437).into(),
1✔
1377
                (499_999.999_999_999_5, 4_649_776.224_819_178).into()
1✔
1378
            ]
1✔
1379
        );
1✔
1380
    }
1381

1382
    #[tokio::test]
1✔
1383
    async fn points_from_utm36n_to_wgs84() {
1✔
1384
        let exe_ctx = MockExecutionContext::test_default();
1✔
1385
        let query_ctx = MockQueryContext::test_default();
1✔
1386

1✔
1387
        let point_source = MockFeatureCollectionSource::with_collections_and_sref(
1✔
1388
            vec![MultiPointCollection::from_data(
1✔
1389
                MultiPoint::many(vec![
1✔
1390
                    vec![(166_021.443_080_538_42, 0.0)],
1✔
1391
                    vec![(534_994.655_061_136_1, 9_329_005.182_447_437)],
1✔
1392
                    vec![(499_999.999_999_999_5, 4_649_776.224_819_178)],
1✔
1393
                ])
1✔
1394
                .unwrap(),
1✔
1395
                vec![TimeInterval::default(); 3],
1✔
1396
                HashMap::default(),
1✔
1397
            )
1✔
1398
            .unwrap()],
1✔
1399
            SpatialReference::new(SpatialReferenceAuthority::Epsg, 32636), //utm36n
1✔
1400
        )
1✔
1401
        .boxed();
1✔
1402

1403
        let initialized_operator = VectorOperator::boxed(Reprojection {
1✔
1404
            params: ReprojectionParams {
1✔
1405
                target_spatial_reference: SpatialReference::new(
1✔
1406
                    SpatialReferenceAuthority::Epsg,
1✔
1407
                    4326, // utm36n
1✔
1408
                ),
1✔
1409
            },
1✔
1410
            sources: SingleRasterOrVectorSource {
1✔
1411
                source: point_source.into(),
1✔
1412
            },
1✔
1413
        })
1✔
1414
        .initialize(WorkflowOperatorPath::initialize_root(), &exe_ctx)
1✔
1415
        .await
×
1416
        .unwrap();
1✔
1417

1✔
1418
        let qp = initialized_operator
1✔
1419
            .query_processor()
1✔
1420
            .unwrap()
1✔
1421
            .multi_point()
1✔
1422
            .unwrap();
1✔
1423

1✔
1424
        let spatial_bounds = BoundingBox2D::new(
1✔
1425
            (30.0, 0.0).into(),  // lower left of utm36n area of use
1✔
1426
            (33.0, 42.0).into(), // upper right of utm36n area of use
1✔
1427
        )
1✔
1428
        .unwrap();
1✔
1429

1430
        let qs = qp
1✔
1431
            .vector_query(
1✔
1432
                QueryRectangle {
1✔
1433
                    spatial_bounds,
1✔
1434
                    time_interval: TimeInterval::default(),
1✔
1435
                    spatial_resolution: SpatialResolution::zero_point_one(),
1✔
1436
                },
1✔
1437
                &query_ctx,
1✔
1438
            )
1✔
1439
            .await
×
1440
            .unwrap();
1✔
1441

1442
        let points = qs.map(Result::unwrap).collect::<Vec<_>>().await;
1✔
1443

1444
        assert_eq!(points.len(), 1);
1✔
1445

1446
        let points = &points[0];
1✔
1447

1✔
1448
        assert!(approx_eq!(
1✔
1449
            &[Coordinate2D],
1✔
1450
            points.coordinates(),
1✔
1451
            &[
1✔
1452
                (30.0, 0.0).into(), // lower left of utm36n area of use
1✔
1453
                (36.0, 84.0).into(),
1✔
1454
                (33.0, 42.0).into(), // upper right of utm36n area of use
1✔
1455
            ]
1✔
1456
        ));
1✔
1457
    }
1458

1459
    #[tokio::test]
1✔
1460
    async fn points_from_utm36n_to_wgs84_out_of_area() {
1✔
1461
        // This test checks that points that are outside the area of use of the target spatial reference are not projected and an empty collection is returned
1✔
1462

1✔
1463
        let exe_ctx = MockExecutionContext::test_default();
1✔
1464
        let query_ctx = MockQueryContext::test_default();
1✔
1465

1✔
1466
        let point_source = MockFeatureCollectionSource::with_collections_and_sref(
1✔
1467
            vec![MultiPointCollection::from_data(
1✔
1468
                MultiPoint::many(vec![
1✔
1469
                    vec![(758_565., 4_928_353.)], // (12.25, 44,46)
1✔
1470
                ])
1✔
1471
                .unwrap(),
1✔
1472
                vec![TimeInterval::default(); 1],
1✔
1473
                HashMap::default(),
1✔
1474
            )
1✔
1475
            .unwrap()],
1✔
1476
            SpatialReference::new(SpatialReferenceAuthority::Epsg, 32636), //utm36n
1✔
1477
        )
1✔
1478
        .boxed();
1✔
1479

1480
        let initialized_operator = VectorOperator::boxed(Reprojection {
1✔
1481
            params: ReprojectionParams {
1✔
1482
                target_spatial_reference: SpatialReference::new(
1✔
1483
                    SpatialReferenceAuthority::Epsg,
1✔
1484
                    4326, // utm36n
1✔
1485
                ),
1✔
1486
            },
1✔
1487
            sources: SingleRasterOrVectorSource {
1✔
1488
                source: point_source.into(),
1✔
1489
            },
1✔
1490
        })
1✔
1491
        .initialize(WorkflowOperatorPath::initialize_root(), &exe_ctx)
1✔
1492
        .await
×
1493
        .unwrap();
1✔
1494

1✔
1495
        let qp = initialized_operator
1✔
1496
            .query_processor()
1✔
1497
            .unwrap()
1✔
1498
            .multi_point()
1✔
1499
            .unwrap();
1✔
1500

1✔
1501
        let spatial_bounds = BoundingBox2D::new(
1✔
1502
            (10.0, 0.0).into(),  // -20 x values left of lower left of utm36n area of use
1✔
1503
            (13.0, 42.0).into(), // -20 x values left of upper right of utm36n area of use
1✔
1504
        )
1✔
1505
        .unwrap();
1✔
1506

1507
        let qs = qp
1✔
1508
            .vector_query(
1✔
1509
                QueryRectangle {
1✔
1510
                    spatial_bounds,
1✔
1511
                    time_interval: TimeInterval::default(),
1✔
1512
                    spatial_resolution: SpatialResolution::zero_point_one(),
1✔
1513
                },
1✔
1514
                &query_ctx,
1✔
1515
            )
1✔
1516
            .await
×
1517
            .unwrap();
1✔
1518

1519
        let points = qs.map(Result::unwrap).collect::<Vec<_>>().await;
1✔
1520

1521
        assert_eq!(points.len(), 1);
1✔
1522

1523
        let points = &points[0];
1✔
1524

1✔
1525
        assert!(geoengine_datatypes::collections::FeatureCollectionInfos::is_empty(points));
1✔
1526
        assert!(points.coordinates().is_empty());
1✔
1527
    }
1528

1529
    #[test]
1✔
1530
    fn it_derives_raster_result_descriptor() {
1✔
1531
        let in_proj = SpatialReference::epsg_4326();
1✔
1532
        let out_proj = SpatialReference::from_str("EPSG:3857").unwrap();
1✔
1533
        let bbox = Some(SpatialPartition2D::new_unchecked(
1✔
1534
            (-180., 90.).into(),
1✔
1535
            (180., -90.).into(),
1✔
1536
        ));
1✔
1537

1✔
1538
        let resolution = Some(SpatialResolution::new_unchecked(0.1, 0.1));
1✔
1539

1✔
1540
        let (in_bounds, out_bounds, out_res) =
1✔
1541
            InitializedRasterReprojection::derive_raster_in_bounds_out_bounds_out_res(
1✔
1542
                in_proj, out_proj, resolution, bbox,
1✔
1543
            )
1✔
1544
            .unwrap();
1✔
1545

1✔
1546
        assert_eq!(
1✔
1547
            in_bounds.unwrap(),
1✔
1548
            SpatialPartition2D::new_unchecked((-180., 85.06).into(), (180., -85.06).into(),)
1✔
1549
        );
1✔
1550

1551
        assert_eq!(
1✔
1552
            out_bounds.unwrap(),
1✔
1553
            out_proj
1✔
1554
                .area_of_use_projected::<SpatialPartition2D>()
1✔
1555
                .unwrap()
1✔
1556
        );
1✔
1557

1558
        // TODO: y resolution should be double the x resolution, but currently we only compute a uniform resolution
1559
        assert_eq!(
1✔
1560
            out_res.unwrap(),
1✔
1561
            SpatialResolution::new_unchecked(14_237.781_884_528_267, 14_237.781_884_528_267),
1✔
1562
        );
1✔
1563
    }
1✔
1564
}
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