sylva-rs 2.0.0

Terrestrial and airborne laser scanning processing for forest ecology and remote sensing research.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// Sylva: LiDAR processing for forest ecology and remote sensing research.
// Copyright (C) 2026 Tim Devereux, The University of Queensland.
// Free software under the GNU General Public License v3.0 or later;
// see the LICENSE file. There is no warranty, to the extent permitted by law.
//! Georeferenced 2-D grid used for DTMs and CHMs.

use std::io::Write;
use std::path::Path;

use crate::error::{Error, Result};

/// A north-up grid; `data[row * ncols + col]`, row 0 is the *southern* edge.
///
/// The cells are one flat list, not a list of rows, so a cell is reached by
/// `row * ncols + col` - what NumPy calls C order. Square cells only: one
/// `resolution` serves both axes.
///
/// Row 0 being the southern edge is worth dwelling on, because it is the
/// opposite of how an image is stored and of how a GeoTIFF is written: here
/// the row index grows northwards, with the grid's corner at
/// `(xmin, ymin)`, so a cell's centre is at
/// `(xmin + (col + 0.5) * resolution, ymin + (row + 0.5) * resolution)`.
/// The writers flip the rows on the way out.
///
/// A cell with no value holds NaN rather than a sentinel such as -9999, so
/// arithmetic on missing data stays missing instead of quietly becoming a
/// very low height. Comparisons against NaN are always false, which is why
/// code here tests `is_finite()` rather than `!= nodata`.
#[derive(Debug, Clone, PartialEq)]
pub struct Raster {
    /// Cell values in row-major order, `data[row * ncols + col]`; NaN is a cell with no value.
    pub data: Vec<f64>,
    /// Number of rows: cells along y, counted northwards from row 0 at the southern edge.
    pub nrows: usize,
    /// Number of columns: cells along x, counted eastwards from column 0 at the western edge.
    pub ncols: usize,
    /// x of the grid's south-west corner (m), the western edge of column 0, not a cell centre.
    pub xmin: f64,
    /// y of the grid's south-west corner (m), the southern edge of row 0.
    pub ymin: f64,
    /// Cell size (m), the same along x and y.
    pub resolution: f64,
}

/// Rows of the cells containing each `y` and columns of those containing
/// each `x` (the two may differ in length) on a grid with corner
/// `(xmin, ymin)`, not clipped to any extent.
///
/// A coordinate whose index is not a finite `i64` (NaN, infinite or too far
/// away) gets `i64::MIN`, as NumPy's float-to-integer cast gives on x86.
pub fn cell_indices(xmin: f64, ymin: f64, resolution: f64, x: &[f64], y: &[f64]) -> (Vec<i64>, Vec<i64>) {
    let cast = |v: f64| -> i64 {
        let f = v.floor();
        if (-9.223_372_036_854_776e18..9.223_372_036_854_776e18).contains(&f) {
            f as i64
        } else {
            i64::MIN
        }
    };
    let rows = y.iter().map(|&v| cast((v - ymin) / resolution)).collect();
    let cols = x.iter().map(|&v| cast((v - xmin) / resolution)).collect();
    (rows, cols)
}

/// Cell-centre coordinates of an `nrows x ncols` grid, each as a row-major
/// `nrows * ncols` vector aligned with the raster's data.
pub fn cell_centers(nrows: usize, ncols: usize, xmin: f64, ymin: f64, resolution: f64) -> (Vec<f64>, Vec<f64>) {
    let mut xs = Vec::with_capacity(nrows * ncols);
    let mut ys = Vec::with_capacity(nrows * ncols);
    for r in 0..nrows {
        let y = ymin + (r as f64 + 0.5) * resolution;
        for c in 0..ncols {
            xs.push(xmin + (c as f64 + 0.5) * resolution);
            ys.push(y);
        }
    }
    (xs, ys)
}

/// How to reduce many point values into one cell.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Reducer {
    /// The lowest value that fell in the cell (a DTM from the lowest returns).
    Min,
    /// The highest value that fell in the cell (a CHM from the tallest returns).
    Max,
    /// The arithmetic mean of the values in the cell.
    Mean,
    /// The number of points in the cell (count), whatever their values.
    Count,
}

impl Raster {
    /// A grid of `nrows x ncols` cells with corner `(xmin, ymin)` and this `resolution`, every cell holding `value`.
    pub fn filled(nrows: usize, ncols: usize, xmin: f64, ymin: f64, resolution: f64, value: f64) -> Self {
        Raster { data: vec![value; nrows * ncols], nrows, ncols, xmin, ymin, resolution }
    }

    /// x of the eastern edge (m): `xmin` plus `ncols` cells.
    pub fn xmax(&self) -> f64 {
        self.xmin + self.ncols as f64 * self.resolution
    }

    /// y of the northern edge (m): `ymin` plus `nrows` cells.
    pub fn ymax(&self) -> f64 {
        self.ymin + self.nrows as f64 * self.resolution
    }

    #[inline]
    /// Value of the cell at `(row, col)`; panics when either is out of range.
    pub fn get(&self, row: usize, col: usize) -> f64 {
        self.data[row * self.ncols + col]
    }

    #[inline]
    /// Overwrite the cell at `(row, col)` with `v`; panics when either is out of range.
    pub fn set(&mut self, row: usize, col: usize, v: f64) {
        self.data[row * self.ncols + col] = v;
    }

    /// `(row, col)` of the cell containing `(x, y)` (m), possibly out of range.
    ///
    /// A coordinate that is NaN or infinite gives `i64::MIN` for its index,
    /// which [`Raster::in_bounds`] rejects, so that a point without a
    /// position is never binned into row or column 0 (a bare float-to-integer
    /// cast turns NaN into 0).
    #[inline]
    pub fn cell_index(&self, x: f64, y: f64) -> (i64, i64) {
        let cast = |v: f64| -> i64 {
            let f = v.floor();
            if (-9.223_372_036_854_776e18..9.223_372_036_854_776e18).contains(&f) {
                f as i64
            } else {
                i64::MIN
            }
        };
        (cast((y - self.ymin) / self.resolution), cast((x - self.xmin) / self.resolution))
    }

    #[inline]
    /// Whether `(row, col)`, as [`Raster::cell_index`] gives them, lies inside the grid.
    pub fn in_bounds(&self, row: i64, col: i64) -> bool {
        row >= 0 && col >= 0 && (row as usize) < self.nrows && (col as usize) < self.ncols
    }

    /// Cell-centre coordinates.
    pub fn cell_center(&self, row: usize, col: usize) -> (f64, f64) {
        (self.xmin + (col as f64 + 0.5) * self.resolution, self.ymin + (row as f64 + 0.5) * self.resolution)
    }

    /// Bilinear sample with edge clamping. NaN cells propagate.
    pub fn sample(&self, x: f64, y: f64) -> f64 {
        if self.nrows == 0 || self.ncols == 0 {
            return f64::NAN;
        }
        let fc = ((x - self.xmin) / self.resolution - 0.5).clamp(0.0, (self.ncols - 1) as f64);
        let fr = ((y - self.ymin) / self.resolution - 0.5).clamp(0.0, (self.nrows - 1) as f64);
        let c0 = fc.floor() as usize;
        let r0 = fr.floor() as usize;
        let c1 = (c0 + 1).min(self.ncols - 1);
        let r1 = (r0 + 1).min(self.nrows - 1);
        let tx = fc - c0 as f64;
        let ty = fr - r0 as f64;
        self.get(r0, c0) * (1.0 - tx) * (1.0 - ty)
            + self.get(r0, c1) * tx * (1.0 - ty)
            + self.get(r1, c0) * (1.0 - tx) * ty
            + self.get(r1, c1) * tx * ty
    }

    /// Sample many coordinates.
    pub fn sample_many(&self, xy: impl Iterator<Item = (f64, f64)>) -> Vec<f64> {
        xy.map(|(x, y)| self.sample(x, y)).collect()
    }

    /// Rasterise point values on a grid of `resolution` m cells.
    ///
    /// `bounds` is `(xmin, ymin, xmax, ymax)` in metres. The grid's corner is
    /// exactly `(xmin, ymin)` and it has `floor((xmax - xmin) / resolution) + 1`
    /// columns (rows likewise), so that a point lying exactly at `xmax` has a
    /// cell. The consequence to know about: when the extent is a whole number
    /// of cells, the grid is one column and one row larger than
    /// `extent / resolution` - bounds `(0, 0, 20, 20)` at 0.5 m give 41 x 41
    /// cells reaching to 20.5 m. For exactly `n` columns from `xmin`, pass
    /// `xmax = xmin + (n - 0.5) * resolution`, as the catalogue engine does.
    /// Points outside the grid are left out.
    ///
    /// Without `bounds` the grid covers the points with finite coordinates:
    /// its corner is their minimum snapped down to a multiple of
    /// `resolution`, and the same `floor + 1` rule holds their maximum.
    ///
    /// A point with a NaN or infinite coordinate is ignored.
    ///
    /// # Errors
    /// A `resolution` that is not a positive, finite number; non-finite or
    /// reversed `bounds`; or, without `bounds`, no point with finite
    /// coordinates.
    pub fn from_points(
        xy: impl Iterator<Item = (f64, f64)> + Clone,
        values: impl Iterator<Item = f64>,
        resolution: f64,
        reducer: Reducer,
        bounds: Option<(f64, f64, f64, f64)>,
        fill: f64,
    ) -> Result<Raster> {
        if !(resolution.is_finite() && resolution > 0.0) {
            return Err(Error::invalid(format!("resolution must be a positive number of metres, got {resolution}")));
        }
        let (xmin, ymin, xmax, ymax) = match bounds {
            Some(b) => {
                if !(b.0.is_finite() && b.1.is_finite() && b.2.is_finite() && b.3.is_finite()) || b.2 < b.0 || b.3 < b.1 {
                    return Err(Error::invalid(format!("bounds must be finite (xmin, ymin, xmax, ymax), got {b:?}")));
                }
                b
            }
            None => {
                let mut lo = (f64::INFINITY, f64::INFINITY);
                let mut hi = (f64::NEG_INFINITY, f64::NEG_INFINITY);
                for (x, y) in xy.clone() {
                    if !(x.is_finite() && y.is_finite()) {
                        continue;
                    }
                    lo.0 = lo.0.min(x);
                    lo.1 = lo.1.min(y);
                    hi.0 = hi.0.max(x);
                    hi.1 = hi.1.max(y);
                }
                if !lo.0.is_finite() {
                    return Err(Error::invalid("cannot rasterise an empty point set"));
                }
                ((lo.0 / resolution).floor() * resolution, (lo.1 / resolution).floor() * resolution, hi.0, hi.1)
            }
        };
        let ncols = (((xmax - xmin) / resolution).floor() as usize + 1).max(1);
        let nrows = (((ymax - ymin) / resolution).floor() as usize + 1).max(1);
        let size = nrows * ncols;
        let init = match reducer {
            Reducer::Min => f64::INFINITY,
            Reducer::Max => f64::NEG_INFINITY,
            _ => 0.0,
        };
        let mut acc = vec![init; size];
        let mut counts = vec![0u32; size];
        let mut r = Raster::filled(nrows, ncols, xmin, ymin, resolution, fill);
        for ((x, y), v) in xy.zip(values) {
            let (row, col) = r.cell_index(x, y);
            if !r.in_bounds(row, col) {
                continue;
            }
            let i = row as usize * ncols + col as usize;
            counts[i] += 1;
            match reducer {
                Reducer::Min => acc[i] = acc[i].min(v),
                Reducer::Max => acc[i] = acc[i].max(v),
                Reducer::Mean => acc[i] += v,
                Reducer::Count => {}
            }
        }
        for i in 0..size {
            r.data[i] = match reducer {
                Reducer::Count => counts[i] as f64,
                _ if counts[i] == 0 => fill,
                Reducer::Mean => acc[i] / counts[i] as f64,
                _ => acc[i],
            };
        }
        Ok(r)
    }

    /// Fill NaN cells with the value of the nearest non-NaN cell (brute-force BFS by rings).
    pub fn fill_nearest(&mut self) {
        let nan: Vec<(usize, usize)> = (0..self.nrows)
            .flat_map(|r| (0..self.ncols).map(move |c| (r, c)))
            .filter(|&(r, c)| self.get(r, c).is_nan())
            .collect();
        if nan.is_empty() || nan.len() == self.data.len() {
            return;
        }
        // Multi-source BFS from valid cells (4-connectivity), a good enough
        // approximation of nearest-neighbour fill for terrain gaps.
        let mut out = self.data.clone();
        let mut queue: std::collections::VecDeque<(usize, usize)> = (0..self.nrows)
            .flat_map(|r| (0..self.ncols).map(move |c| (r, c)))
            .filter(|&(r, c)| !self.get(r, c).is_nan())
            .collect();
        while let Some((r, c)) = queue.pop_front() {
            let v = out[r * self.ncols + c];
            let nb = [
                (r.wrapping_sub(1), c),
                (r + 1, c),
                (r, c.wrapping_sub(1)),
                (r, c + 1),
            ];
            for (nr, nc) in nb {
                if nr < self.nrows && nc < self.ncols && out[nr * self.ncols + nc].is_nan() {
                    out[nr * self.ncols + nc] = v;
                    queue.push_back((nr, nc));
                }
            }
        }
        self.data = out;
    }

    /// Shaded relief: how brightly each cell would be lit by a distant light
    /// `azimuth` degrees clockwise from north and `altitude` degrees above the
    /// horizon, the same calculation as `gdaldem hillshade`.
    ///
    /// Values run from 0, for a face turned away from the light, to 255 for
    /// one square on to it; a cell with no data stays NaN. The result is a
    /// picture of the surface rather than a measurement of it - a way to see
    /// the shape of a DTM or the texture of a CHM, and the usual backdrop to
    /// draw other layers over.
    ///
    /// `z_factor` scales the heights before the slopes are taken, so 2.0
    /// exaggerates the relief twofold; it is also where a unit conversion
    /// goes if z is not in the units of x and y (feet over metres: 0.3048).
    ///
    /// The slope at each cell is Horn's (1981) weighted average over the
    /// eight neighbours, as GDAL and ArcGIS compute it. Where a neighbour
    /// is off the edge of the grid or has no data the centre cell stands in
    /// for it, which flattens the estimate there a little instead of losing
    /// a ring of cells around every edge and hole.
    pub fn hillshade(&self, azimuth: f64, altitude: f64, z_factor: f64) -> Raster {
        let mut out = Raster::filled(self.nrows, self.ncols, self.xmin, self.ymin, self.resolution, f64::NAN);
        // The light as a unit vector pointing from the ground up towards the
        // source. Azimuth is a compass bearing, clockwise from north, so it
        // is sin for the eastward component and cos for the northward one -
        // the opposite way round from the usual maths convention.
        let (az, alt) = (azimuth.to_radians(), altitude.to_radians());
        let (lx, ly, lz) = (az.sin() * alt.cos(), az.cos() * alt.cos(), alt.sin());
        for r in 0..self.nrows {
            for c in 0..self.ncols {
                let z = self.get(r, c);
                if !z.is_finite() {
                    continue;
                }
                // The 3x3 window as `w[north step + 1][east step + 1]`, so
                // `w[2][2]` is the north-east neighbour and `w[1][1]` the
                // cell itself. Starting from the centre value everywhere
                // leaves it standing in for any neighbour that is missing.
                let mut w = [[z; 3]; 3];
                for dr in -1i64..=1 {
                    for dc in -1i64..=1 {
                        let (rr, cc) = (r as i64 + dr, c as i64 + dc);
                        if self.in_bounds(rr, cc) {
                            let v = self.get(rr as usize, cc as usize);
                            if v.is_finite() {
                                w[(dr + 1) as usize][(dc + 1) as usize] = v;
                            }
                        }
                    }
                }
                // Horn's slopes: the rise from one side of the window to the
                // other over the run across it, counting the middle of each
                // side twice. `gx` rises towards the east, `gy` towards the
                // north.
                let run = 8.0 * self.resolution;
                let gx = z_factor * ((w[0][2] + 2.0 * w[1][2] + w[2][2]) - (w[0][0] + 2.0 * w[1][0] + w[2][0])) / run;
                let gy = z_factor * ((w[2][0] + 2.0 * w[2][1] + w[2][2]) - (w[0][0] + 2.0 * w[0][1] + w[0][2])) / run;
                // Brightness is the cosine of the angle between the light and
                // the surface, which is the dot product of the two unit
                // vectors. A surface rising by `(gx, gy)` has normal
                // `(-gx, -gy, 1)`, and the square root normalises it.
                // Negative means the cell faces away from the light, so it
                // sits in its own shadow and clamps to black.
                let lit = (lz - gx * lx - gy * ly) / (gx * gx + gy * gy + 1.0).sqrt();
                out.set(r, c, 255.0 * lit.max(0.0));
            }
        }
        out
    }

    /// Grey morphological opening (min then max) with a square window of `half` cells.
    pub fn grey_opening(&self, half: usize) -> Raster {
        let eroded = self.window_reduce(half, f64::min, f64::INFINITY);
        eroded.window_reduce(half, f64::max, f64::NEG_INFINITY)
    }

    /// Mean filter with a square window of `half` cells (NaN-aware).
    pub fn smooth(&self, half: usize) -> Raster {
        let mut out = self.clone();
        for r in 0..self.nrows {
            for c in 0..self.ncols {
                let mut s = 0.0;
                let mut n = 0.0;
                for rr in r.saturating_sub(half)..=(r + half).min(self.nrows - 1) {
                    for cc in c.saturating_sub(half)..=(c + half).min(self.ncols - 1) {
                        let v = self.get(rr, cc);
                        if v.is_finite() {
                            s += v;
                            n += 1.0;
                        }
                    }
                }
                out.set(r, c, if n > 0.0 { s / n } else { f64::NAN });
            }
        }
        out
    }

    /// `f` (a minimum or a maximum) over the finite cells of the square
    /// window of `half` cells around each cell; NaN where the window holds
    /// none. Minimum and maximum over a square are those over its rows and
    /// then over the columns of the result, exactly, so the work is two
    /// passes of `2 half + 1` cells and not one of `(2 half + 1)^2`.
    fn window_reduce(&self, half: usize, f: fn(f64, f64) -> f64, init: f64) -> Raster {
        let (nr, nc) = (self.nrows, self.ncols);
        let mut rows = vec![init; self.data.len()];
        for r in 0..nr {
            for c in 0..nc {
                let mut v = init;
                for cc in c.saturating_sub(half)..=(c + half).min(nc - 1) {
                    let x = self.data[r * nc + cc];
                    if x.is_finite() {
                        v = f(v, x);
                    }
                }
                rows[r * nc + c] = v;
            }
        }
        let mut out = self.clone();
        for r in 0..nr {
            for c in 0..nc {
                let mut v = init;
                for rr in r.saturating_sub(half)..=(r + half).min(nr - 1) {
                    v = f(v, rows[rr * nc + c]);
                }
                out.data[r * nc + c] = if v.is_finite() { v } else { f64::NAN };
            }
        }
        out
    }

    /// Write an ESRI ASCII grid.
    pub fn write_ascii_grid(&self, path: impl AsRef<Path>, nodata: f64) -> Result<()> {
        let mut f = std::io::BufWriter::new(std::fs::File::create(path)?);
        writeln!(f, "ncols {}", self.ncols)?;
        writeln!(f, "nrows {}", self.nrows)?;
        writeln!(f, "xllcorner {}", self.xmin)?;
        writeln!(f, "yllcorner {}", self.ymin)?;
        writeln!(f, "cellsize {}", self.resolution)?;
        writeln!(f, "NODATA_value {nodata}")?;
        for r in (0..self.nrows).rev() {
            let row: Vec<String> = (0..self.ncols)
                .map(|c| {
                    let v = self.get(r, c);
                    if v.is_nan() {
                        format!("{nodata}")
                    } else {
                        format!("{v:.4}")
                    }
                })
                .collect();
            writeln!(f, "{}", row.join(" "))?;
        }
        Ok(())
    }

    /// Read an ESRI ASCII grid.
    pub fn read_ascii_grid(path: impl AsRef<Path>) -> Result<Raster> {
        let text = std::fs::read_to_string(path.as_ref())?;
        let mut lines = text.lines();
        let mut ncols = 0usize;
        let mut nrows = 0usize;
        let mut xll = 0.0;
        let mut yll = 0.0;
        let mut cell = 1.0;
        let mut nodata = -9999.0;
        let mut centre = false;
        let mut data_lines = Vec::new();
        for line in lines.by_ref() {
            let mut it = line.split_whitespace();
            let Some(key) = it.next() else { continue };
            let val = it.next().unwrap_or("");
            match key.to_ascii_lowercase().as_str() {
                "ncols" => ncols = val.parse().map_err(|_| Error::file(path.as_ref(), "bad ncols"))?,
                "nrows" => nrows = val.parse().map_err(|_| Error::file(path.as_ref(), "bad nrows"))?,
                "xllcorner" => xll = val.parse().map_err(|_| Error::file(path.as_ref(), "bad xllcorner"))?,
                "yllcorner" => yll = val.parse().map_err(|_| Error::file(path.as_ref(), "bad yllcorner"))?,
                "xllcenter" => {
                    xll = val.parse().map_err(|_| Error::file(path.as_ref(), "bad xllcenter"))?;
                    centre = true;
                }
                "yllcenter" => {
                    yll = val.parse().map_err(|_| Error::file(path.as_ref(), "bad yllcenter"))?;
                    centre = true;
                }
                "cellsize" => cell = val.parse().map_err(|_| Error::file(path.as_ref(), "bad cellsize"))?,
                "nodata_value" => nodata = val.parse().unwrap_or(-9999.0),
                _ => {
                    data_lines.push(line);
                    break;
                }
            }
        }
        data_lines.extend(lines);
        if centre {
            xll -= cell / 2.0;
            yll -= cell / 2.0;
        }
        let mut r = Raster::filled(nrows, ncols, xll, yll, cell, f64::NAN);
        let mut vals = data_lines.iter().flat_map(|l| l.split_whitespace());
        for row in (0..nrows).rev() {
            for col in 0..ncols {
                let v: f64 = vals
                    .next()
                    .ok_or_else(|| Error::file(path.as_ref(), "truncated grid"))?
                    .parse()
                    .map_err(|_| Error::file(path.as_ref(), "bad grid value"))?;
                r.set(row, col, if v == nodata { f64::NAN } else { v });
            }
        }
        Ok(r)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cell_indices_are_floored_and_unclipped() {
        let (r, c) = cell_indices(10.0, -5.0, 0.5, &[10.0, 9.9, 11.26, f64::NAN], &[-5.0, -5.01, 0.0, 0.0]);
        assert_eq!(c, vec![0, -1, 2, i64::MIN]);
        assert_eq!(r, vec![0, -1, 10, 10]);
    }

    #[test]
    fn hillshade_follows_the_light() {
        // A plane rising one metre per metre towards the east, with a hole in
        // it. Such a slope turns its face *westwards*, so the light has to
        // come from the west to fall on it square.
        let mut r = Raster::filled(7, 7, 0.0, 0.0, 1.0, 0.0);
        for row in 0..7 {
            for col in 0..7 {
                r.set(row, col, col as f64);
            }
        }
        r.set(3, 3, f64::NAN);
        let from_west = r.hillshade(270.0, 45.0, 1.0);
        let from_east = r.hillshade(90.0, 45.0, 1.0);
        // A 45 degree slope lit from 45 degrees above the opposite side is as
        // bright as a surface can be; from behind it is in its own shadow.
        assert!((from_west.get(1, 1) - 255.0).abs() < 1e-9, "{}", from_west.get(1, 1));
        assert_eq!(from_east.get(1, 1), 0.0);
        // The hole stays a hole, and the cells around it are still shaded.
        assert!(from_west.get(3, 3).is_nan());
        assert!(from_west.get(3, 2) > 250.0, "{}", from_west.get(3, 2));

        // Flat ground takes the light at its own altitude, whatever the bearing.
        let flat = Raster::filled(3, 3, 0.0, 0.0, 1.0, 7.0).hillshade(315.0, 30.0, 1.0);
        for v in flat.data {
            assert!((v - 255.0 * 0.5).abs() < 1e-9, "{v}");
        }
    }

    #[test]
    fn a_point_without_a_position_is_not_binned() {
        // UTM-scale coordinates; the second point has no easting.
        let xy = [(512_010.0, 6_234_010.0), (f64::NAN, 6_234_010.2), (512_012.0, 6_234_012.0), (512_011.0, f64::INFINITY)];
        let b = Some((512_000.0, 6_234_000.0, 512_020.0, 6_234_020.0));
        let r = Raster::from_points(xy.iter().copied(), [1.0, 30.0, 2.0, 40.0].into_iter(), 1.0, Reducer::Max, b, 0.0).unwrap();
        assert_eq!(r.data.iter().cloned().fold(0.0, f64::max), 2.0);
        assert_eq!(r.get(10, 0), 0.0);
        assert_eq!(r.cell_index(f64::NAN, 6_234_010.2), (10, i64::MIN));
        assert!(!r.in_bounds(10, i64::MIN));
        // Without bounds the extent is that of the points that have one.
        let free = Raster::from_points(xy.iter().copied(), [1.0, 30.0, 2.0, 40.0].into_iter(), 1.0, Reducer::Max, None, 0.0).unwrap();
        assert_eq!((free.nrows, free.ncols, free.xmin, free.ymin), (3, 3, 512_010.0, 6_234_010.0));
    }

    #[test]
    fn bad_resolutions_and_bounds_are_refused() {
        let xy = [(512_010.0, 6_234_010.0)];
        for res in [0.0, -1.0, f64::NAN, f64::INFINITY] {
            assert!(Raster::from_points(xy.iter().copied(), [1.0].into_iter(), res, Reducer::Max, None, 0.0).is_err(), "{res}");
        }
        for b in [(0.0, 0.0, f64::NAN, 1.0), (5.0, 0.0, 1.0, 1.0), (0.0, 0.0, f64::INFINITY, 1.0)] {
            assert!(Raster::from_points(xy.iter().copied(), [1.0].into_iter(), 1.0, Reducer::Max, Some(b), 0.0).is_err(), "{b:?}");
        }
        // The documented size: a whole number of cells gets one more each way.
        let r = Raster::from_points(xy.iter().copied(), [1.0].into_iter(), 0.5, Reducer::Max, Some((0.0, 0.0, 20.0, 20.0)), 0.0).unwrap();
        assert_eq!((r.nrows, r.ncols, r.xmax()), (41, 41, 20.5));
        let r = Raster::from_points(xy.iter().copied(), [1.0].into_iter(), 0.5, Reducer::Max, Some((0.0, 0.0, 19.75, 19.75)), 0.0).unwrap();
        assert_eq!((r.nrows, r.ncols, r.xmax()), (40, 40, 20.0));
    }

    #[test]
    fn opening_is_that_of_the_whole_window() {
        // The two-pass minimum and maximum against the window taken whole.
        let mut s = 7u64;
        let mut unit = || {
            s ^= s << 13;
            s ^= s >> 7;
            s ^= s << 17;
            (s >> 11) as f64 / (1u64 << 53) as f64
        };
        let mut r = Raster::filled(23, 31, 512_000.0, 6_234_000.0, 0.5, 0.0);
        for v in r.data.iter_mut() {
            let u = unit();
            *v = if u < 0.15 { f64::NAN } else { 100.0 * u };
        }
        for v in r.data[..31 * 4].iter_mut() {
            *v = f64::NAN;
        }
        let whole = |src: &Raster, half: usize, f: fn(f64, f64) -> f64, init: f64| {
            let mut out = src.clone();
            for row in 0..src.nrows {
                for col in 0..src.ncols {
                    let mut v = init;
                    for rr in row.saturating_sub(half)..=(row + half).min(src.nrows - 1) {
                        for cc in col.saturating_sub(half)..=(col + half).min(src.ncols - 1) {
                            let x = src.get(rr, cc);
                            if x.is_finite() {
                                v = f(v, x);
                            }
                        }
                    }
                    out.set(row, col, if v.is_finite() { v } else { f64::NAN });
                }
            }
            out
        };
        for half in [0, 1, 3, 40] {
            let expect = whole(&whole(&r, half, f64::min, f64::INFINITY), half, f64::max, f64::NEG_INFINITY);
            let got = r.grey_opening(half);
            assert!(got.data.iter().zip(&expect.data).all(|(a, b)| a.to_bits() == b.to_bits() || (a.is_nan() && b.is_nan())), "half {half}");
        }
    }

    #[test]
    fn cell_centers_follow_the_data_layout() {
        let (x, y) = cell_centers(2, 3, 1.0, 2.0, 2.0);
        assert_eq!(x, vec![2.0, 4.0, 6.0, 2.0, 4.0, 6.0]);
        assert_eq!(y, vec![3.0, 3.0, 3.0, 5.0, 5.0, 5.0]);
        let r = Raster::filled(2, 3, 1.0, 2.0, 2.0, 0.0);
        assert_eq!(r.cell_center(1, 2), (x[5], y[5]));
    }
}