oxigdal-algorithms 0.1.4

High-performance SIMD-optimized raster and vector algorithms for OxiGDAL - Pure Rust geospatial processing
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
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
//! Stream network extraction and analysis
//!
//! Implements:
//!   - Stream extraction from flow accumulation (threshold-based)
//!   - Strahler stream order (Strahler, 1957)
//!   - Shreve stream magnitude (Shreve, 1966)
//!   - Stream head identification
//!   - Stream link identification (unique ID per reach)
//!   - Stream vectorisation (raster to polyline coordinates)

use crate::error::{AlgorithmError, Result};
use crate::raster::hydrology::flow_direction::{D8_DX, D8_DY, D8Direction};
use oxigdal_core::buffer::RasterBuffer;
use oxigdal_core::types::RasterDataType;
use std::collections::VecDeque;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

#[inline]
fn in_bounds(x: i64, y: i64, w: u64, h: u64) -> bool {
    x >= 0 && y >= 0 && (x as u64) < w && (y as u64) < h
}

/// Resolves a D8 code to the downstream cell coordinates.
fn downstream_cell(x: u64, y: u64, code: u8, w: u64, h: u64) -> Option<(u64, u64)> {
    let dir = D8Direction::from_code(code)?;
    let (dx, dy) = dir.offset();
    let nx = x as i64 + dx;
    let ny = y as i64 + dy;
    if in_bounds(nx, ny, w, h) {
        Some((nx as u64, ny as u64))
    } else {
        None
    }
}

/// Returns indices of all neighbours that flow **into** cell (x, y).
fn upstream_cells(
    x: u64,
    y: u64,
    flow_dir: &RasterBuffer,
    w: u64,
    h: u64,
) -> Result<Vec<(u64, u64)>> {
    let mut ups = Vec::new();
    for i in 0..8 {
        let nx = x as i64 + D8_DX[i];
        let ny = y as i64 + D8_DY[i];
        if !in_bounds(nx, ny, w, h) {
            continue;
        }
        let nxu = nx as u64;
        let nyu = ny as u64;
        let code = flow_dir.get_pixel(nxu, nyu).map_err(AlgorithmError::Core)? as u8;
        if let Some((dx, dy)) = downstream_cell(nxu, nyu, code, w, h) {
            if dx == x && dy == y {
                ups.push((nxu, nyu));
            }
        }
    }
    Ok(ups)
}

// ---------------------------------------------------------------------------
// Stream extraction
// ---------------------------------------------------------------------------

/// Extracts a binary stream network from flow accumulation using a threshold.
///
/// Returns a raster: 1.0 where `accumulation >= threshold`, 0.0 otherwise.
///
/// # Errors
///
/// Returns an error if pixel access fails.
pub fn extract_stream_network(
    flow_accumulation: &RasterBuffer,
    threshold: f64,
) -> Result<RasterBuffer> {
    let w = flow_accumulation.width();
    let h = flow_accumulation.height();
    let mut streams = RasterBuffer::zeros(w, h, RasterDataType::UInt8);

    for y in 0..h {
        for x in 0..w {
            let v = flow_accumulation
                .get_pixel(x, y)
                .map_err(AlgorithmError::Core)?;
            if v >= threshold {
                streams.set_pixel(x, y, 1.0).map_err(AlgorithmError::Core)?;
            }
        }
    }

    Ok(streams)
}

// ---------------------------------------------------------------------------
// Stream head identification
// ---------------------------------------------------------------------------

/// Identifies stream head cells.
///
/// A stream head is a stream cell that has no upstream stream neighbours
/// (i.e. no neighbour flows into it and is also a stream cell).
///
/// Returns a binary raster: 1.0 = stream head, 0.0 otherwise.
///
/// # Errors
///
/// Returns an error if pixel access fails.
pub fn identify_stream_heads(
    streams: &RasterBuffer,
    flow_dir: &RasterBuffer,
) -> Result<RasterBuffer> {
    let w = streams.width();
    let h = streams.height();
    let mut heads = RasterBuffer::zeros(w, h, RasterDataType::UInt8);

    for y in 0..h {
        for x in 0..w {
            let is_stream = streams.get_pixel(x, y).map_err(AlgorithmError::Core)?;
            if is_stream < 0.5 {
                continue;
            }

            let ups = upstream_cells(x, y, flow_dir, w, h)?;
            let has_upstream_stream = ups
                .iter()
                .any(|&(ux, uy)| streams.get_pixel(ux, uy).map(|v| v >= 0.5).unwrap_or(false));

            if !has_upstream_stream {
                heads.set_pixel(x, y, 1.0).map_err(AlgorithmError::Core)?;
            }
        }
    }

    Ok(heads)
}

// ---------------------------------------------------------------------------
// Strahler stream order
// ---------------------------------------------------------------------------

/// Computes Strahler stream order (Strahler, 1957).
///
/// Rules:
///   - Stream heads (no upstream tributaries) get order 1.
///   - When two streams of the *same* order join, the downstream order is
///     incremented by 1.
///   - When two streams of *different* orders join, the downstream gets
///     the higher order.
///
/// Requires a D8 flow direction grid and a binary stream mask.
///
/// # Errors
///
/// Returns an error if pixel access fails.
pub fn compute_stream_order(
    _dem: &RasterBuffer,
    flow_accumulation: &RasterBuffer,
    threshold: f64,
    _cell_size: f64,
) -> Result<RasterBuffer> {
    let w = flow_accumulation.width();
    let h = flow_accumulation.height();

    // We need a flow direction; re-derive it from the DEM passed through _dem
    // but actually we need it from _dem. Use the provided DEM.
    let flow_dir =
        crate::raster::hydrology::flow_direction::compute_d8_flow_direction(_dem, _cell_size)?;
    let streams = extract_stream_network(flow_accumulation, threshold)?;

    compute_strahler_order(&streams, &flow_dir)
}

/// Computes Strahler order from precomputed stream and flow direction rasters.
///
/// # Errors
///
/// Returns an error if pixel access fails.
pub fn compute_strahler_order(
    streams: &RasterBuffer,
    flow_dir: &RasterBuffer,
) -> Result<RasterBuffer> {
    let w = streams.width();
    let h = streams.height();
    let n = (w * h) as usize;

    let mut order = RasterBuffer::zeros(w, h, RasterDataType::Int32);
    let mut computed = vec![false; n];

    // Count upstream stream neighbours for each stream cell
    let mut upstream_count = vec![0u32; n];
    for y in 0..h {
        for x in 0..w {
            let is_stream = streams.get_pixel(x, y).map_err(AlgorithmError::Core)?;
            if is_stream < 0.5 {
                continue;
            }
            let ups = upstream_cells(x, y, flow_dir, w, h)?;
            let count = ups
                .iter()
                .filter(|&&(ux, uy)| streams.get_pixel(ux, uy).map(|v| v >= 0.5).unwrap_or(false))
                .count();
            upstream_count[(y * w + x) as usize] = count as u32;
        }
    }

    // BFS from stream heads (upstream_count == 0 and is_stream)
    let mut queue = VecDeque::new();
    for y in 0..h {
        for x in 0..w {
            let idx = (y * w + x) as usize;
            let is_stream = streams.get_pixel(x, y).map_err(AlgorithmError::Core)?;
            if is_stream >= 0.5 && upstream_count[idx] == 0 {
                order.set_pixel(x, y, 1.0).map_err(AlgorithmError::Core)?;
                computed[idx] = true;
                queue.push_back((x, y));
            }
        }
    }

    while let Some((x, y)) = queue.pop_front() {
        let code = flow_dir.get_pixel(x, y).map_err(AlgorithmError::Core)? as u8;
        if let Some((dx, dy)) = downstream_cell(x, y, code, w, h) {
            let didx = (dy * w + dx) as usize;
            let ds_stream = streams.get_pixel(dx, dy).map_err(AlgorithmError::Core)?;
            if ds_stream < 0.5 {
                continue;
            }

            upstream_count[didx] = upstream_count[didx].saturating_sub(1);

            if upstream_count[didx] == 0 && !computed[didx] {
                // All upstream tributaries computed -- determine Strahler order
                let ups = upstream_cells(dx, dy, flow_dir, w, h)?;
                let mut max_order = 0i32;
                let mut max_count = 0u32;

                for &(ux, uy) in &ups {
                    let us = streams.get_pixel(ux, uy).map_err(AlgorithmError::Core)?;
                    if us < 0.5 {
                        continue;
                    }
                    let o = order.get_pixel(ux, uy).map_err(AlgorithmError::Core)? as i32;
                    if o > max_order {
                        max_order = o;
                        max_count = 1;
                    } else if o == max_order {
                        max_count += 1;
                    }
                }

                let result_order = if max_count >= 2 {
                    max_order + 1
                } else {
                    max_order.max(1)
                };

                order
                    .set_pixel(dx, dy, f64::from(result_order))
                    .map_err(AlgorithmError::Core)?;
                computed[didx] = true;
                queue.push_back((dx, dy));
            }
        }
    }

    Ok(order)
}

// ---------------------------------------------------------------------------
// Shreve stream magnitude
// ---------------------------------------------------------------------------

/// Computes Shreve stream magnitude (Shreve, 1966).
///
/// Rules:
///   - Stream heads get magnitude 1.
///   - At confluences, magnitudes are summed.
///
/// # Errors
///
/// Returns an error if pixel access fails.
pub fn compute_shreve_magnitude(
    streams: &RasterBuffer,
    flow_dir: &RasterBuffer,
) -> Result<RasterBuffer> {
    let w = streams.width();
    let h = streams.height();
    let n = (w * h) as usize;

    let mut magnitude = RasterBuffer::zeros(w, h, RasterDataType::Float64);
    let mut computed = vec![false; n];

    // Count upstream stream neighbours
    let mut upstream_count = vec![0u32; n];
    for y in 0..h {
        for x in 0..w {
            let is_stream = streams.get_pixel(x, y).map_err(AlgorithmError::Core)?;
            if is_stream < 0.5 {
                continue;
            }
            let ups = upstream_cells(x, y, flow_dir, w, h)?;
            let count = ups
                .iter()
                .filter(|&&(ux, uy)| streams.get_pixel(ux, uy).map(|v| v >= 0.5).unwrap_or(false))
                .count();
            upstream_count[(y * w + x) as usize] = count as u32;
        }
    }

    // BFS from stream heads
    let mut queue = VecDeque::new();
    for y in 0..h {
        for x in 0..w {
            let idx = (y * w + x) as usize;
            let is_stream = streams.get_pixel(x, y).map_err(AlgorithmError::Core)?;
            if is_stream >= 0.5 && upstream_count[idx] == 0 {
                magnitude
                    .set_pixel(x, y, 1.0)
                    .map_err(AlgorithmError::Core)?;
                computed[idx] = true;
                queue.push_back((x, y));
            }
        }
    }

    while let Some((x, y)) = queue.pop_front() {
        let code = flow_dir.get_pixel(x, y).map_err(AlgorithmError::Core)? as u8;
        if let Some((dx, dy)) = downstream_cell(x, y, code, w, h) {
            let didx = (dy * w + dx) as usize;
            let ds_stream = streams.get_pixel(dx, dy).map_err(AlgorithmError::Core)?;
            if ds_stream < 0.5 {
                continue;
            }

            upstream_count[didx] = upstream_count[didx].saturating_sub(1);

            if upstream_count[didx] == 0 && !computed[didx] {
                // Sum magnitudes of all upstream stream neighbours
                let ups = upstream_cells(dx, dy, flow_dir, w, h)?;
                let mut total_mag = 0.0_f64;
                for &(ux, uy) in &ups {
                    let us = streams.get_pixel(ux, uy).map_err(AlgorithmError::Core)?;
                    if us < 0.5 {
                        continue;
                    }
                    total_mag += magnitude.get_pixel(ux, uy).map_err(AlgorithmError::Core)?;
                }

                magnitude
                    .set_pixel(dx, dy, total_mag.max(1.0))
                    .map_err(AlgorithmError::Core)?;
                computed[didx] = true;
                queue.push_back((dx, dy));
            }
        }
    }

    Ok(magnitude)
}

// ---------------------------------------------------------------------------
// Stream link identification
// ---------------------------------------------------------------------------

/// Assigns a unique integer ID to each stream link (reach between junctions
/// or between a stream head and the next junction/outlet).
///
/// Returns a raster where stream cells have their link ID (>= 1) and
/// non-stream cells are 0.
///
/// # Errors
///
/// Returns an error if pixel access fails.
pub fn compute_stream_links(
    streams: &RasterBuffer,
    flow_dir: &RasterBuffer,
) -> Result<RasterBuffer> {
    let w = streams.width();
    let h = streams.height();
    let mut links = RasterBuffer::zeros(w, h, RasterDataType::Int32);
    let mut link_id = 1i32;

    // Identify stream heads and junctions
    let heads = identify_stream_heads(streams, flow_dir)?;

    // Also identify junction cells (stream cells with >= 2 upstream stream neighbours)
    let mut is_junction = vec![false; (w * h) as usize];
    for y in 0..h {
        for x in 0..w {
            let is_stream = streams.get_pixel(x, y).map_err(AlgorithmError::Core)?;
            if is_stream < 0.5 {
                continue;
            }
            let ups = upstream_cells(x, y, flow_dir, w, h)?;
            let stream_ups = ups
                .iter()
                .filter(|&&(ux, uy)| streams.get_pixel(ux, uy).map(|v| v >= 0.5).unwrap_or(false))
                .count();
            if stream_ups >= 2 {
                is_junction[(y * w + x) as usize] = true;
            }
        }
    }

    // Trace downstream from each stream head, assigning link IDs
    for y in 0..h {
        for x in 0..w {
            let is_head = heads.get_pixel(x, y).map_err(AlgorithmError::Core)?;
            if is_head < 0.5 {
                continue;
            }

            let mut cx = x;
            let mut cy = y;
            let current_link = link_id;
            link_id += 1;

            loop {
                let idx = (cy * w + cx) as usize;
                let already = links.get_pixel(cx, cy).map_err(AlgorithmError::Core)? as i32;
                if already > 0 {
                    break; // Already assigned
                }

                links
                    .set_pixel(cx, cy, f64::from(current_link))
                    .map_err(AlgorithmError::Core)?;

                let code = flow_dir.get_pixel(cx, cy).map_err(AlgorithmError::Core)? as u8;
                match downstream_cell(cx, cy, code, w, h) {
                    Some((dx, dy)) => {
                        let ds_stream = streams.get_pixel(dx, dy).map_err(AlgorithmError::Core)?;
                        if ds_stream < 0.5 {
                            break; // Left the stream
                        }
                        // Check if we hit a junction: start a new link ID there
                        let didx = (dy * w + dx) as usize;
                        if is_junction[didx] {
                            // The junction itself will be labeled when traced from
                            // another head or will get its own link
                            break;
                        }
                        cx = dx;
                        cy = dy;
                    }
                    None => break, // Edge of grid
                }
            }
        }
    }

    // Label junction cells that might not have been visited yet
    for y in 0..h {
        for x in 0..w {
            let idx = (y * w + x) as usize;
            if !is_junction[idx] {
                continue;
            }
            let already = links.get_pixel(x, y).map_err(AlgorithmError::Core)? as i32;
            if already > 0 {
                continue;
            }
            // Assign a new link ID and trace downstream
            let current_link = link_id;
            link_id += 1;

            let mut cx = x;
            let mut cy = y;
            loop {
                let a = links.get_pixel(cx, cy).map_err(AlgorithmError::Core)? as i32;
                if a > 0 {
                    break;
                }
                links
                    .set_pixel(cx, cy, f64::from(current_link))
                    .map_err(AlgorithmError::Core)?;

                let code = flow_dir.get_pixel(cx, cy).map_err(AlgorithmError::Core)? as u8;
                match downstream_cell(cx, cy, code, w, h) {
                    Some((dx, dy)) => {
                        let ds = streams.get_pixel(dx, dy).map_err(AlgorithmError::Core)?;
                        if ds < 0.5 {
                            break;
                        }
                        let didx = (dy * w + dx) as usize;
                        if is_junction[didx] && (dx != x || dy != y) {
                            break;
                        }
                        cx = dx;
                        cy = dy;
                    }
                    None => break,
                }
            }
        }
    }

    Ok(links)
}

// ---------------------------------------------------------------------------
// Stream vectorisation
// ---------------------------------------------------------------------------

/// A polyline segment representing a stream reach.
#[derive(Debug, Clone)]
pub struct StreamSegment {
    /// Unique segment ID (corresponds to stream link ID)
    pub id: i32,
    /// Strahler order of this segment (if computed)
    pub strahler_order: Option<i32>,
    /// Shreve magnitude (if computed)
    pub shreve_magnitude: Option<f64>,
    /// Coordinates of the segment: (col, row) pairs from upstream to downstream
    pub coordinates: Vec<(u64, u64)>,
}

/// Vectorises the stream network into polyline segments.
///
/// Each segment corresponds to a stream link (reach between junctions).
///
/// # Arguments
///
/// * `streams` - Binary stream raster
/// * `flow_dir` - D8 flow direction raster
/// * `strahler` - Optional Strahler order raster
/// * `shreve` - Optional Shreve magnitude raster
///
/// # Errors
///
/// Returns an error if pixel access fails.
pub fn vectorize_streams(
    streams: &RasterBuffer,
    flow_dir: &RasterBuffer,
    strahler: Option<&RasterBuffer>,
    shreve: Option<&RasterBuffer>,
) -> Result<Vec<StreamSegment>> {
    let w = streams.width();
    let h = streams.height();
    let mut segments = Vec::new();

    let heads = identify_stream_heads(streams, flow_dir)?;
    let links = compute_stream_links(streams, flow_dir)?;

    // Find unique link IDs and collect their cells
    let mut link_cells: std::collections::HashMap<i32, Vec<(u64, u64)>> =
        std::collections::HashMap::new();

    for y in 0..h {
        for x in 0..w {
            let lid = links.get_pixel(x, y).map_err(AlgorithmError::Core)? as i32;
            if lid > 0 {
                link_cells.entry(lid).or_default().push((x, y));
            }
        }
    }

    // For each link, trace from the head to build ordered coordinates
    for (lid, cells) in &link_cells {
        // Find the head cell of this link (cell with no upstream stream in the same link)
        let mut head_cell = None;
        for &(cx, cy) in cells {
            let is_head = heads.get_pixel(cx, cy).map_err(AlgorithmError::Core)?;
            if is_head >= 0.5 {
                head_cell = Some((cx, cy));
                break;
            }
            // Also check if this cell has no upstream stream cell with the same link ID
            let ups = upstream_cells(cx, cy, flow_dir, w, h)?;
            let has_same_link_upstream = ups.iter().any(|&(ux, uy)| {
                links
                    .get_pixel(ux, uy)
                    .map(|v| v as i32 == *lid)
                    .unwrap_or(false)
            });
            if !has_same_link_upstream {
                head_cell = Some((cx, cy));
                break;
            }
        }

        let start = match head_cell {
            Some(c) => c,
            None => {
                if let Some(&c) = cells.first() {
                    c
                } else {
                    continue;
                }
            }
        };

        // Trace downstream
        let mut coords = Vec::new();
        let mut cx = start.0;
        let mut cy = start.1;
        let mut visited_count = 0usize;

        loop {
            let current_lid = links.get_pixel(cx, cy).map_err(AlgorithmError::Core)? as i32;
            if current_lid != *lid && !coords.is_empty() {
                break;
            }
            coords.push((cx, cy));
            visited_count += 1;
            if visited_count > cells.len() + 1 {
                break; // Safety: avoid infinite loop
            }

            let code = flow_dir.get_pixel(cx, cy).map_err(AlgorithmError::Core)? as u8;
            match downstream_cell(cx, cy, code, w, h) {
                Some((dx, dy)) => {
                    let dl = links.get_pixel(dx, dy).map_err(AlgorithmError::Core)? as i32;
                    if dl != *lid {
                        break;
                    }
                    cx = dx;
                    cy = dy;
                }
                None => break,
            }
        }

        // Get Strahler order and Shreve magnitude from the first cell
        let so = strahler
            .and_then(|s| s.get_pixel(start.0, start.1).ok())
            .map(|v| v as i32);
        let sm = shreve.and_then(|s| s.get_pixel(start.0, start.1).ok());

        segments.push(StreamSegment {
            id: *lid,
            strahler_order: so,
            shreve_magnitude: sm,
            coordinates: coords,
        });
    }

    // Sort by segment ID
    segments.sort_by_key(|s| s.id);
    Ok(segments)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::raster::hydrology::flow_direction::compute_d8_flow_direction;

    fn make_v_shaped_dem(size: u64) -> RasterBuffer {
        // V-shaped valley: elevation = |x - center| + y_distance_from_outlet
        let mut dem = RasterBuffer::zeros(size, size, RasterDataType::Float32);
        let center = size / 2;
        for y in 0..size {
            for x in 0..size {
                let dx = (x as f64 - center as f64).abs();
                let dy = (size - 1 - y) as f64;
                let _ = dem.set_pixel(x, y, dx + dy);
            }
        }
        dem
    }

    fn make_simple_accum(size: u64) -> RasterBuffer {
        // Synthetic accumulation: high in center column, increasing downstream
        let mut accum = RasterBuffer::zeros(size, size, RasterDataType::Float32);
        let center = size / 2;
        for y in 0..size {
            let _ = accum.set_pixel(center, y, (y + 1) as f64 * 10.0);
        }
        accum
    }

    #[test]
    fn test_extract_stream_network() {
        let accum = make_simple_accum(7);
        let streams = extract_stream_network(&accum, 20.0);
        assert!(streams.is_ok());
        let streams = streams.expect("should succeed");

        // Center column with accum >= 20 should be stream
        let v = streams.get_pixel(3, 2).expect("should succeed");
        assert!(v >= 0.5, "Cell with high accumulation should be stream");

        let v2 = streams.get_pixel(0, 0).expect("should succeed");
        assert!(v2 < 0.5, "Off-stream cell should be 0");
    }

    #[test]
    fn test_identify_stream_heads() {
        let dem = make_v_shaped_dem(9);
        let flow_dir = compute_d8_flow_direction(&dem, 1.0).expect("should succeed");
        let accum =
            crate::raster::hydrology::flow_accumulation::compute_flow_accumulation(&dem, 1.0)
                .expect("should succeed");
        let streams = extract_stream_network(&accum, 5.0).expect("should succeed");

        let heads = identify_stream_heads(&streams, &flow_dir);
        assert!(heads.is_ok());
        let heads = heads.expect("should succeed");

        // At least one stream head should exist
        let mut head_count = 0;
        for y in 0..9u64 {
            for x in 0..9u64 {
                let v = heads.get_pixel(x, y).expect("should succeed");
                if v >= 0.5 {
                    head_count += 1;
                }
            }
        }
        assert!(head_count > 0, "Should find at least one stream head");
    }

    #[test]
    fn test_strahler_order_basic() {
        let dem = make_v_shaped_dem(11);
        let flow_dir = compute_d8_flow_direction(&dem, 1.0).expect("should succeed");
        let accum =
            crate::raster::hydrology::flow_accumulation::compute_flow_accumulation(&dem, 1.0)
                .expect("should succeed");
        let streams = extract_stream_network(&accum, 3.0).expect("should succeed");

        let order = compute_strahler_order(&streams, &flow_dir);
        assert!(order.is_ok());
        let order = order.expect("should succeed");

        // At least some cells should have order >= 1
        let mut has_order = false;
        for y in 0..11u64 {
            for x in 0..11u64 {
                let o = order.get_pixel(x, y).expect("should succeed");
                if o >= 1.0 {
                    has_order = true;
                    break;
                }
            }
            if has_order {
                break;
            }
        }
        assert!(has_order, "Some stream cells should have order >= 1");
    }

    #[test]
    fn test_shreve_magnitude_basic() {
        let dem = make_v_shaped_dem(11);
        let flow_dir = compute_d8_flow_direction(&dem, 1.0).expect("should succeed");
        let accum =
            crate::raster::hydrology::flow_accumulation::compute_flow_accumulation(&dem, 1.0)
                .expect("should succeed");
        let streams = extract_stream_network(&accum, 3.0).expect("should succeed");

        let mag = compute_shreve_magnitude(&streams, &flow_dir);
        assert!(mag.is_ok());
        let mag = mag.expect("should succeed");

        // Downstream should have higher magnitude than upstream
        let mut has_mag = false;
        for y in 0..11u64 {
            for x in 0..11u64 {
                let m = mag.get_pixel(x, y).expect("should succeed");
                if m >= 1.0 {
                    has_mag = true;
                    break;
                }
            }
            if has_mag {
                break;
            }
        }
        assert!(has_mag, "Some stream cells should have magnitude >= 1");
    }

    #[test]
    fn test_stream_links() {
        let dem = make_v_shaped_dem(11);
        let flow_dir = compute_d8_flow_direction(&dem, 1.0).expect("should succeed");
        let accum =
            crate::raster::hydrology::flow_accumulation::compute_flow_accumulation(&dem, 1.0)
                .expect("should succeed");
        let streams = extract_stream_network(&accum, 3.0).expect("should succeed");

        let links = compute_stream_links(&streams, &flow_dir);
        assert!(links.is_ok());
        let links = links.expect("should succeed");

        // At least one link should exist
        let mut has_link = false;
        for y in 0..11u64 {
            for x in 0..11u64 {
                let v = links.get_pixel(x, y).expect("should succeed") as i32;
                if v > 0 {
                    has_link = true;
                    break;
                }
            }
            if has_link {
                break;
            }
        }
        assert!(has_link, "Should find at least one stream link");
    }

    #[test]
    fn test_vectorize_streams() {
        let dem = make_v_shaped_dem(11);
        let flow_dir = compute_d8_flow_direction(&dem, 1.0).expect("should succeed");
        let accum =
            crate::raster::hydrology::flow_accumulation::compute_flow_accumulation(&dem, 1.0)
                .expect("should succeed");
        let streams = extract_stream_network(&accum, 3.0).expect("should succeed");

        let segments = vectorize_streams(&streams, &flow_dir, None, None);
        assert!(segments.is_ok());
        let segments = segments.expect("should succeed");

        assert!(
            !segments.is_empty(),
            "Should produce at least one stream segment"
        );
        for seg in &segments {
            assert!(
                !seg.coordinates.is_empty(),
                "Segment {} should have coordinates",
                seg.id
            );
        }
    }

    #[test]
    fn test_compute_stream_order_legacy_api() {
        // Test the legacy API that takes DEM + accum
        let dem = make_v_shaped_dem(11);
        let accum =
            crate::raster::hydrology::flow_accumulation::compute_flow_accumulation(&dem, 1.0)
                .expect("should succeed");

        let order = compute_stream_order(&dem, &accum, 3.0, 1.0);
        assert!(order.is_ok());
    }
}