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

geo-engine / geoengine / 46690410060

01 Dec 2026 04:22PM UTC coverage: 88.942% (+0.02%) from 88.925%
46690410060

Pull #1553

github

web-flow
Merge 136f4046b into f14359359
Pull Request #1553: perf(backend): skip expression tiles without input data if the expression keeps no data

109 of 109 new or added lines in 2 files covered. (100.0%)

4 existing lines in 2 files now uncovered.

150592 of 169314 relevant lines covered (88.94%)

428715.4 hits per line

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

86.84
/geoengine/operators/src/processing/expression/raster_query_processor.rs
1
use super::RasterExpressionError;
2
use crate::{
3
    engine::{
4
        BoxRasterQueryProcessor, QueryContext, QueryProcessor, RasterQueryProcessor,
5
        RasterResultDescriptor,
6
    },
7
    util::Result,
8
};
9
use async_trait::async_trait;
10
use futures::{StreamExt, TryStreamExt, stream::BoxStream};
11
use geoengine_datatypes::{
12
    primitives::{BandSelection, CacheHint, RasterQueryRectangle, TimeInterval},
13
    raster::{
14
        ConvertDataType, FromIndexFnParallel, GeoTransform, GridBoundingBox2D, GridIdx2D,
15
        GridIndexAccess, GridOrEmpty, GridOrEmpty2D, GridShape2D, GridShapeAccess,
16
        MapElementsParallel, MaskedGrid2D, Pixel, RasterTile2D,
17
    },
18
};
19
use geoengine_expression::LinkedExpression;
20
use libloading::Symbol;
21
use num_traits::AsPrimitive;
22
use std::{marker::PhantomData, sync::Arc};
23

24
pub struct ExpressionInput<const N: usize> {
25
    pub raster: BoxRasterQueryProcessor<f64>,
26
}
27

28
pub struct ExpressionQueryProcessor<TO, Sources>
29
where
30
    TO: Pixel,
31
{
32
    pub sources: Sources,
33
    pub result_descriptor: RasterResultDescriptor,
34
    pub phantom_data: PhantomData<TO>,
35
    pub program: Arc<LinkedExpression>,
36
    pub map_no_data: bool,
37
}
38

39
impl<TO, Sources> ExpressionQueryProcessor<TO, Sources>
40
where
41
    TO: Pixel,
42
{
43
    pub fn new(
29 ✔
44
        program: Arc<LinkedExpression>,
29 ✔
45
        sources: Sources,
29 ✔
46
        result_descriptor: RasterResultDescriptor,
29 ✔
47
        map_no_data: bool,
29 ✔
48
    ) -> Self {
29 ✔
49
        Self {
29 ✔
50
            sources,
29 ✔
51
            result_descriptor,
29 ✔
52
            program,
29 ✔
53
            phantom_data: PhantomData,
29 ✔
54
            map_no_data,
29 ✔
55
        }
29 ✔
56
    }
29 ✔
57
}
58

59
#[async_trait]
60
impl<TO, Tuple> QueryProcessor for ExpressionQueryProcessor<TO, Tuple>
61
where
62
    TO: Pixel,
63
    Tuple: ExpressionTupleProcessor<TO>,
64
{
65
    type Output = RasterTile2D<TO>;
66
    type SpatialBounds = GridBoundingBox2D;
67
    type Selection = BandSelection;
68
    type ResultDescription = RasterResultDescriptor;
69

70
    async fn _query<'b>(
71
        &'b self,
72
        query: RasterQueryRectangle,
73
        ctx: &'b dyn QueryContext,
74
    ) -> Result<BoxStream<'b, Result<Self::Output>>> {
34 ✔
75
        // rewrite query to request all input bands from the source. They are all combined in the single output band by means of the expression.
76
        let source_query = query.select_attributes(BandSelection::first_n(Tuple::num_bands()));
77

78
        // a tile whose inputs are all empty is empty, unless the expression makes data of no data
79
        let skips_empty = !self.map_no_data || Tuple::maps_no_data_to_no_data(&self.program)?;
80

81
        let stream =
82
            self.sources
83
                .zip_bands(source_query, ctx)
84
                .await?
85
                .and_then(move |rasters| async move {
185 ✔
86
                    if skips_empty && Tuple::all_empty(&rasters) {
185 ✔
87
                        return Ok(Tuple::empty_raster(&rasters));
29 ✔
88
                    }
156 ✔
89

90
                    let (
91
                        out_time,
156 ✔
92
                        out_tile_position,
156 ✔
93
                        out_global_geo_transform,
156 ✔
94
                        _output_grid_shape,
156 ✔
95
                        cache_hint,
156 ✔
96
                    ) = Tuple::metadata(&rasters);
156 ✔
97

98
                    let program = self.program.clone();
156 ✔
99
                    let map_no_data: bool = self.map_no_data;
156 ✔
100

101
                    let out = crate::util::spawn_blocking_with_thread_pool(
156 ✔
102
                        ctx.thread_pool().clone(),
156 ✔
103
                        move || Tuple::compute_expression(rasters, &program, map_no_data),
156 ✔
104
                    )
105
                    .await??;
156 ✔
106

107
                    Ok(RasterTile2D::new(
156 ✔
108
                        out_time,
156 ✔
109
                        out_tile_position,
156 ✔
110
                        0,
156 ✔
111
                        out_global_geo_transform,
156 ✔
112
                        out,
156 ✔
113
                        cache_hint,
156 ✔
114
                    ))
156 ✔
115
                });
370 ✔
116

117
        Ok(stream.boxed())
118
    }
34 ✔
119

120
    fn result_descriptor(&self) -> &RasterResultDescriptor {
80 ✔
121
        &self.result_descriptor
80 ✔
122
    }
80 ✔
123
}
124

125
#[async_trait]
126
impl<TO, Tuple> RasterQueryProcessor for ExpressionQueryProcessor<TO, Tuple>
127
where
128
    TO: Pixel,
129
    Tuple: ExpressionTupleProcessor<TO>,
130
{
131
    type RasterType = TO;
132

133
    async fn _time_query<'a>(
134
        &'a self,
135
        query: geoengine_datatypes::primitives::TimeInterval,
136
        ctx: &'a dyn QueryContext,
137
    ) -> Result<BoxStream<'a, Result<geoengine_datatypes::primitives::TimeInterval>>> {
×
138
        self.sources.time_query(query, ctx).await
139
    }
×
140
}
141

142
#[async_trait]
143
trait ExpressionTupleProcessor<TO: Pixel>: Send + Sync {
144
    type Tuple: Send + 'static;
145

146
    async fn zip_bands<'a>(
147
        &'a self,
148
        query: RasterQueryRectangle,
149
        ctx: &'a dyn QueryContext,
150
    ) -> Result<BoxStream<'a, Result<Self::Tuple>>>;
151

152
    fn all_empty(tuple: &Self::Tuple) -> bool;
153

154
    fn empty_raster(tuple: &Self::Tuple) -> RasterTile2D<TO>;
155

156
    fn metadata(
157
        tuple: &Self::Tuple,
158
    ) -> (
159
        TimeInterval,
160
        GridIdx2D,
161
        GeoTransform,
162
        GridShape2D,
163
        CacheHint,
164
    );
165

166
    fn compute_expression(
167
        tuple: Self::Tuple,
168
        program: &LinkedExpression,
169
        map_no_data: bool,
170
    ) -> Result<GridOrEmpty2D<TO>>;
171

172
    fn num_bands() -> u32;
173

174
    /// Whether the expression gives no data if all of its inputs are no data.
175
    fn maps_no_data_to_no_data(program: &LinkedExpression) -> Result<bool>;
176

177
    async fn time_query<'a>(
178
        &'a self,
179
        query: geoengine_datatypes::primitives::TimeInterval,
180
        ctx: &'a dyn QueryContext,
181
    ) -> Result<BoxStream<'a, Result<geoengine_datatypes::primitives::TimeInterval>>>;
182
}
183

184
#[async_trait]
185
impl<TO> ExpressionTupleProcessor<TO> for ExpressionInput<1>
186
where
187
    TO: Pixel,
188
    f64: AsPrimitive<TO>,
189
{
190
    type Tuple = RasterTile2D<f64>;
191

192
    #[inline]
193
    async fn zip_bands<'a>(
194
        &'a self,
195
        query: RasterQueryRectangle,
196
        ctx: &'a dyn QueryContext,
197
    ) -> Result<BoxStream<'a, Result<Self::Tuple>>> {
15 ✔
198
        self.raster.query(query, ctx).await
199
    }
15 ✔
200

201
    #[inline]
202
    fn all_empty(tuple: &Self::Tuple) -> bool {
143 ✔
203
        tuple.grid_array.is_empty()
143 ✔
204
    }
143 ✔
205

206
    #[inline]
207
    fn empty_raster(tuple: &Self::Tuple) -> RasterTile2D<TO> {
16 ✔
208
        tuple.clone().convert_data_type()
16 ✔
209
    }
16 ✔
210

211
    #[inline]
212
    fn metadata(
127 ✔
213
        tuple: &Self::Tuple,
127 ✔
214
    ) -> (
127 ✔
215
        TimeInterval,
127 ✔
216
        GridIdx2D,
127 ✔
217
        GeoTransform,
127 ✔
218
        GridShape2D,
127 ✔
219
        CacheHint,
127 ✔
220
    ) {
127 ✔
221
        let raster = &tuple;
127 ✔
222

223
        (
127 ✔
224
            raster.time,
127 ✔
225
            raster.tile_position,
127 ✔
226
            raster.global_geo_transform,
127 ✔
227
            raster.grid_shape(),
127 ✔
228
            raster.cache_hint,
127 ✔
229
        )
127 ✔
230
    }
127 ✔
231

232
    #[inline]
233
    fn compute_expression(
127 ✔
234
        raster: Self::Tuple,
127 ✔
235
        program: &LinkedExpression,
127 ✔
236
        map_no_data: bool,
127 ✔
237
    ) -> Result<GridOrEmpty2D<TO>> {
127 ✔
238
        let expression = unsafe {
127 ✔
239
            // we have to "trust" that the function has the signature we expect
240
            program
127 ✔
241
                .function_1::<Option<f64>>()
127 ✔
242
                .map_err(RasterExpressionError::from)?
127 ✔
243
        };
244

245
        if raster.is_empty() && !map_no_data {
127 ✔
246
            return Ok(GridOrEmpty::new_empty_shape(raster.grid_shape()));
×
247
        }
127 ✔
248

249
        let empty_result = if map_no_data {
127 ✔
250
            expression(None).map(TO::from_)
9 ✔
251
        } else {
252
            None
118 ✔
253
        };
254

255
        if raster.is_empty() {
127 ✔
256
            return match empty_result {
×
257
                Some(value) => Ok(GridOrEmpty::new_grid(MaskedGrid2D::new_filled(
×
258
                    raster.grid_shape(),
×
259
                    value,
×
260
                ))),
×
261
                None => Ok(GridOrEmpty::new_empty_shape(raster.grid_shape())),
×
262
            };
263
        }
127 ✔
264

265
        let map_fn = |in_value: Option<f64>| {
29,884,494 ✔
266
            // TODO: could be a |in_value: T1| if map no data is false!
267
            if in_value.is_none() {
29,884,494 ✔
268
                return empty_result;
4,327,496 ✔
269
            }
25,556,998 ✔
270

271
            let result = expression(in_value);
25,556,998 ✔
272

273
            result.map(TO::from_)
25,556,998 ✔
274
        };
29,884,494 ✔
275

276
        let res = raster.grid_array.map_elements_parallel(map_fn);
127 ✔
277

278
        Result::Ok(res)
127 ✔
279
    }
127 ✔
280

281
    fn num_bands() -> u32 {
15 ✔
282
        1
15 ✔
283
    }
15 ✔
284

285
    fn maps_no_data_to_no_data(program: &LinkedExpression) -> Result<bool> {
5 ✔
286
        let expression = unsafe {
5 ✔
287
            // we have to "trust" that the function has the signature we expect
288
            program
5 ✔
289
                .function_1::<Option<f64>>()
5 ✔
290
                .map_err(RasterExpressionError::from)?
5 ✔
291
        };
292
        Ok(expression(None).is_none())
5 ✔
293
    }
5 ✔
294

295
    async fn time_query<'a>(
296
        &'a self,
297
        query: geoengine_datatypes::primitives::TimeInterval,
298
        ctx: &'a dyn QueryContext,
299
    ) -> Result<BoxStream<'a, Result<geoengine_datatypes::primitives::TimeInterval>>> {
×
300
        self.raster.time_query(query, ctx).await
301
    }
×
302
}
303

304
// TODO: implement this via macro for 2-8 sources
305
#[async_trait]
306
impl<TO> ExpressionTupleProcessor<TO> for ExpressionInput<2>
307
where
308
    TO: Pixel,
309
    f64: AsPrimitive<TO>,
310
{
311
    type Tuple = (RasterTile2D<f64>, RasterTile2D<f64>);
312

313
    #[inline]
314
    async fn zip_bands<'a>(
315
        &'a self,
316
        query: RasterQueryRectangle,
317
        ctx: &'a dyn QueryContext,
318
    ) -> Result<BoxStream<'a, Result<Self::Tuple>>> {
14 ✔
319
        // chunk up the stream to get all bands for a spatial tile at once
320
        let stream = self.raster.query(query, ctx).await?.chunks(2).map(|chunk| {
37 ✔
321
            if chunk.len() != 2 {
37 ✔
322
                // if there are not exactly two tiles, it should mean the last tile was an error and the chunker ended prematurely
323
                if let Some(Err(e)) = chunk.into_iter().next_back() {
×
324
                    return Err(e);
×
325
                }
×
326
                // if there is no error, the source did not produce all bands, which likely means a bug in an operator
327
                return Err(crate::error::Error::MustNotHappen {
×
328
                    message: "source did not produce all bands (N: 2)".to_string(),
×
329
                });
×
330
            }
37 ✔
331

332
            let [a, b]: [Result<RasterTile2D<f64>>; 2] = chunk
37 ✔
333
                .try_into()
37 ✔
334
                .expect("all chunks should be of length 2 because it was checked above");
37 ✔
335
            match (a, b) {
37 ✔
336
                (Ok(t0), Ok(t1)) => Ok((t0, t1)),
37 ✔
337
                (Err(e), _) | (_, Err(e)) => Err(e),
×
338
            }
339
        });
37 ✔
340

341
        Ok(stream.boxed())
342
    }
14 ✔
343

344
    #[inline]
345
    fn all_empty(tuple: &Self::Tuple) -> bool {
37 ✔
346
        tuple.0.grid_array.is_empty() && tuple.1.grid_array.is_empty()
37 ✔
347
    }
37 ✔
348

349
    #[inline]
350
    fn empty_raster(tuple: &Self::Tuple) -> RasterTile2D<TO> {
12 ✔
351
        tuple.0.clone().convert_data_type()
12 ✔
352
    }
12 ✔
353

354
    #[inline]
355
    fn metadata(
25 ✔
356
        tuple: &Self::Tuple,
25 ✔
357
    ) -> (
25 ✔
358
        TimeInterval,
25 ✔
359
        GridIdx2D,
25 ✔
360
        GeoTransform,
25 ✔
361
        GridShape2D,
25 ✔
362
        CacheHint,
25 ✔
363
    ) {
25 ✔
364
        let raster = &tuple.0;
25 ✔
365

366
        (
25 ✔
367
            raster.time,
25 ✔
368
            raster.tile_position,
25 ✔
369
            raster.global_geo_transform,
25 ✔
370
            raster.grid_shape(),
25 ✔
371
            tuple.0.cache_hint.merged(&tuple.1.cache_hint),
25 ✔
372
        )
25 ✔
373
    }
25 ✔
374

375
    #[inline]
376
    fn compute_expression(
25 ✔
377
        rasters: Self::Tuple,
25 ✔
378
        program: &LinkedExpression,
25 ✔
379
        map_no_data: bool,
25 ✔
380
    ) -> Result<GridOrEmpty2D<TO>> {
25 ✔
381
        let expression = unsafe {
25 ✔
382
            // we have to "trust" that the function has the signature we expect
383
            program
25 ✔
384
                .function_2::<Option<f64>, Option<f64>>()
25 ✔
385
                .map_err(RasterExpressionError::from)?
25 ✔
386
        };
387

388
        if !map_no_data && rasters.0.is_empty() && rasters.1.is_empty() {
25 ✔
389
            return Ok(GridOrEmpty::new_empty_shape(rasters.0.grid_shape()));
×
390
        }
25 ✔
391

392
        let empty_result = if map_no_data {
25 ✔
393
            expression(None, None).map(TO::from_)
13 ✔
394
        } else {
395
            None
12 ✔
396
        };
397

398
        if rasters.0.is_empty() && rasters.1.is_empty() {
25 ✔
UNCOV
399
            return match empty_result {
×
400
                Some(value) => Ok(GridOrEmpty::new_grid(MaskedGrid2D::new_filled(
×
401
                    rasters.0.grid_shape(),
×
402
                    value,
×
403
                ))),
×
UNCOV
404
                None => Ok(GridOrEmpty::new_empty_shape(rasters.0.grid_shape())),
×
405
            };
406
        }
25 ✔
407

408
        let map_fn = |lin_idx: usize| {
3,932,230 ✔
409
            let t0_value = rasters.0.get_at_grid_index_unchecked(lin_idx);
3,932,230 ✔
410
            let t1_value = rasters.1.get_at_grid_index_unchecked(lin_idx);
3,932,230 ✔
411

412
            if !map_no_data && (t0_value.is_none() || t1_value.is_none()) {
3,932,230 ✔
413
                return None;
99 ✔
414
            }
3,932,131 ✔
415

416
            let result = expression(t0_value, t1_value);
3,932,131 ✔
417

418
            result.map(TO::from_)
3,932,131 ✔
419
        };
3,932,230 ✔
420

421
        let grid_shape = rasters.0.grid_shape();
25 ✔
422
        let out = GridOrEmpty::from_index_fn_parallel(&grid_shape, map_fn);
25 ✔
423

424
        let out_or_empty = match out {
25 ✔
425
            GridOrEmpty::Empty(t) => GridOrEmpty::Empty(t),
×
426
            GridOrEmpty::Grid(t) => {
25 ✔
427
                if t.validity_mask.data.iter().all(|&t| !t) {
1,407,636 ✔
428
                    tracing::trace!("Converting empty expression output to EmptyGrid");
×
429
                    GridOrEmpty::new_empty_shape(t.grid_shape())
×
430
                } else {
431
                    GridOrEmpty::Grid(t)
25 ✔
432
                }
433
            }
434
        };
435

436
        Result::Ok(out_or_empty)
25 ✔
437
    }
25 ✔
438

439
    fn num_bands() -> u32 {
14 ✔
440
        2
14 ✔
441
    }
14 ✔
442

443
    fn maps_no_data_to_no_data(program: &LinkedExpression) -> Result<bool> {
5 ✔
444
        let expression = unsafe {
5 ✔
445
            // we have to "trust" that the function has the signature we expect
446
            program
5 ✔
447
                .function_2::<Option<f64>, Option<f64>>()
5 ✔
448
                .map_err(RasterExpressionError::from)?
5 ✔
449
        };
450
        Ok(expression(None, None).is_none())
5 ✔
451
    }
5 ✔
452

453
    async fn time_query<'a>(
454
        &'a self,
455
        query: geoengine_datatypes::primitives::TimeInterval,
456
        ctx: &'a dyn QueryContext,
457
    ) -> Result<BoxStream<'a, Result<geoengine_datatypes::primitives::TimeInterval>>> {
×
458
        self.raster.time_query(query, ctx).await
459
    }
×
460
}
461

462
type Function3 = fn(Option<f64>, Option<f64>, Option<f64>) -> Option<f64>;
463
type Function4 = fn(Option<f64>, Option<f64>, Option<f64>, Option<f64>) -> Option<f64>;
464
type Function5 = fn(Option<f64>, Option<f64>, Option<f64>, Option<f64>, Option<f64>) -> Option<f64>;
465
type Function6 =
466
    fn(Option<f64>, Option<f64>, Option<f64>, Option<f64>, Option<f64>, Option<f64>) -> Option<f64>;
467
type Function7 = fn(
468
    Option<f64>,
469
    Option<f64>,
470
    Option<f64>,
471
    Option<f64>,
472
    Option<f64>,
473
    Option<f64>,
474
    Option<f64>,
475
) -> Option<f64>;
476
type Function8 = fn(
477
    Option<f64>,
478
    Option<f64>,
479
    Option<f64>,
480
    Option<f64>,
481
    Option<f64>,
482
    Option<f64>,
483
    Option<f64>,
484
    Option<f64>,
485
) -> Option<f64>;
486

487
macro_rules! impl_expression_tuple_processor {
488
    ( $i:tt => $( $x:tt ),+ ) => {
489
        paste::paste! {
490
            impl_expression_tuple_processor!(
491
                @inner
492
                $i
493
                |
494
                $( $x ),*
495
                |
496
                $( [< pixel_ $x >] ),*
497
                |
498
                $( [< is_nodata_ $x >] ),*
499
                |
500
                [< Function $i >]
501
            );
502
        }
503
    };
504

505
    // We have `0, 1, 2, …` and `T0, T1, T2, …`
506
    (@inner $N:tt | $( $I:tt ),+ | $( $PIXEL:tt ),+ | $( $IS_NODATA:tt ),+ | $FN_T:ty ) => {
507
        #[async_trait]
508
        impl<TO> ExpressionTupleProcessor<TO> for ExpressionInput<$N>
509
        where
510
            TO: Pixel,
511
            f64: AsPrimitive<TO>,
512
        {
513
            type Tuple = [RasterTile2D<f64>; $N];
514

515
            #[inline]
516
            async fn zip_bands<'a>(
517
                &'a self,
518
                query: RasterQueryRectangle,
519
                ctx: &'a dyn QueryContext,
520
            ) -> Result<BoxStream<'a, Result<Self::Tuple>>> {
521
                // chunk up the stream to get all bands for a spatial tile at once
522
                let stream = self.raster.query(query, ctx).await?.chunks($N).map(|chunk| {
5 ✔
523
                    if chunk.len() != $N {
1 ✔
524
                        // if there are not exactly N tiles, it should mean the last tile was an error and the chunker ended prematurely
525
                        if let Some(Err(e)) = chunk.into_iter().last() {
×
526
                            return Err(e);
×
527
                        }
×
528
                        // if there is no error, the source did not produce all bands, which likely means a bug in an operator
529
                        return Err(crate::error::Error::MustNotHappen {
×
530
                            message: format!("source did not produce all bands, (N: {})", $N),
×
531
                        });
×
532
                    }
5 ✔
533

534
                    let mut ok_tiles = Vec::with_capacity($N);
5 ✔
535

536
                    for tile in chunk {
20 ✔
537
                        match tile {
20 ✔
538
                            Ok(tile) => ok_tiles.push(tile),
20 ✔
539
                            Err(e) => return Err(e),
×
540
                        }
541
                    }
542

543
                    let tuple: [RasterTile2D<f64>; $N] = ok_tiles
5 ✔
544
                        .try_into()
5 ✔
545
                        .expect("all chunks should be of the expected langth because it was checked above");
5 ✔
546

547
                    Ok(tuple)
5 ✔
548
                });
5 ✔
549

550
                Ok(stream.boxed())
551
            }
5 ✔
552

553
            #[inline]
554
            fn all_empty(tuple: &Self::Tuple) -> bool {
4 ✔
555
                $( tuple[$I].grid_array.is_empty() )&&*
1 ✔
556
            }
4 ✔
557

558
            #[inline]
559
            fn empty_raster(tuple: &Self::Tuple) -> RasterTile2D<TO> {
1 ✔
560
                tuple[0].clone().convert_data_type()
1 ✔
561
            }
1 ✔
562

563
            #[inline]
564
            fn metadata(tuple: &Self::Tuple) -> (TimeInterval, GridIdx2D, GeoTransform, GridShape2D, CacheHint) {
4 ✔
565
                let raster = &tuple[0];
4 ✔
566

567
                (
568
                    raster.time,
4 ✔
569
                    raster.tile_position,
4 ✔
570
                    raster.global_geo_transform,
4 ✔
571
                    raster.grid_shape(),
4 ✔
572
                    tuple.iter().fold(CacheHint::max_duration(), |acc, r| acc.merged(&r.cache_hint)),
17 ✔
573
                )
574
            }
4 ✔
575

576
            fn compute_expression(
4 ✔
577
                rasters: Self::Tuple,
4 ✔
578
                program: &LinkedExpression,
4 ✔
579
                map_no_data: bool,
4 ✔
580
            ) -> Result<GridOrEmpty2D<TO>> {
4 ✔
581
                let expression: Symbol<$FN_T> = unsafe {
4 ✔
582
                    // we have to "trust" that the function has the signature we expect
583
                    program.function_nary().map_err(RasterExpressionError::from)?
4 ✔
584
                };
585

586
                let all_empty = rasters.iter().all(|r| r.grid_array.is_empty());
6 ✔
587

588
                if all_empty && !map_no_data {
4 ✔
589
                    return Ok(GridOrEmpty::new_empty_shape(rasters[0].grid_shape()));
×
590
                };
4 ✔
591

592
                // call expression with $N None values correctly:
593
                let empty_result = if map_no_data {
4 ✔
594
                    expression(
1 ✔
595
                        $(
1 ✔
596
                            { let _ = $I; None } // this is a nice trick to get the correct number of None values for the function call
1 ✔
597
                        ),+
1 ✔
598
                    ).map(TO::from_)
1 ✔
599
                } else {
600
                    None
3 ✔
601
                };
602

603
                if all_empty {
4 ✔
604
                    return match empty_result {
1 ✔
605
                        Some(value) => Ok(GridOrEmpty::new_grid(MaskedGrid2D::new_filled(
1 ✔
606
                            rasters[0].grid_shape(),
1 ✔
607
                            value,
1 ✔
608
                        ))),
1 ✔
609
                        None => Ok(GridOrEmpty::new_empty_shape(rasters[0].grid_shape())),
×
610
                    };
611
                }
3 ✔
612

613
                let map_fn = |lin_idx: usize| {
18 ✔
614
                    $(
615
                        let $PIXEL = rasters[$I].get_at_grid_index_unchecked(lin_idx);
18 ✔
616
                        let $IS_NODATA = $PIXEL.is_none();
18 ✔
617
                    )*
618

619
                    if !map_no_data && ( $($IS_NODATA)||* ) {
18 ✔
620
                        return None;
1 ✔
621
                    }
17 ✔
622

623
                    let result = expression(
17 ✔
624
                        $(
17 ✔
625
                            $PIXEL
17 ✔
626
                        ),*
17 ✔
627
                    );
17 ✔
628

629
                    result.map(TO::from_)
17 ✔
630
                };
18 ✔
631

632
                let grid_shape = rasters[0].grid_shape();
3 ✔
633
                let out = GridOrEmpty::from_index_fn_parallel(&grid_shape, map_fn);
3 ✔
634

635
                Result::Ok(out)
3 ✔
636
            }
4 ✔
637

638
            fn num_bands() -> u32 {
5 ✔
639
                $N
640
            }
5 ✔
641

642
            fn maps_no_data_to_no_data(program: &LinkedExpression) -> Result<bool> {
2 ✔
643
                let expression: Symbol<$FN_T> = unsafe {
2 ✔
644
                    // we have to "trust" that the function has the signature we expect
645
                    program.function_nary().map_err(RasterExpressionError::from)?
2 ✔
646
                };
647
                // call expression with $N None values correctly:
648
                Ok(expression(
2 ✔
649
                    $(
2 ✔
650
                        { let _ = $I; None }
2 ✔
651
                    ),+
2 ✔
652
                )
2 ✔
653
                .is_none())
2 ✔
654
            }
2 ✔
655

656
            async fn time_query<'a>(
657
                &'a self,
658
                query: geoengine_datatypes::primitives::TimeInterval,
659
                ctx: &'a dyn QueryContext,
660
                ) -> Result<BoxStream<'a, Result<geoengine_datatypes::primitives::TimeInterval>>> {
661
                    self.raster.time_query(query, ctx).await
662
                }
×
663
        }
664

665

666
    };
667

668
    // For any input, generate `f64, bool`
669
    (@input_dtypes $x:tt) => {
670
        f64, bool
671
    };
672
}
673

674
impl_expression_tuple_processor!(3 => 0, 1, 2);
675
impl_expression_tuple_processor!(4 => 0, 1, 2, 3);
676
impl_expression_tuple_processor!(5 => 0, 1, 2, 3, 4);
677
impl_expression_tuple_processor!(6 => 0, 1, 2, 3, 4, 5);
678
impl_expression_tuple_processor!(7 => 0, 1, 2, 3, 4, 5, 6);
679
impl_expression_tuple_processor!(8 => 0, 1, 2, 3, 4, 5, 6, 7);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc