surtgis 1.10.0

High-performance geospatial analysis CLI
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
//! `surtgis hydrology lake-catchment`: lake-aware catchments for many lakes.
//!
//! Per lake: a square UTM window around it; every DEM source warped onto
//! that window (STAC items read by range, or local tiles such as FABDEM);
//! the lake rasterized from its polygon (or extracted from a water mask
//! for point lakes); [`lake_catchment`] with the lake as the outlet; the
//! window doubled while the catchment or the lake touches its edge. One
//! output row per lake and DEM; with two or more DEMs, `dem_spread` says
//! how much they disagree.

use std::path::{Path, PathBuf};
use std::sync::Mutex;

use anyhow::{Context, Result, bail};
use geo::{BoundingRect, Centroid, Geometry, MapCoords};
use rayon::prelude::*;
use surtgis_algorithms::hydrology::{
    LakeCatchmentParams, WaterBodyParams, lake_catchment, water_body_at,
};
use surtgis_cloud::blocking::{CogReaderBlocking, StacClientBlocking};
use surtgis_cloud::{BBox, CogReaderOptions, StacCatalog, StacClientOptions, StacSearchParams};
use surtgis_core::io::window::{geotiff_info, read_geotiff_window_bands};
use surtgis_core::vector::{AttributeValue, FeatureCollection, rasterize_polygons, read_vector};
use surtgis_core::warp::{self, GridSpec, Resampling, Transformer};
use surtgis_core::{CRS, GeoTransform, Raster};

/// Options of `hydrology lake-catchment`, as parsed from the command line.
pub struct Args {
    pub lakes: PathBuf,
    pub id_field: Option<String>,
    pub dems: Vec<String>,
    pub water_masks: Vec<String>,
    pub water_value: f64,
    pub min_lake_cells: usize,
    pub max_neck_cells: usize,
    pub min_compactness: f64,
    pub window_km: f64,
    pub max_window_km: f64,
    pub pixel_size: f64,
    pub jobs: usize,
    pub retries: u32,
    pub output: PathBuf,
    pub masks_dir: Option<PathBuf>,
    pub sink_depth: Option<f64>,
}

/// A raster source that can be warped onto any window grid.
enum Source {
    /// STAC collection; each item's `asset` is a COG read by range.
    Stac {
        catalog: String,
        collection: String,
        asset: String,
        /// One client per source, shared by every lake and window, so the
        /// SAS-token cache is reused (Planetary Computer rate-limits signing).
        client: std::sync::OnceLock<StacClientBlocking>,
    },
    /// Local GeoTIFF/COG tiles (a file, or every `.tif` under a directory).
    Tiles(Vec<Tile>),
}

struct Tile {
    path: PathBuf,
    epsg: u32,
    /// Bounds in the tile's own CRS.
    bounds: (f64, f64, f64, f64),
}

struct NamedSource {
    label: String,
    source: Source,
}

/// Which cells of a water raster are water.
#[derive(Clone, Copy, Debug)]
enum WaterRule {
    Eq(f64),
    Ge(f64),
    Gt(f64),
}

impl WaterRule {
    fn is_water(self, v: f64) -> bool {
        v.is_finite()
            && match self {
                Self::Eq(x) => (v - x).abs() < 0.5,
                Self::Ge(x) => v >= x,
                Self::Gt(x) => v > x,
            }
    }
}

/// A water raster and its rule, e.g. `stac:pc:jrc-gsw:occurrence>=90`.
struct WaterSource {
    src: NamedSource,
    rule: WaterRule,
}

/// Split an optional trailing rule (`>=v`, `>v`, `=v`) off a water-mask
/// spec; without one, water is `== default`.
fn parse_water(spec: &str, default: f64) -> Result<WaterSource> {
    let (base, rule) = if let Some(i) = spec.rfind(">=") {
        (
            &spec[..i],
            WaterRule::Ge(spec[i + 2..].trim().parse().context("water rule value")?),
        )
    } else if let Some(i) = spec.rfind('>') {
        (
            &spec[..i],
            WaterRule::Gt(spec[i + 1..].trim().parse().context("water rule value")?),
        )
    } else if let Some(i) = spec
        .rfind('=')
        .filter(|&i| spec[i + 1..].trim().parse::<f64>().is_ok())
    {
        (&spec[..i], WaterRule::Eq(spec[i + 1..].trim().parse()?))
    } else {
        (spec, WaterRule::Eq(default))
    };
    Ok(WaterSource {
        src: parse_source(base)?,
        rule,
    })
}

/// One input lake: id, geometry in EPSG:4326 and a representative point.
struct Lake {
    id: String,
    geom: Geometry<f64>,
    lon: f64,
    lat: f64,
    is_point: bool,
}

/// One output row.
struct Row {
    lake_id: String,
    dem: String,
    lon: f64,
    lat: f64,
    utm_epsg: u32,
    window_km: f64,
    lake_area_km2: f64,
    lake_perimeter_km: f64,
    lake_elev_m: f64,
    catchment_area_km2: f64,
    local_catchment_area_km2: f64,
    catchment_mean_elev_m: f64,
    water_trimmed: Option<f64>,
    lake_source: String,
    lake_compactness: f64,
    edge_reached: bool,
    status: String,
    message: String,
}

pub fn run(a: Args) -> Result<()> {
    if a.dems.is_empty() {
        bail!(
            "give at least one --dem (stac:<catalog>:<collection>[:asset] or a local file/directory)"
        );
    }
    let dems: Vec<NamedSource> = a
        .dems
        .iter()
        .map(|s| parse_source(s))
        .collect::<Result<_>>()?;
    let water: Vec<WaterSource> = a
        .water_masks
        .iter()
        .map(|s| parse_water(s, a.water_value))
        .collect::<Result<_>>()?;
    let lakes = read_lakes(&a.lakes, a.id_field.as_deref())?;
    if lakes.iter().any(|l| l.is_point) && water.is_empty() {
        bail!(
            "{} has point lakes: give --water-mask (e.g. stac:pc:esa-worldcover:map) to extract \
             each lake, or pass lake polygons",
            a.lakes.display()
        );
    }
    if let Some(dir) = &a.masks_dir {
        std::fs::create_dir_all(dir)?;
    }
    eprintln!(
        "lake-catchment: {} lakes x {} DEM(s), window {}–{} km, {} jobs",
        lakes.len(),
        dems.len(),
        a.window_km,
        a.max_window_km,
        a.jobs
    );

    let done = Mutex::new(0usize);
    let pool = rayon::ThreadPoolBuilder::new()
        .num_threads(a.jobs.max(1))
        .build()
        .context("thread pool")?;
    let mut rows: Vec<Row> = pool.install(|| {
        lakes
            .par_iter()
            .flat_map_iter(|lake| {
                let out: Vec<Row> = dems.iter().map(|d| one(lake, d, &water, &a)).collect();
                let mut n = done.lock().unwrap_or_else(|e| e.into_inner());
                *n += 1;
                let summary: Vec<String> = out
                    .iter()
                    .map(|r| {
                        if r.status == "ok" {
                            format!("{} {:.2} km²", r.dem, r.catchment_area_km2)
                        } else {
                            format!("{} {}", r.dem, r.status)
                        }
                    })
                    .collect();
                eprintln!(
                    "  [{}/{}] {}: {}",
                    *n,
                    lakes.len(),
                    lake.id,
                    summary.join(", ")
                );
                out
            })
            .collect()
    });

    // Spread across DEMs (max / min catchment area among successful runs).
    let mut spread = std::collections::HashMap::<String, f64>::new();
    for id in lakes.iter().map(|l| &l.id) {
        let v: Vec<f64> = rows
            .iter()
            .filter(|r| {
                &r.lake_id == id
                    && r.status != "error"
                    && r.status != "edge"
                    && r.catchment_area_km2 > 0.0
            })
            .map(|r| r.catchment_area_km2)
            .collect();
        if v.len() >= 2 {
            let (mn, mx) = v
                .iter()
                .fold((f64::INFINITY, 0.0f64), |(a, b), &x| (a.min(x), b.max(x)));
            spread.insert(id.clone(), mx / mn);
        }
    }
    rows.sort_by(|x, y| x.lake_id.cmp(&y.lake_id).then(x.dem.cmp(&y.dem)));
    write_csv(&a.output, &rows, &spread)?;
    let count = |s: &str| rows.iter().filter(|r| r.status == s).count();
    println!(
        "Lake catchments: {}/{} lake x DEM runs ok ({} review, {} point_only, {} edge, {} error) -> {}",
        count("ok"),
        rows.len(),
        count("review"),
        count("point_only"),
        count("edge"),
        count("error"),
        a.output.display()
    );
    Ok(())
}

/// Delineate one lake with one DEM, enlarging the window as needed.
fn one(lake: &Lake, dem: &NamedSource, water: &[WaterSource], a: &Args) -> Row {
    let zone = (((lake.lon + 180.0) / 6.0).floor() as i64).clamp(0, 59) as u32 + 1;
    let epsg = if lake.lat >= 0.0 {
        32600 + zone
    } else {
        32700 + zone
    };
    let mut row = Row {
        lake_id: lake.id.clone(),
        dem: dem.label.clone(),
        lon: lake.lon,
        lat: lake.lat,
        utm_epsg: epsg,
        window_km: 0.0,
        lake_area_km2: f64::NAN,
        lake_perimeter_km: f64::NAN,
        lake_elev_m: f64::NAN,
        catchment_area_km2: f64::NAN,
        local_catchment_area_km2: f64::NAN,
        catchment_mean_elev_m: f64::NAN,
        water_trimmed: None,
        lake_source: if lake.is_point {
            String::new()
        } else {
            "polygon".into()
        },
        lake_compactness: f64::NAN,
        edge_reached: false,
        status: "error".into(),
        message: String::new(),
    };
    let result = (|| -> Result<()> {
        let to_utm = Transformer::new(4326, epsg)?;
        let (cx, cy) = to_utm
            .forward(lake.lon, lake.lat)
            .context("lake centre cannot be projected to UTM")?;
        // Start from the lake's own size when it has a polygon.
        let lake_utm: Option<Geometry<f64>> = if lake.is_point {
            None
        } else {
            Some(project_geometry(&lake.geom, &to_utm)?)
        };
        let lake_extent_km = lake_utm
            .as_ref()
            .and_then(|g| g.bounding_rect())
            .map(|r| r.width().max(r.height()) / 1000.0)
            .unwrap_or(0.0);
        let mut win_km = a.window_km.max(lake_extent_km * 3.0);
        loop {
            row.window_km = win_km;
            let half = win_km * 500.0;
            let n = ((2.0 * half) / a.pixel_size).ceil() as usize;
            let grid = GridSpec {
                transform: GeoTransform::new(cx - half, cy + half, a.pixel_size, -a.pixel_size),
                rows: n,
                cols: n,
                epsg,
            };
            let z = build_on_grid(&dem.source, &grid, Resampling::Nearest, a.retries)
                .with_context(|| format!("DEM {}", dem.label))?;
            let mut notes: Vec<String> = Vec::new();
            let lake_mask = match &lake_utm {
                Some(g) => rasterize_lake(g, &grid, cx, cy)?,
                None => {
                    // Water masks in order; the first that has data here and a
                    // water body of at least --min-lake-cells near the point wins.
                    let (c, r) = grid_cell(&grid, cx, cy);
                    let mut found = None;
                    let mut any_mask = false;
                    for w in water {
                        let wm = match build_on_grid(
                            &w.src.source,
                            &grid,
                            Resampling::Nearest,
                            a.retries,
                        ) {
                            Ok(m) => m,
                            Err(e) => {
                                notes.push(format!("{}: {e:#}", w.src.label));
                                continue;
                            }
                        };
                        any_mask = true;
                        let mut bin = Raster::<u8>::new(n, n);
                        bin.set_transform(grid.transform);
                        for (o, v) in bin.data_mut().iter_mut().zip(wm.data().iter()) {
                            *o = u8::from(w.rule.is_water(*v));
                        }
                        let mut wp = WaterBodyParams::default();
                        wp.max_neck_cells = a.max_neck_cells;
                        match water_body_at(&bin, r, c, wp) {
                            Some(wb) if wb.cells >= a.min_lake_cells => {
                                found = Some((wb, w.src.label.clone()));
                                break;
                            }
                            Some(wb) => notes.push(format!(
                                "{}: water body of {} cells (< --min-lake-cells {})",
                                w.src.label, wb.cells, a.min_lake_cells
                            )),
                            None => {
                                notes.push(format!("{}: no water within 10 cells", w.src.label))
                            }
                        }
                    }
                    if found.is_none() && !any_mask {
                        // Every mask failed to load: an error, not a lake
                        // without a polygon.
                        bail!("no water mask could be read: {}", notes.join("; "));
                    }
                    match found {
                        Some((wb, label)) => {
                            row.water_trimmed =
                                Some(wb.raw_component_cells as f64 / wb.cells.max(1) as f64);
                            row.lake_source = label;
                            wb.mask
                        }
                        None => {
                            // No usable polygon: the cell under the point is the lake.
                            row.lake_source = "point".into();
                            let mut m = Raster::<u8>::new(n, n);
                            m.set_transform(grid.transform);
                            m.data_mut()[(r, c)] = 1;
                            m
                        }
                    }
                }
            };
            let lc = lake_catchment(&z, &lake_mask, LakeCatchmentParams::default())?;
            let edge = lc.touches_edge || lc.lake_touches_edge;
            if !edge || win_km * 2.0 > a.max_window_km {
                row.lake_area_km2 = lc.lake_area / 1e6;
                row.lake_perimeter_km = lc.lake_perimeter / 1e3;
                row.lake_elev_m = lc.lake_elevation;
                row.catchment_area_km2 = lc.catchment_area / 1e6;
                row.catchment_mean_elev_m = lc.catchment_mean_elevation;
                if let Some(d) = a.sink_depth {
                    let mut p = LakeCatchmentParams::default();
                    p.sink_depth = Some(d);
                    let local = lake_catchment(&z, &lake_mask, p)?;
                    row.local_catchment_area_km2 = local.catchment_area / 1e6;
                }
                row.edge_reached = edge;
                row.lake_compactness = if lc.lake_perimeter > 0.0 {
                    4.0 * std::f64::consts::PI * lc.lake_area / lc.lake_perimeter.powi(2)
                } else {
                    f64::NAN
                };
                let from_mask = lake.is_point && row.lake_source != "point";
                let riverish = from_mask
                    && (row.lake_compactness < a.min_compactness
                        || row.water_trimmed.is_some_and(|t| t > 2.0));
                row.status = if edge {
                    "edge".into()
                } else if row.lake_source == "point" {
                    "point_only".into()
                } else if riverish {
                    "review".into()
                } else {
                    "ok".into()
                };
                if edge {
                    notes.push(format!(
                        "catchment still reaches the edge of a {win_km} km window \
                         (--max-window-km {}); areas are lower bounds",
                        a.max_window_km
                    ));
                }
                if row.lake_source == "point" {
                    notes.push("no water polygon: the lake is the cell under the point".into());
                }
                if riverish {
                    notes.push(format!(
                        "elongated or trimmed water body (compactness {:.3}, trimmed {:.1}); \
                         check it is not a river or a merged lake",
                        row.lake_compactness,
                        row.water_trimmed.unwrap_or(1.0)
                    ));
                }
                row.message = notes.join("; ");
                if let Some(dir) = &a.masks_dir {
                    let name = format!("{}_{}.tif", sanitize(&lake.id), sanitize(&dem.label));
                    let mut m = lc.mask.clone();
                    m.set_crs(Some(CRS::from_epsg(epsg)));
                    surtgis_core::io::write_geotiff(&m, dir.join(name), None)?;
                }
                return Ok(());
            }
            win_km *= 2.0;
        }
    })();
    if let Err(e) = result {
        row.status = "error".into();
        row.message = format!("{e:#}").replace(['\n', ','], " ");
    }
    row
}

/// Cell (col, row) of map coordinate (x, y) on `grid`, clamped.
fn grid_cell(grid: &GridSpec, x: f64, y: f64) -> (usize, usize) {
    let t = &grid.transform;
    let c = ((x - t.origin_x) / t.pixel_width).floor().max(0.0) as usize;
    let r = ((y - t.origin_y) / t.pixel_height).floor().max(0.0) as usize;
    (c.min(grid.cols - 1), r.min(grid.rows - 1))
}

/// Burn a (UTM) lake polygon onto the grid; a lake smaller than a cell
/// keeps at least the cell under its centre.
fn rasterize_lake(g: &Geometry<f64>, grid: &GridSpec, cx: f64, cy: f64) -> Result<Raster<u8>> {
    let mut fc = FeatureCollection::with_crs(Some(CRS::from_epsg(grid.epsg)));
    fc.push(surtgis_core::vector::Feature::new(g.clone()));
    let crs = CRS::from_epsg(grid.epsg);
    let burnt = rasterize_polygons(&fc, &grid.transform, grid.rows, grid.cols, None, Some(&crs))?;
    let mut m = Raster::<u8>::new(grid.rows, grid.cols);
    m.set_transform(grid.transform);
    let mut any = false;
    for (o, v) in m.data_mut().iter_mut().zip(burnt.data().iter()) {
        *o = u8::from(v.is_finite());
        any |= v.is_finite();
    }
    if !any {
        let (c, r) = grid_cell(grid, cx, cy);
        m.data_mut()[(r, c)] = 1;
    }
    Ok(m)
}

/// Warp every piece of `source` that overlaps `grid` onto it; cells no
/// source covers stay NaN. First valid value wins where pieces overlap.
fn build_on_grid(
    source: &Source,
    grid: &GridSpec,
    method: Resampling,
    retries: u32,
) -> Result<Raster<f64>> {
    let mut out = Raster::<f64>::new(grid.rows, grid.cols);
    out.set_transform(grid.transform);
    out.set_crs(Some(CRS::from_epsg(grid.epsg)));
    out.data_mut().fill(f64::NAN);
    let mut merge = |piece: &Raster<f64>, src_epsg: u32| -> Result<()> {
        let tf = Transformer::new(src_epsg, grid.epsg)?;
        let warped = warp::warp(
            std::slice::from_ref(piece),
            piece.transform(),
            &tf,
            grid,
            method,
        )?;
        let nd = piece.nodata();
        for (o, v) in out.data_mut().iter_mut().zip(warped[0].data().iter()) {
            if o.is_nan() && v.is_finite() && nd.is_none_or(|n| *v != n) {
                *o = *v;
            }
        }
        Ok(())
    };
    match source {
        Source::Tiles(tiles) => {
            for t in tiles {
                let tf = Transformer::new(t.epsg, grid.epsg)?;
                let Ok(b) = warp::source_window(grid, &tf, 2) else {
                    continue;
                };
                let (x0, y0, x1, y1) = t.bounds;
                if b.max_x < x0 || b.min_x > x1 || b.max_y < y0 || b.min_y > y1 {
                    continue;
                }
                let info = geotiff_info(&t.path)
                    .with_context(|| format!("reading {}", t.path.display()))?;
                let Some(pw) = info.window_for_bounds(0, b.min_x, b.min_y, b.max_x, b.max_y) else {
                    continue;
                };
                let piece = read_geotiff_window_bands::<f64, _>(&t.path, &info, 0, &pw)?
                    .into_iter()
                    .next()
                    .context("tile has no band")?;
                merge(&piece, t.epsg)?;
            }
        }
        Source::Stac {
            catalog,
            collection,
            asset,
            client,
        } => {
            let to_ll = Transformer::new(grid.epsg, 4326)?;
            let gb = grid.bounds();
            let corners = [
                (gb.min_x, gb.min_y),
                (gb.min_x, gb.max_y),
                (gb.max_x, gb.min_y),
                (gb.max_x, gb.max_y),
            ];
            let ll: Vec<(f64, f64)> = corners
                .iter()
                .filter_map(|&(x, y)| to_ll.forward(x, y))
                .collect();
            if ll.is_empty() {
                bail!("window cannot be projected to lon/lat");
            }
            let (w, e) = ll
                .iter()
                .fold((f64::MAX, f64::MIN), |(a, b), p| (a.min(p.0), b.max(p.0)));
            let (s, n) = ll
                .iter()
                .fold((f64::MAX, f64::MIN), |(a, b), p| (a.min(p.1), b.max(p.1)));
            if client.get().is_none() {
                let c = StacClientBlocking::new(
                    StacCatalog::from_str_or_url(catalog),
                    StacClientOptions::default(),
                )?;
                let _ = client.set(c);
            }
            let client = client.get().expect("set above");
            let params = StacSearchParams::new()
                .bbox(w, s, e, n)
                .collections(&[collection.as_str()])
                .limit(50);
            let items = client.search_all(&params)?;
            if items.is_empty() {
                bail!("no {collection} item covers the window");
            }
            for item in &items {
                let Some(a) = item.asset(asset) else { continue };
                let href = client
                    .sign_asset_href(&a.href, item.collection.as_deref().unwrap_or(collection))?;
                let mut last = None;
                for attempt in 0..retries.max(1) {
                    let res = (|| -> Result<()> {
                        let mut reader =
                            CogReaderBlocking::open(&href, CogReaderOptions::default())?;
                        let src_epsg = item
                            .epsg()
                            .or_else(|| reader.metadata().crs.as_ref().and_then(|c| c.epsg()))
                            .context("item has no EPSG")?;
                        let tf = Transformer::new(src_epsg, grid.epsg)?;
                        let b = warp::source_window(grid, &tf, 2)?;
                        let bb = BBox {
                            min_x: b.min_x,
                            min_y: b.min_y,
                            max_x: b.max_x,
                            max_y: b.max_y,
                        };
                        // Coarsest overview still at least as fine as the
                        // working grid: a 10 m water mask is read at 20 m for
                        // a 30 m grid, and a big window does not pull in
                        // billions of native cells.
                        let ov = coarsest_fine_enough(
                            &reader,
                            src_epsg,
                            grid.transform.pixel_width.abs(),
                        );
                        match reader.read_bbox::<f64>(&bb, ov) {
                            Ok(piece) => merge(&piece, src_epsg),
                            // the item's footprint does not reach the window
                            Err(e) if e.to_string().contains("does not intersect") => Ok(()),
                            Err(e) => Err(e.into()),
                        }
                    })();
                    match res {
                        Ok(()) => {
                            last = None;
                            break;
                        }
                        Err(e) => {
                            last = Some(e);
                            std::thread::sleep(std::time::Duration::from_millis(500 << attempt));
                        }
                    }
                }
                if let Some(e) = last {
                    return Err(e.context(format!("item {}", item.id)));
                }
                let search_url = client.catalog().search_url();
                let root = search_url.strip_suffix("/search").unwrap_or(&search_url);
                surtgis_core::provenance::observe_stac_item(
                    surtgis_core::provenance::StacItemRecord::new(
                        root,
                        item.collection.as_deref().unwrap_or(collection),
                        item.id.as_str(),
                    )
                    .with_assets([asset.clone()]),
                );
            }
        }
    }
    if out.data().iter().all(|v| v.is_nan()) {
        bail!("no data in the window");
    }
    Ok(out)
}

/// Index of the coarsest overview whose pixel is no larger than `target_m`
/// metres (`None` = full resolution). Geographic sources are judged by the
/// north-south size of a degree, the stricter axis.
fn coarsest_fine_enough(reader: &CogReaderBlocking, src_epsg: u32, target_m: f64) -> Option<usize> {
    let meta = reader.metadata();
    let full_px = meta.geo_transform.pixel_width.abs();
    let to_m = if src_epsg == 4326 || (4000..5000).contains(&src_epsg) {
        111_320.0
    } else {
        1.0
    };
    let mut best = None;
    for ov in reader.overviews() {
        if ov.width == 0 {
            continue;
        }
        let px_m = full_px * (meta.width as f64 / ov.width as f64) * to_m;
        if px_m <= target_m * 1.001 {
            best = Some(ov.index);
        }
    }
    best
}

/// `stac:<catalog>:<collection>[:asset]`, or a local file or directory.
fn parse_source(spec: &str) -> Result<NamedSource> {
    if let Some(rest) = spec.strip_prefix("stac:") {
        let parts: Vec<&str> = rest.split(':').collect();
        // catalog may be a URL with ':' (https://…); collection and asset are last
        let (catalog, collection, asset) = match parts.len() {
            2 => (
                parts[0].to_string(),
                parts[1].to_string(),
                "data".to_string(),
            ),
            n if n >= 3 && !parts[0].starts_with("http") => (
                parts[0].to_string(),
                parts[1].to_string(),
                parts[2..].join(":"),
            ),
            n if n >= 4 => (
                parts[..n - 2].join(":"),
                parts[n - 2].to_string(),
                parts[n - 1].to_string(),
            ),
            _ => bail!("'{spec}': expected stac:<catalog>:<collection>[:asset]"),
        };
        return Ok(NamedSource {
            label: collection.clone(),
            source: Source::Stac {
                catalog,
                collection,
                asset,
                client: std::sync::OnceLock::new(),
            },
        });
    }
    let path = Path::new(spec);
    let files: Vec<PathBuf> = if path.is_dir() {
        let mut v = Vec::new();
        collect_tifs(path, &mut v)?;
        v.sort();
        v
    } else if path.is_file() {
        vec![path.to_path_buf()]
    } else {
        bail!("'{spec}' is neither stac:<catalog>:<collection> nor an existing file or directory");
    };
    if files.is_empty() {
        bail!("no .tif under {}", path.display());
    }
    let tiles = files
        .into_iter()
        .map(|p| {
            let info = geotiff_info(&p).with_context(|| format!("reading {}", p.display()))?;
            let epsg = info
                .crs
                .as_ref()
                .and_then(|c| c.epsg())
                .with_context(|| format!("{} has no EPSG code", p.display()))?;
            let t = info.transform;
            let (x0, y0, x1, y1) = t.bounds(info.width as usize, info.height as usize);
            Ok(Tile {
                path: p,
                epsg,
                bounds: (x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)),
            })
        })
        .collect::<Result<Vec<_>>>()?;
    let label = path
        .file_stem()
        .map(|s| s.to_string_lossy().into_owned())
        .unwrap_or_else(|| "dem".into());
    Ok(NamedSource {
        label,
        source: Source::Tiles(tiles),
    })
}

fn collect_tifs(dir: &Path, out: &mut Vec<PathBuf>) -> Result<()> {
    for e in std::fs::read_dir(dir)? {
        let p = e?.path();
        if p.is_dir() {
            collect_tifs(&p, out)?;
        } else if p
            .extension()
            .is_some_and(|x| x.eq_ignore_ascii_case("tif") || x.eq_ignore_ascii_case("tiff"))
        {
            out.push(p);
        }
    }
    Ok(())
}

/// Read lakes (points or polygons) and bring them to EPSG:4326.
fn read_lakes(path: &Path, id_field: Option<&str>) -> Result<Vec<Lake>> {
    let fc = read_vector(path).with_context(|| format!("reading {}", path.display()))?;
    let src_epsg = match fc.crs().and_then(|c| c.epsg()) {
        Some(e) => e,
        None => {
            eprintln!("Warning: {} has no CRS; assuming EPSG:4326", path.display());
            4326
        }
    };
    let to_ll = if src_epsg == 4326 {
        None
    } else {
        Some(Transformer::new(src_epsg, 4326)?)
    };
    let mut lakes = Vec::new();
    for (i, f) in fc.iter().enumerate() {
        let Some(g) = &f.geometry else { continue };
        let g = match &to_ll {
            Some(tf) => project_geometry(g, tf)?,
            None => g.clone(),
        };
        let id = match id_field.and_then(|k| f.properties.get(k)) {
            Some(AttributeValue::String(s)) => s.clone(),
            Some(AttributeValue::Int(v)) => v.to_string(),
            Some(AttributeValue::Float(v)) => v.to_string(),
            Some(_) | None => f.id.clone().unwrap_or_else(|| (i + 1).to_string()),
        };
        let (is_point, c) = match &g {
            Geometry::Point(p) => (true, *p),
            Geometry::Polygon(_) | Geometry::MultiPolygon(_) => {
                (false, g.centroid().context("lake polygon has no centroid")?)
            }
            other => bail!(
                "feature {id}: lakes must be points or polygons, got {:?}",
                std::mem::discriminant(other)
            ),
        };
        lakes.push(Lake {
            id,
            geom: g,
            lon: c.x(),
            lat: c.y(),
            is_point,
        });
    }
    if lakes.is_empty() {
        bail!("{} has no lake features", path.display());
    }
    Ok(lakes)
}

fn project_geometry(g: &Geometry<f64>, tf: &Transformer) -> Result<Geometry<f64>> {
    let failed = std::cell::Cell::new(false);
    let out = g.map_coords(|c| match tf.forward(c.x, c.y) {
        Some((x, y)) => geo::coord! { x: x, y: y },
        None => {
            failed.set(true);
            c
        }
    });
    if failed.get() {
        bail!("geometry cannot be projected");
    }
    Ok(out)
}

fn sanitize(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
                c
            } else {
                '_'
            }
        })
        .collect()
}

fn write_csv(
    path: &Path,
    rows: &[Row],
    spread: &std::collections::HashMap<String, f64>,
) -> Result<()> {
    let mut w =
        csv::Writer::from_path(path).with_context(|| format!("writing {}", path.display()))?;
    w.write_record([
        "lake_id",
        "dem",
        "lon",
        "lat",
        "utm_epsg",
        "window_km",
        "lake_area_km2",
        "lake_perimeter_km",
        "lake_elev_m",
        "catchment_area_km2",
        "catchment_lake_ratio",
        "local_catchment_area_km2",
        "catchment_mean_elev_m",
        "dem_spread",
        "lake_source",
        "lake_compactness",
        "water_trimmed",
        "edge_reached",
        "status",
        "message",
    ])?;
    let f = |v: f64, d: usize| {
        if v.is_finite() {
            format!("{v:.d$}")
        } else {
            String::new()
        }
    };
    for r in rows {
        let ratio = r.catchment_area_km2 / r.lake_area_km2;
        w.write_record([
            r.lake_id.clone(),
            r.dem.clone(),
            f(r.lon, 6),
            f(r.lat, 6),
            r.utm_epsg.to_string(),
            f(r.window_km, 0),
            f(r.lake_area_km2, 4),
            f(r.lake_perimeter_km, 3),
            f(r.lake_elev_m, 2),
            f(r.catchment_area_km2, 4),
            f(ratio, 3),
            f(r.local_catchment_area_km2, 4),
            f(r.catchment_mean_elev_m, 2),
            spread.get(&r.lake_id).map(|v| f(*v, 3)).unwrap_or_default(),
            r.lake_source.clone(),
            f(r.lake_compactness, 3),
            r.water_trimmed.map(|v| f(v, 2)).unwrap_or_default(),
            r.edge_reached.to_string(),
            r.status.clone(),
            r.message.clone(),
        ])?;
    }
    w.flush()?;
    Ok(())
}

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

    #[test]
    fn water_rules_parse_and_classify() {
        let w = parse_water("stac:pc:jrc-gsw:occurrence>=50", 80.0).unwrap();
        assert_eq!(w.src.label, "jrc-gsw");
        assert!(w.rule.is_water(50.0) && !w.rule.is_water(49.9));
        let w = parse_water("stac:pc:jrc-gsw:occurrence>50", 80.0).unwrap();
        assert!(!w.rule.is_water(50.0) && w.rule.is_water(51.0));
        let w = parse_water("stac:pc:esa-worldcover:map=80", 0.0).unwrap();
        assert_eq!(w.src.label, "esa-worldcover");
        assert!(w.rule.is_water(80.0) && !w.rule.is_water(90.0));
        // No rule: water is == --water-value.
        let w = parse_water("stac:pc:esa-worldcover:map", 80.0).unwrap();
        assert!(w.rule.is_water(80.0) && !w.rule.is_water(70.0));
        assert!(!w.rule.is_water(f64::NAN));
        assert!(parse_water("stac:pc:jrc-gsw:occurrence>=x", 80.0).is_err());
    }
}