oxigeo 0.2.4

Pure Rust geospatial data abstraction library — the Rust alternative to GDAL
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
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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
//! Raster pixel-reading surface of [`Dataset`]: full-band reads, windowed
//! reads, read-into-caller-buffer reads, band iteration, and the clip-window
//! plumbing they share.
//!
//! # Which reader should I use?
//!
//! Every reader below opens the file, decodes only the blocks it needs, and
//! honours any [`PixelWindow`] recorded by [`Dataset::clip`](crate::Dataset::clip).
//! They differ in how many bands they return, who owns the destination memory,
//! and whether the element type is converted on the way out.
//!
//! | Method | Bands | Allocates | Reads | Converts |
//! |---|---|---|---|---|
//! | [`Dataset::read_band`] | one | one `RasterBuffer` (whole band) | every block of the band (or of the clip window) | no — file's element type |
//! | [`Dataset::read_window`] | one | one `RasterBuffer` (window) | only the blocks the window overlaps | no — file's element type |
//! | [`Dataset::read_band_into`] | one | **nothing** — you own `dst` | every block of the band (or of the clip window) | yes — to `T`, fused into the decode |
//! | [`Dataset::read_window_into`] | one | **nothing** — you own `dst` | only the blocks the window overlaps | yes — to `T`, fused into the decode |
//! | [`Dataset::read_interleaved`] | many, interleaved | one `Vec<T>` + fixed scratch | every block once, whatever the band count | yes — to `T`, fused into the decode |
//! | [`Dataset::read_interleaved_into`] | many, interleaved | fixed scratch — you own `dst` | every block once, whatever the band count | yes — to `T`, fused into the decode |
//! | [`Dataset::read_window_interleaved`] | many, interleaved | one `Vec<T>` + fixed scratch | only the blocks the window overlaps, once each | yes — to `T`, fused into the decode |
//! | [`Dataset::read_window_interleaved_into`] | many, interleaved | fixed scratch — you own `dst` | only the blocks the window overlaps, once each | yes — to `T`, fused into the decode |
//!
//! "Allocates nothing" is literal: the single-band `*_into` readers reuse one
//! block-sized scratch buffer inside the driver and scatter decoded samples
//! straight into `dst`, so the peak extra memory is one tile/strip regardless of
//! raster size.  That is the direct equivalent of GDAL's
//! `RasterBand::read_into_slice`.  The interleaved readers keep the same
//! guarantee: their scratch is a fixed handful of block-sized buffers inside the
//! driver plus a list of the band indices, so peak memory is set by the file's
//! block geometry and the band count, never by the raster's dimensions.
//!
//! "Every block once" is also literal, and is the reason the interleaved readers
//! exist as a single call rather than a loop over [`Dataset::read_band_into`].
//! In a chunky GeoTIFF (`PlanarConfiguration = 1` — nearly every RGB, RGBA or
//! multispectral file) one block physically holds every band of the pixels it
//! covers, so `n` single-band reads decompress each block `n` times and throw
//! `n − 1` bands away each pass.  Decompression dominates the read; asking for
//! the bands together pays it once.
//!
//! # One band or many?
//!
//! [`Dataset::read_band`] returns **one** band.  Before 0.2.2 it returned the
//! whole pixel-interleaved image whatever band index it was given; if you are
//! porting code that relied on that, [`Dataset::read_interleaved`] is the
//! replacement and says what it does.
//!
//! # Clip semantics
//!
//! Every reader here works in the dataset's **current** pixel grid.  On a
//! dataset returned by [`Dataset::clip`] that grid is the clipped region, so
//! "the whole band" means the clip window and window coordinates are relative to
//! the clip's upper-left corner.  The dimensions are exactly
//! [`Dataset::width`] × [`Dataset::height`], which is what
//! [`Dataset::read_band_into`] requires `dst.len()` to match.  Clipping is
//! applied by *reading only the clipped blocks*, not by reading the file and
//! discarding pixels.

use crate::{Dataset, OxiGeoError, Result};
use oxigeo_core::buffer::{RasterBuffer, RasterElement};

// Needed by both the GeoTIFF and the VRT read paths, which are independently
// selectable features.
#[cfg(any(feature = "geotiff", feature = "vrt"))]
use crate::DatasetFormat;
#[cfg(any(feature = "geotiff", feature = "vrt"))]
use oxigeo_core::types::RasterDataType;

#[cfg(feature = "geotiff")]
use oxigeo_core::io::FileDataSource;
#[cfg(feature = "geotiff")]
use oxigeo_core::types::NoDataValue;
#[cfg(feature = "geotiff")]
use oxigeo_geotiff::GeoTiffReader;

/// The GeoTIFF reader type every raster read of a local file goes through.
#[cfg(feature = "geotiff")]
type LocalTiffReader = GeoTiffReader<FileDataSource>;

/// A pixel-space sub-rectangle of a raster dataset.
///
/// Coordinates are in the on-disk file's pixel grid: `(col, row)` is the
/// upper-left corner (0-based) and `(width, height)` the size of the window.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct PixelWindow {
    /// Left column (0-based) of the window in the source file.
    pub col: u32,
    /// Top row (0-based) of the window in the source file.
    pub row: u32,
    /// Window width in pixels.
    pub width: u32,
    /// Window height in pixels.
    pub height: u32,
}

/// A read request already resolved into the **source file's** pixel grid.
///
/// Produced by [`Dataset::resolve_source_window`], which folds the dataset's
/// clip window and the caller's window request into a single rectangle so the
/// driver can be asked for exactly those pixels once.
#[cfg(feature = "geotiff")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct SourceWindow {
    /// Left column in the source file.
    x: u64,
    /// Top row in the source file.
    y: u64,
    /// Window width in pixels.
    width: u64,
    /// Window height in pixels.
    height: u64,
    /// `true` when the window covers the entire band of the source file, which
    /// lets the driver take its cheaper full-band path.
    full_band: bool,
}

#[cfg(feature = "geotiff")]
impl SourceWindow {
    /// Number of pixels the window contains.
    fn pixel_count(&self) -> Result<usize> {
        usize::try_from(self.width)
            .ok()
            .and_then(|w| {
                usize::try_from(self.height)
                    .ok()
                    .and_then(|h| w.checked_mul(h))
            })
            .ok_or_else(|| OxiGeoError::Internal {
                message: format!(
                    "window {}×{} overflows the address space",
                    self.width, self.height
                ),
            })
    }
}

/// Crop an interleaved raster byte buffer to a pixel window.
///
/// `data` is `full_width × full_height` pixels, each pixel occupying a fixed
/// stride of bytes (the stride covers every band / sample of that pixel, so
/// this works for both single-band and pixel-interleaved multi-band buffers).
/// The window `(col, row, width, height)` must fit entirely within the source
/// dimensions.
///
/// Returns `None` when the buffer length is not an exact multiple of the pixel
/// count or the window falls outside the source extent — callers treat that as
/// "cannot honour the clip" and surface an error rather than returning wrong
/// pixels.
///
/// The single-band readers no longer need this: they ask the driver for the
/// clipped rectangle directly.  It survives for
/// [`Dataset::convert`](crate::Dataset::convert), which builds a chunky
/// multi-band plane in memory before writing and therefore still crops a
/// materialised buffer.
#[cfg(feature = "geotiff")]
pub(crate) fn crop_interleaved(
    data: &[u8],
    full_width: u32,
    full_height: u32,
    window: PixelWindow,
) -> Option<Vec<u8>> {
    let full_w = full_width as usize;
    let full_h = full_height as usize;
    if full_w == 0 || full_h == 0 {
        return None;
    }
    let total_px = full_w.checked_mul(full_h)?;
    if total_px == 0 || !data.len().is_multiple_of(total_px) {
        return None;
    }
    let stride = data.len() / total_px;
    let col = window.col as usize;
    let row = window.row as usize;
    let w = window.width as usize;
    let h = window.height as usize;
    if col.checked_add(w)? > full_w || row.checked_add(h)? > full_h {
        return None;
    }
    let mut out = Vec::with_capacity(w.saturating_mul(h).saturating_mul(stride));
    for r in 0..h {
        let src_row_start = (row + r) * full_w;
        for c in 0..w {
            let px = (src_row_start + col + c) * stride;
            out.extend_from_slice(&data[px..px + stride]);
        }
    }
    Some(out)
}

impl Dataset {
    /// Read a single raster band by 0-based index and return its pixel data as
    /// a `RasterBuffer`.
    ///
    /// Requires the `geotiff` feature for GeoTIFF datasets.  Other formats
    /// return [`OxiGeoError::NotSupported`].
    ///
    /// `band` is **0-based**: band 0 is the first raster band.
    ///
    /// # Behaviour change in 0.2.2
    ///
    /// This returns **that one band's samples** — `width × height` of them.
    ///
    /// Up to and including 0.2.1 it ignored `band` for multi-band files and
    /// returned the *whole pixel-interleaved image* (`width × height × bands`
    /// samples, `b0 b1 b2 b0 b1 b2 …`), which silently mis-fed every caller that
    /// asked for one band and quietly worked for callers who wanted the lot.  The
    /// single-band case is unchanged; multi-band callers who wanted the
    /// interleaved image should move to [`Self::read_interleaved`], which returns
    /// exactly what the old `read_band` did (and lets you choose and order the
    /// bands).  `read_band(0)` on a 3-band file used to yield three times as many
    /// samples as it does now, so a length check is the quickest way to find
    /// affected code.
    ///
    /// # Cost
    ///
    /// One allocation — the returned buffer — plus one block-sized scratch
    /// inside the driver.  On a clipped dataset only the blocks overlapping the
    /// clip window are read.  If you already own the destination memory, or want
    /// the samples converted to another element type, prefer
    /// [`Self::read_band_into`], which allocates nothing at all.
    ///
    /// # Errors
    ///
    /// - [`OxiGeoError::NotSupported`] — format is not supported.
    /// - [`OxiGeoError::InvalidParameter`] — `band` index is out of range.
    /// - [`OxiGeoError::Io`] / [`OxiGeoError::Format`] — underlying read failure.
    pub fn read_band(&self, band: u32) -> Result<RasterBuffer> {
        self.validate_band(band)?;

        #[cfg(feature = "geotiff")]
        if matches!(self.info.format, DatasetFormat::GeoTiff) {
            let reader = self.open_geotiff_reader()?;
            let window = self.resolve_source_window(&reader, None)?;
            return self.read_buffer_with(&reader, band, window);
        }

        #[cfg(feature = "vrt")]
        if matches!(self.info.format, DatasetFormat::Vrt) {
            let reader = self.open_vrt_reader()?;
            let window = self.resolve_vrt_window(&reader, None)?;
            return self.read_vrt_buffer(&reader, band, window);
        }

        Err(self.unsupported("read_band()"))
    }

    /// Read a whole band directly into a caller-supplied buffer, converting to
    /// `T` in one pass.
    ///
    /// This is the fast path this crate recommends, and the direct equivalent of
    /// GDAL's `RasterBand::read_into_slice`: the samples are converted from the
    /// file's element type to `T` *while* the blocks are decoded, so a
    /// `Float32` DEM lands in a `&mut [f64]` with **no** full-size intermediate
    /// buffer, no second pass, and no allocation beyond the `dst` you already
    /// own.
    ///
    /// `dst.len()` must be exactly `self.width() as usize * self.height() as
    /// usize` — one element per pixel of the dataset's **current** extent.  On a
    /// dataset returned by [`Dataset::clip`] that is the clipped extent, and
    /// only the blocks overlapping the clip window are read (see the
    /// module docs above, under `Clip semantics`).
    ///
    /// Conversion is saturating, with floats rounded to nearest (halves away
    /// from zero), matching GDAL's `RasterIO`.
    ///
    /// # Example
    ///
    /// The whole answer to "how do I read a DEM as fast as possible", end to
    /// end:
    ///
    /// ```rust,no_run
    /// use oxigeo::Dataset;
    ///
    /// # fn main() -> oxigeo::Result<()> {
    /// let ds = Dataset::open("dem.tif")?;
    ///
    /// // The element type is known from the header, before any pixel is read,
    /// // so the destination can be sized (and typed) up front.
    /// println!("on-disk type: {:?}", ds.data_type());
    ///
    /// let (width, height) = (ds.width() as usize, ds.height() as usize);
    /// let mut dem = vec![0.0f64; width * height];   // the only large allocation
    /// ds.read_band_into(0, &mut dem)?;              // decode + Float32→f64 in one pass
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Bridging to `ndarray`
    ///
    /// `oxigeo` itself has no array dependency, but the buffer above is already
    /// in row-major order, so an `Array2<f64>` is a move away — or, if you want
    /// to decode straight into an array you allocated, hand `read_band_into` the
    /// array's own backing slice:
    ///
    /// ```rust,ignore
    /// use ndarray::Array2;
    ///
    /// let mut grid = Array2::<f64>::zeros((height, width));
    /// let dst = grid.as_slice_mut().expect("standard layout");
    /// ds.read_band_into(0, dst)?;
    /// ```
    ///
    /// # Errors
    ///
    /// - [`OxiGeoError::NotSupported`] — format is not a supported raster type.
    /// - [`OxiGeoError::InvalidParameter`] — `band` is out of range, or
    ///   `dst.len()` is not exactly the band's pixel count (the error names the
    ///   expected length; a wrong length is never silently truncated).
    /// - [`OxiGeoError::Io`] / [`OxiGeoError::Format`] — underlying read failure.
    pub fn read_band_into<T: RasterElement>(&self, band: u32, dst: &mut [T]) -> Result<()> {
        self.validate_band(band)?;

        #[cfg(feature = "geotiff")]
        if matches!(self.info.format, DatasetFormat::GeoTiff) {
            let reader = self.open_geotiff_reader()?;
            let window = self.resolve_source_window(&reader, None)?;
            return self.read_typed_with(&reader, band, window, dst);
        }

        #[cfg(feature = "vrt")]
        if matches!(self.info.format, DatasetFormat::Vrt) {
            let reader = self.open_vrt_reader()?;
            let window = self.resolve_vrt_window(&reader, None)?;
            return self.read_vrt_interleaved(&reader, &[band], window, dst);
        }

        let _ = dst;
        Err(self.unsupported("read_band_into()"))
    }

    /// Return a lazy iterator over all raster bands.
    ///
    /// Each call to `Iterator::next()` reads the next band from the underlying
    /// file.  For multi-band GeoTIFF datasets this avoids loading all bands
    /// into memory simultaneously.
    ///
    /// The file is opened — and its IFD chain parsed — **once** for the whole
    /// iteration, not once per band: the reader is created on the first
    /// `next()` and reused for every subsequent band.
    ///
    /// The iterator yields `Result<RasterBuffer>` so that per-band read errors
    /// are propagated without aborting the iteration.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use oxigeo::Dataset;
    ///
    /// # fn main() -> oxigeo::Result<()> {
    /// let ds = Dataset::open("elevation.tif")?;
    /// for band_result in ds.bands() {
    ///     let buf = band_result?;
    ///     println!("band pixels: {}", buf.pixel_count());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn bands(&self) -> BandIter<'_> {
        BandIter {
            dataset: self,
            next_band: 0,
            band_count: self.info.band_count,
            #[cfg(feature = "geotiff")]
            reader: None,
            #[cfg(feature = "geotiff")]
            reader_unavailable: false,
        }
    }

    /// Read a rectangular pixel window from a single raster band.
    ///
    /// Unlike [`Self::read_band`] (which reads the full band), this reads only
    /// the `width × height` sub-rectangle whose upper-left corner is
    /// `(col, row)` in this dataset's pixel grid.  It is the real
    /// pixel-populating primitive behind windowed / tiled access: callers can
    /// walk a [`crate::streaming::TileStream`]'s coordinates and call
    /// `read_window` per tile to obtain actual pixels.
    ///
    /// Coordinates are relative to the dataset's *current* extent, so on a
    /// dataset produced by [`Dataset::clip`] the window is taken within the
    /// clipped region.
    ///
    /// `band` is **0-based**.
    ///
    /// # Cost
    ///
    /// One allocation (the returned buffer).  Only the tiles or strips the
    /// window overlaps are fetched and decoded — a tile-aligned window on a
    /// tiled file touches exactly one tile's worth of bytes, not the band.
    ///
    /// # Errors
    ///
    /// - [`OxiGeoError::NotSupported`] — format is not a supported raster type.
    /// - [`OxiGeoError::InvalidParameter`] — `band` is out of range, the window
    ///   has zero size, or it extends past the dataset extent.
    /// - [`OxiGeoError::Io`] / [`OxiGeoError::Format`] — underlying read failure.
    pub fn read_window(
        &self,
        band: u32,
        col: u32,
        row: u32,
        width: u32,
        height: u32,
    ) -> Result<RasterBuffer> {
        Self::validate_window_size(width, height)?;
        self.validate_band(band)?;

        #[cfg(feature = "geotiff")]
        if matches!(self.info.format, DatasetFormat::GeoTiff) {
            let reader = self.open_geotiff_reader()?;
            let window = self.resolve_source_window(&reader, Some((col, row, width, height)))?;
            return self.read_buffer_with(&reader, band, window);
        }

        #[cfg(feature = "vrt")]
        if matches!(self.info.format, DatasetFormat::Vrt) {
            let reader = self.open_vrt_reader()?;
            let window = self.resolve_vrt_window(&reader, Some((col, row, width, height)))?;
            return self.read_vrt_buffer(&reader, band, window);
        }

        let _ = (col, row);
        Err(self.unsupported("read_window()"))
    }

    /// Read a pixel window directly into a caller-supplied buffer, converting to
    /// `T` in one pass.
    ///
    /// The windowed counterpart of [`Self::read_band_into`]: `(col, row)` is the
    /// window's upper-left corner in this dataset's **current** pixel grid
    /// (the clipped grid on a dataset returned by [`Dataset::clip`]), and
    /// `dst.len()` must be exactly `width as usize * height as usize`.
    ///
    /// # Cost
    ///
    /// No allocation whatsoever, and only the tiles or strips the window
    /// overlaps are fetched and decoded.  This is the primitive to use when
    /// walking a large raster tile by tile with a reusable destination buffer.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use oxigeo::Dataset;
    ///
    /// # fn main() -> oxigeo::Result<()> {
    /// let ds = Dataset::open("dem.tif")?;
    /// let mut tile = vec![0.0f32; 256 * 256];
    /// // Reuse `tile` for every window — no per-tile allocation.
    /// ds.read_window_into(0, 0, 0, 256, 256, &mut tile)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// - [`OxiGeoError::NotSupported`] — format is not a supported raster type.
    /// - [`OxiGeoError::InvalidParameter`] — `band` is out of range, the window
    ///   has zero size or extends past the dataset extent, or `dst.len()` is not
    ///   exactly `width × height` (the error names the expected length).
    /// - [`OxiGeoError::Io`] / [`OxiGeoError::Format`] — underlying read failure.
    pub fn read_window_into<T: RasterElement>(
        &self,
        band: u32,
        col: u32,
        row: u32,
        width: u32,
        height: u32,
        dst: &mut [T],
    ) -> Result<()> {
        Self::validate_window_size(width, height)?;
        self.validate_band(band)?;

        #[cfg(feature = "geotiff")]
        if matches!(self.info.format, DatasetFormat::GeoTiff) {
            let reader = self.open_geotiff_reader()?;
            let window = self.resolve_source_window(&reader, Some((col, row, width, height)))?;
            return self.read_typed_with(&reader, band, window, dst);
        }

        #[cfg(feature = "vrt")]
        if matches!(self.info.format, DatasetFormat::Vrt) {
            let reader = self.open_vrt_reader()?;
            let window = self.resolve_vrt_window(&reader, Some((col, row, width, height)))?;
            return self.read_vrt_interleaved(&reader, &[band], window, dst);
        }

        let _ = (col, row, dst);
        Err(self.unsupported("read_window_into()"))
    }

    // -- interleaved (multi-band) readers ------------------------------------

    /// Read several bands at once, pixel-interleaved, into a fresh `Vec<T>`.
    ///
    /// This is the multi-band counterpart of [`Self::read_band`] and the
    /// supported replacement for the pre-0.2.2 behaviour of `read_band`, which
    /// returned the whole interleaved image regardless of the band index it was
    /// given (see [`Self::read_band`] for the full note on that change).
    ///
    /// # Band selection
    ///
    /// `bands` names the bands to read, in output order:
    ///
    /// - `None` — every band of the dataset, in file order.  This is the common
    ///   case and costs no allocation to express.
    /// - `Some(&[..])` — exactly those 0-based band indices, in that order.  The
    ///   list may reorder bands (`&[2, 1, 0]` reads an RGB file as BGR), read a
    ///   subset (`&[0, 1, 2]` of a 5-band scene reads three bands and decodes
    ///   only those three), and may repeat an index (`&[0, 0, 0]` expands a
    ///   grey band to three channels).  This mirrors GDAL's `panBandMap`, where
    ///   `nullptr` likewise means "all bands in order".
    ///
    /// # Layout
    ///
    /// The result is `width × height × bands` elements: for every pixel, its
    /// selected bands consecutively (`b0 b1 b2 b0 b1 b2 …`), pixels in row-major
    /// order.  The sample of pixel `(col, row)` in slot `i` is at
    /// `(row * width + col) * bands + i`.
    ///
    /// Unlike [`Self::read_band`] this does **not** return a `RasterBuffer`: that
    /// type describes exactly one sample per pixel and rejects a buffer holding
    /// several.  A typed `Vec<T>` also lets the element conversion stay fused
    /// into the decode, as in [`Self::read_band_into`].
    ///
    /// # Example
    ///
    /// ```rust
    /// use oxigeo::Dataset;
    ///
    /// # fn main() -> oxigeo::Result<()> {
    /// # let path = std::env::temp_dir()
    /// #     .join(format!("oxigeo_doc_read_interleaved_{}.tif", std::process::id()));
    /// # struct Cleanup(std::path::PathBuf);
    /// # impl Drop for Cleanup {
    /// #     fn drop(&mut self) {
    /// #         let _ = std::fs::remove_file(&self.0);
    /// #     }
    /// # }
    /// # let _cleanup = Cleanup(path.clone());
    /// # {
    /// #     use oxigeo::builder::{DatasetCreateBuilder, OutputFormat};
    /// #     use oxigeo::{GeoTransform, RasterDataType};
    /// #     let mut writer =
    /// #         DatasetCreateBuilder::new(&path, OutputFormat::GeoTiff).create()?;
    /// #     writer.set_dimensions(2, 2, 3)?;
    /// #     writer.set_data_type(RasterDataType::UInt8);
    /// #     writer.set_geo_transform(GeoTransform::north_up(0.0, 2.0, 1.0, 1.0));
    /// #     // Four RGB pixels: (10,20,30), (11,21,31), (12,22,32), (13,23,33),
    /// #     // handed to the writer band-sequentially, as it expects.
    /// #     let planes: Vec<u8> =
    /// #         [10u8, 20, 30].iter().flat_map(|b| (0..4u8).map(move |p| b + p)).collect();
    /// #     writer.write_all_bands(&planes)?;
    /// #     writer.finalize()?;
    /// # }
    /// # let path = path.to_string_lossy().into_owned();
    /// let ds = Dataset::open(&path)?;
    ///
    /// // Every band, interleaved — the old `read_band` result, now explicit.
    /// let rgb: Vec<u8> = ds.read_interleaved(None)?;
    /// assert_eq!(rgb.len(), 2 * 2 * 3);
    /// assert_eq!(&rgb[..3], &[10, 20, 30]);
    ///
    /// // Just the bands you want, in the order you want them.
    /// let bgr: Vec<u8> = ds.read_interleaved(Some(&[2, 1, 0]))?;
    /// assert_eq!(&bgr[..3], &[30, 20, 10]);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Cost
    ///
    /// One allocation for the result, plus the fixed scratch
    /// [`Self::read_interleaved_into`] uses.  Every block the selection touches
    /// is decoded exactly once however many bands are named, so on a chunky file
    /// an `n`-band read costs one pass over the data rather than `n`; and naming
    /// a subset really does skip work — a block no selected band lives in is
    /// never fetched at all.
    ///
    /// # Errors
    ///
    /// - [`OxiGeoError::NotSupported`] — format is not a supported raster type.
    /// - [`OxiGeoError::InvalidParameter`] — `bands` is empty or names an index
    ///   out of range.
    /// - [`OxiGeoError::Io`] / [`OxiGeoError::Format`] — underlying read failure.
    pub fn read_interleaved<T: RasterElement>(&self, bands: Option<&[u32]>) -> Result<Vec<T>> {
        #[cfg(feature = "geotiff")]
        if matches!(self.info.format, DatasetFormat::GeoTiff) {
            let reader = self.open_geotiff_reader()?;
            let window = self.resolve_source_window(&reader, None)?;
            return self.alloc_interleaved(&reader, bands, window);
        }

        #[cfg(feature = "vrt")]
        if matches!(self.info.format, DatasetFormat::Vrt) {
            let reader = self.open_vrt_reader()?;
            let window = self.resolve_vrt_window(&reader, None)?;
            let selection = self.resolve_vrt_bands(bands)?;
            let pixels =
                usize::try_from(window.x_size.saturating_mul(window.y_size)).map_err(|_| {
                    OxiGeoError::InvalidParameter {
                        parameter: "window",
                        message: "window is too large for this platform".to_string(),
                    }
                })?;
            let mut out = vec![T::default(); pixels.saturating_mul(selection.len())];
            self.read_vrt_interleaved(&reader, &selection, window, &mut out)?;
            return Ok(out);
        }

        let _ = bands;
        Err(self.unsupported("read_interleaved()"))
    }

    /// Read several bands at once, pixel-interleaved, directly into a
    /// caller-supplied buffer, converting to `T` in one pass.
    ///
    /// The multi-band counterpart of [`Self::read_band_into`], and the fast path
    /// for anyone who used to rely on `read_band` returning the whole
    /// interleaved image.  `dst.len()` must be exactly
    /// `width × height × bands`, where `width`/`height` are the dataset's
    /// **current** (clipped) extent — see `Clip semantics` in the module docs.
    ///
    /// See [`Self::read_interleaved`] for how `bands` selects and orders the
    /// bands, and for the destination layout.
    ///
    /// # Cost
    ///
    /// A fixed handful of block-sized scratch buffers inside the driver, plus a
    /// `bands.len()`-element index list.  Nothing is sized by the raster, and no
    /// full-size intermediate is ever materialised, so peak memory over a
    /// 10000×10000 `f64` read is the same as over a 64×64 one.  When `bands`
    /// names a single band this delegates straight to [`Self::read_band_into`]'s
    /// path, which allocates nothing at all.
    ///
    /// The bands are read *together*, not one after another: each block is
    /// decoded once and every selected band is lifted out of it before it is
    /// discarded.  On a chunky file — `PlanarConfiguration = 1`, which is what
    /// almost every RGB, RGBA or multispectral GeoTIFF is — one block holds all
    /// of a pixel's bands, so this is the difference between decompressing the
    /// file once and decompressing it once per band.  On a planar file a block
    /// holds a single band and there is nothing to share, so the read is one
    /// pass per *distinct* band; a band repeated in `bands` is copied from the
    /// slot that already holds it rather than decoded again.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use oxigeo::Dataset;
    ///
    /// # fn main() -> oxigeo::Result<()> {
    /// let ds = Dataset::open("scene.tif")?;
    /// let (width, height) = (ds.width() as usize, ds.height() as usize);
    ///
    /// // Bands 3, 2, 1 of a 5-band scene as an RGB f32 image; the two unnamed
    /// // bands are never decoded.
    /// let mut rgb = vec![0.0f32; width * height * 3];
    /// ds.read_interleaved_into(Some(&[3, 2, 1]), &mut rgb)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// - [`OxiGeoError::NotSupported`] — format is not a supported raster type.
    /// - [`OxiGeoError::InvalidParameter`] — `bands` is empty or names an index
    ///   out of range, or `dst.len()` is not exactly
    ///   `width × height × bands.len()` (the error names the expected length; a
    ///   wrong length is never silently truncated).
    /// - [`OxiGeoError::Io`] / [`OxiGeoError::Format`] — underlying read failure.
    pub fn read_interleaved_into<T: RasterElement>(
        &self,
        bands: Option<&[u32]>,
        dst: &mut [T],
    ) -> Result<()> {
        #[cfg(feature = "geotiff")]
        if matches!(self.info.format, DatasetFormat::GeoTiff) {
            let reader = self.open_geotiff_reader()?;
            let window = self.resolve_source_window(&reader, None)?;
            let selection = self.resolve_bands(&reader, bands)?;
            return self.read_interleaved_with(&reader, &selection, window, dst);
        }

        #[cfg(feature = "vrt")]
        if matches!(self.info.format, DatasetFormat::Vrt) {
            let reader = self.open_vrt_reader()?;
            let window = self.resolve_vrt_window(&reader, None)?;
            let selection = self.resolve_vrt_bands(bands)?;
            return self.read_vrt_interleaved(&reader, &selection, window, dst);
        }

        let _ = (bands, dst);
        Err(self.unsupported("read_interleaved_into()"))
    }

    /// Read a rectangular pixel window of several bands, pixel-interleaved, into
    /// a fresh `Vec<T>`.
    ///
    /// [`Self::read_interleaved`] restricted to the `width × height`
    /// sub-rectangle at `(col, row)` in this dataset's current pixel grid, in the
    /// same way [`Self::read_window`] restricts [`Self::read_band`].  Only the
    /// tiles or strips the window overlaps are fetched and decoded, for each
    /// selected band.
    ///
    /// # Errors
    ///
    /// - [`OxiGeoError::NotSupported`] — format is not a supported raster type.
    /// - [`OxiGeoError::InvalidParameter`] — `bands` is empty or out of range, or
    ///   the window has zero size or extends past the dataset extent.
    /// - [`OxiGeoError::Io`] / [`OxiGeoError::Format`] — underlying read failure.
    pub fn read_window_interleaved<T: RasterElement>(
        &self,
        bands: Option<&[u32]>,
        col: u32,
        row: u32,
        width: u32,
        height: u32,
    ) -> Result<Vec<T>> {
        Self::validate_window_size(width, height)?;

        #[cfg(feature = "geotiff")]
        if matches!(self.info.format, DatasetFormat::GeoTiff) {
            let reader = self.open_geotiff_reader()?;
            let window = self.resolve_source_window(&reader, Some((col, row, width, height)))?;
            return self.alloc_interleaved(&reader, bands, window);
        }

        #[cfg(feature = "vrt")]
        if matches!(self.info.format, DatasetFormat::Vrt) {
            let reader = self.open_vrt_reader()?;
            let window = self.resolve_vrt_window(&reader, Some((col, row, width, height)))?;
            let selection = self.resolve_vrt_bands(bands)?;
            let pixels =
                usize::try_from(window.x_size.saturating_mul(window.y_size)).map_err(|_| {
                    OxiGeoError::InvalidParameter {
                        parameter: "window",
                        message: "window is too large for this platform".to_string(),
                    }
                })?;
            let mut out = vec![T::default(); pixels.saturating_mul(selection.len())];
            self.read_vrt_interleaved(&reader, &selection, window, &mut out)?;
            return Ok(out);
        }

        let _ = (bands, col, row);
        Err(self.unsupported("read_window_interleaved()"))
    }

    /// Read a rectangular pixel window of several bands, pixel-interleaved,
    /// directly into a caller-supplied buffer, converting to `T` in one pass.
    ///
    /// The windowed counterpart of [`Self::read_interleaved_into`]: `(col, row)`
    /// is the window's upper-left corner in this dataset's **current** pixel grid
    /// and `dst.len()` must be exactly `width × height × bands`.
    ///
    /// This is the primitive for walking a multi-band raster tile by tile with a
    /// reusable interleaved buffer — an RGB tile server's inner loop.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use oxigeo::Dataset;
    ///
    /// # fn main() -> oxigeo::Result<()> {
    /// let ds = Dataset::open("scene.tif")?;
    /// let mut tile = vec![0u8; 256 * 256 * 3];
    /// // Reuse `tile` for every window — no per-tile allocation.
    /// ds.read_window_interleaved_into(Some(&[0, 1, 2]), 0, 0, 256, 256, &mut tile)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// - [`OxiGeoError::NotSupported`] — format is not a supported raster type.
    /// - [`OxiGeoError::InvalidParameter`] — `bands` is empty or out of range, the
    ///   window has zero size or extends past the dataset extent, or `dst.len()`
    ///   is not exactly `width × height × bands.len()`.
    /// - [`OxiGeoError::Io`] / [`OxiGeoError::Format`] — underlying read failure.
    pub fn read_window_interleaved_into<T: RasterElement>(
        &self,
        bands: Option<&[u32]>,
        col: u32,
        row: u32,
        width: u32,
        height: u32,
        dst: &mut [T],
    ) -> Result<()> {
        Self::validate_window_size(width, height)?;

        #[cfg(feature = "geotiff")]
        if matches!(self.info.format, DatasetFormat::GeoTiff) {
            let reader = self.open_geotiff_reader()?;
            let window = self.resolve_source_window(&reader, Some((col, row, width, height)))?;
            let selection = self.resolve_bands(&reader, bands)?;
            return self.read_interleaved_with(&reader, &selection, window, dst);
        }

        #[cfg(feature = "vrt")]
        if matches!(self.info.format, DatasetFormat::Vrt) {
            let reader = self.open_vrt_reader()?;
            let window = self.resolve_vrt_window(&reader, Some((col, row, width, height)))?;
            let selection = self.resolve_vrt_bands(bands)?;
            return self.read_vrt_interleaved(&reader, &selection, window, dst);
        }

        let _ = (bands, col, row, dst);
        Err(self.unsupported("read_window_interleaved_into()"))
    }

    // -- shared validation ---------------------------------------------------

    /// Reject a band index the dataset's metadata says cannot exist.
    fn validate_band(&self, band: u32) -> Result<()> {
        if self.info.band_count > 0 && band >= self.info.band_count {
            return Err(OxiGeoError::InvalidParameter {
                parameter: "band",
                message: format!(
                    "band index {} is out of range (dataset has {} bands)",
                    band, self.info.band_count
                ),
            });
        }
        Ok(())
    }

    /// Reject a degenerate window before any I/O happens.
    fn validate_window_size(width: u32, height: u32) -> Result<()> {
        if width == 0 || height == 0 {
            return Err(OxiGeoError::InvalidParameter {
                parameter: "window",
                message: format!("window size must be non-zero, got {width}×{height}"),
            });
        }
        Ok(())
    }

    // -- VRT plumbing --------------------------------------------------------

    /// Open the dataset's file as a VRT reader.
    ///
    /// Every pixel-read entry point used to be gated on
    /// `DatasetFormat::GeoTiff`, so a `.vrt` — including a warped VRT, the
    /// subject of cool-japan/oxigeo#15 — answered every read with
    /// `NotSupported` even though the driver behind [`oxigeo_vrt`] could serve
    /// it.
    #[cfg(feature = "vrt")]
    fn open_vrt_reader(&self) -> Result<oxigeo_vrt::VrtReader> {
        oxigeo_vrt::VrtReader::open(&self.path).map_err(|e| {
            OxiGeoError::Format(oxigeo_core::error::FormatError::InvalidHeader {
                message: format!("failed to open VRT '{}': {e}", self.path),
            })
        })
    }

    /// Fold the clip window and the caller's request into one VRT pixel
    /// rectangle.
    #[cfg(feature = "vrt")]
    fn resolve_vrt_window(
        &self,
        reader: &oxigeo_vrt::VrtReader,
        request: Option<(u32, u32, u32, u32)>,
    ) -> Result<oxigeo_vrt::PixelRect> {
        let (file_w, file_h) = (reader.width(), reader.height());

        let (view_x, view_y, view_w, view_h) = match self.clip_window {
            Some(window) => (
                u64::from(window.col),
                u64::from(window.row),
                u64::from(window.width),
                u64::from(window.height),
            ),
            None => (0, 0, file_w, file_h),
        };

        if view_x.saturating_add(view_w) > file_w || view_y.saturating_add(view_h) > file_h {
            return Err(OxiGeoError::Internal {
                message: format!(
                    "clip window [{view_x},{view_y} {view_w}×{view_h}] does not fit VRT raster {file_w}×{file_h}"
                ),
            });
        }

        let (x, y, width, height) = match request {
            Some((col, row, width, height)) => {
                let (col, row) = (u64::from(col), u64::from(row));
                let (width, height) = (u64::from(width), u64::from(height));
                if col.saturating_add(width) > view_w || row.saturating_add(height) > view_h {
                    return Err(OxiGeoError::InvalidParameter {
                        parameter: "window",
                        message: format!(
                            "window [{col},{row} {width}×{height}] extends past the dataset extent {view_w}×{view_h}"
                        ),
                    });
                }
                (view_x + col, view_y + row, width, height)
            }
            None => (view_x, view_y, view_w, view_h),
        };

        Ok(oxigeo_vrt::PixelRect::new(x, y, width, height))
    }

    /// Read one VRT band's window as a buffer in the band's own element type.
    #[cfg(feature = "vrt")]
    fn read_vrt_buffer(
        &self,
        reader: &oxigeo_vrt::VrtReader,
        band: u32,
        window: oxigeo_vrt::PixelRect,
    ) -> Result<RasterBuffer> {
        // The VRT driver numbers bands from 1; this API numbers them from 0.
        let band_1based = usize::try_from(band)
            .map_err(|_| OxiGeoError::InvalidParameter {
                parameter: "band",
                message: format!("band index {band} does not fit this platform's usize"),
            })?
            .saturating_add(1);

        reader.read_window(band_1based, window).map_err(|e| {
            OxiGeoError::Format(oxigeo_core::error::FormatError::InvalidHeader {
                message: format!("failed to read VRT '{}' band {band}: {e}", self.path),
            })
        })
    }

    /// Read several VRT bands pixel-interleaved into `dst`, converting to `T`.
    #[cfg(feature = "vrt")]
    fn read_vrt_interleaved<T: RasterElement>(
        &self,
        reader: &oxigeo_vrt::VrtReader,
        bands: &[u32],
        window: oxigeo_vrt::PixelRect,
        dst: &mut [T],
    ) -> Result<()> {
        let pixels =
            usize::try_from(window.x_size.saturating_mul(window.y_size)).map_err(|_| {
                OxiGeoError::InvalidParameter {
                    parameter: "window",
                    message: "window is too large for this platform".to_string(),
                }
            })?;
        let expected =
            pixels
                .checked_mul(bands.len())
                .ok_or_else(|| OxiGeoError::InvalidParameter {
                    parameter: "window",
                    message: "window × band count overflows".to_string(),
                })?;

        if dst.len() != expected {
            return Err(OxiGeoError::InvalidParameter {
                parameter: "dst",
                message: format!(
                    "destination length {} does not match the {expected} samples this read produces",
                    dst.len()
                ),
            });
        }

        let stride = bands.len();
        for (slot, band) in bands.iter().enumerate() {
            let buffer = self.read_vrt_buffer(reader, *band, window)?;
            let samples = buffer.as_bytes();
            let source_type = buffer.data_type();
            let sample_size = source_type.size_bytes();

            for index in 0..pixels {
                let offset = index * sample_size;
                let value = samples
                    .get(offset..offset + sample_size)
                    .and_then(|bytes| read_native_sample(bytes, source_type))
                    .unwrap_or(0.0);
                dst[index * stride + slot] = T::from_raster_f64(value);
            }
        }

        Ok(())
    }

    /// Expand a band selection to a concrete list, defaulting to every band.
    #[cfg(feature = "vrt")]
    fn resolve_vrt_bands(&self, bands: Option<&[u32]>) -> Result<Vec<u32>> {
        match bands {
            Some(list) => {
                if list.is_empty() {
                    return Err(OxiGeoError::InvalidParameter {
                        parameter: "bands",
                        message: "band selection must name at least one band".to_string(),
                    });
                }
                for band in list {
                    self.validate_band(*band)?;
                }
                Ok(list.to_vec())
            }
            None => Ok((0..self.info.band_count).collect()),
        }
    }

    /// The "this format cannot do pixel reads" error, worded per operation.
    fn unsupported(&self, operation: &str) -> OxiGeoError {
        OxiGeoError::NotSupported {
            operation: format!(
                "{operation} is not supported for format '{}' (enable the 'geotiff' feature for GeoTIFF support)",
                self.info.format.driver_name()
            ),
        }
    }

    // -- GeoTIFF plumbing ----------------------------------------------------

    /// Open the dataset's file as a GeoTIFF reader.
    #[cfg(feature = "geotiff")]
    fn open_geotiff_reader(&self) -> Result<LocalTiffReader> {
        let source = FileDataSource::open(&self.path).map_err(|e| {
            OxiGeoError::Io(oxigeo_core::error::IoError::Read {
                message: format!("failed to open '{}': {e}", self.path),
            })
        })?;
        GeoTiffReader::open(source)
    }

    /// Fold the dataset's clip window and an optional caller window request into
    /// a single rectangle in the source file's pixel grid.
    ///
    /// `request` is `(col, row, width, height)` in the dataset's *current*
    /// (possibly clipped) grid; `None` means "the whole current extent".
    #[cfg(feature = "geotiff")]
    fn resolve_source_window(
        &self,
        reader: &LocalTiffReader,
        request: Option<(u32, u32, u32, u32)>,
    ) -> Result<SourceWindow> {
        let file_w = reader.width();
        let file_h = reader.height();

        // The dataset's current view within the file.
        let (view_x, view_y, view_w, view_h) = match self.clip_window {
            Some(window) => (
                u64::from(window.col),
                u64::from(window.row),
                u64::from(window.width),
                u64::from(window.height),
            ),
            None => (0, 0, file_w, file_h),
        };

        // A recorded clip window that does not fit the file it was recorded
        // against is a bug, not a read the caller can be given wrong pixels for.
        if view_x.saturating_add(view_w) > file_w || view_y.saturating_add(view_h) > file_h {
            return Err(OxiGeoError::Internal {
                message: format!(
                    "clip window [{view_x},{view_y} {view_w}×{view_h}] does not fit source raster {file_w}×{file_h}"
                ),
            });
        }

        let (x, y, width, height) = match request {
            Some((col, row, width, height)) => {
                let (col, row) = (u64::from(col), u64::from(row));
                let (width, height) = (u64::from(width), u64::from(height));
                if col.saturating_add(width) > view_w || row.saturating_add(height) > view_h {
                    return Err(OxiGeoError::InvalidParameter {
                        parameter: "window",
                        message: format!(
                            "window [{col},{row} {width}×{height}] extends past dataset extent {view_w}×{view_h}"
                        ),
                    });
                }
                (view_x + col, view_y + row, width, height)
            }
            None => (view_x, view_y, view_w, view_h),
        };

        Ok(SourceWindow {
            x,
            y,
            width,
            height,
            full_band: x == 0 && y == 0 && width == file_w && height == file_h,
        })
    }

    /// Bytes occupied by one sample of `reader`'s bands.
    ///
    /// Derived from the driver's own band geometry rather than from
    /// [`RasterDataType::size_bytes`], so a file whose sample format is not a
    /// recognised [`RasterDataType`] still gets a correctly-sized destination.
    #[cfg(feature = "geotiff")]
    fn sample_size(reader: &LocalTiffReader, data_type: RasterDataType) -> Result<usize> {
        let pixels = reader.band_pixel_count(0)?;
        if pixels == 0 {
            return Ok(data_type.size_bytes());
        }
        Ok(reader.band_byte_len(0)? / pixels)
    }

    /// Read `window` of `band` into a freshly-allocated [`RasterBuffer`].
    ///
    /// Exactly one allocation happens here: the buffer that is returned.
    #[cfg(feature = "geotiff")]
    fn read_buffer_with(
        &self,
        reader: &LocalTiffReader,
        band: u32,
        window: SourceWindow,
    ) -> Result<RasterBuffer> {
        let data_type = reader.data_type().unwrap_or(RasterDataType::UInt8);
        let sample_size = Self::sample_size(reader, data_type)?;
        let len = window
            .pixel_count()?
            .checked_mul(sample_size)
            .ok_or_else(|| OxiGeoError::Internal {
                message: format!(
                    "window {}×{} of {sample_size}-byte samples overflows the address space",
                    window.width, window.height
                ),
            })?;

        let mut bytes = vec![0u8; len];
        if window.full_band {
            reader.read_band_into(0, band as usize, &mut bytes)?;
        } else {
            reader.read_window_into(
                0,
                band as usize,
                window.x,
                window.y,
                window.width,
                window.height,
                &mut bytes,
            )?;
        }

        RasterBuffer::new(
            bytes,
            window.width,
            window.height,
            data_type,
            NoDataValue::None,
        )
        .map_err(|e| OxiGeoError::Internal {
            message: format!("failed to create RasterBuffer: {e}"),
        })
    }

    /// Read `window` of `band` straight into `dst`, converting to `T` on the way.
    ///
    /// Allocates nothing.
    #[cfg(feature = "geotiff")]
    fn read_typed_with<T: RasterElement>(
        &self,
        reader: &LocalTiffReader,
        band: u32,
        window: SourceWindow,
        dst: &mut [T],
    ) -> Result<()> {
        let expected = window.pixel_count()?;
        if dst.len() != expected {
            return Err(OxiGeoError::InvalidParameter {
                parameter: "dst",
                message: format!(
                    "destination buffer must hold exactly {expected} elements ({}×{} pixels), got {}",
                    window.width,
                    window.height,
                    dst.len()
                ),
            });
        }

        if window.full_band {
            reader.read_band_into_typed(0, band as usize, dst)
        } else {
            reader.read_window_into_typed(
                0,
                band as usize,
                window.x,
                window.y,
                window.width,
                window.height,
                dst,
            )
        }
    }

    // -- interleaved plumbing ------------------------------------------------

    /// Turn a caller's band selection into the concrete list of bands to read.
    ///
    /// `None` expands to every band in file order; a caller-supplied list is
    /// borrowed as-is after every index has been range-checked.  The dataset's
    /// recorded band count wins when it is known, so a `Dataset` built from
    /// metadata stays authoritative; otherwise the reader is asked.
    #[cfg(feature = "geotiff")]
    fn resolve_bands<'a>(
        &self,
        reader: &LocalTiffReader,
        bands: Option<&'a [u32]>,
    ) -> Result<std::borrow::Cow<'a, [u32]>> {
        match bands {
            Some(list) => {
                if list.is_empty() {
                    return Err(OxiGeoError::InvalidParameter {
                        parameter: "bands",
                        message: "band selection must name at least one band".to_string(),
                    });
                }
                for &band in list {
                    self.validate_band(band)?;
                }
                Ok(std::borrow::Cow::Borrowed(list))
            }
            None => {
                let count = if self.info.band_count > 0 {
                    self.info.band_count
                } else {
                    reader.band_count()
                };
                if count == 0 {
                    return Err(OxiGeoError::InvalidParameter {
                        parameter: "bands",
                        message: "dataset reports no raster bands to interleave".to_string(),
                    });
                }
                Ok(std::borrow::Cow::Owned((0..count).collect()))
            }
        }
    }

    /// Allocate the interleaved destination and fill it.
    ///
    /// Exactly one allocation beyond what [`Self::read_interleaved_with`] itself
    /// costs: the buffer that is returned.
    #[cfg(feature = "geotiff")]
    fn alloc_interleaved<T: RasterElement>(
        &self,
        reader: &LocalTiffReader,
        bands: Option<&[u32]>,
        window: SourceWindow,
    ) -> Result<Vec<T>> {
        let selection = self.resolve_bands(reader, bands)?;
        let len = window
            .pixel_count()?
            .checked_mul(selection.len())
            .ok_or_else(|| OxiGeoError::Internal {
                message: format!(
                    "{}×{} pixels × {} bands overflows the address space",
                    window.width,
                    window.height,
                    selection.len()
                ),
            })?;

        let mut out = vec![T::default(); len];
        self.read_interleaved_with(reader, &selection, window, &mut out)?;
        Ok(out)
    }

    /// Weave `bands` of `window` into `dst`, pixel-interleaved, converting to `T`.
    ///
    /// Validation lives here — an empty selection and a mis-sized `dst` are
    /// rejected before a single byte is read, so a rejected call never leaves
    /// half a picture behind — and the read itself is handed to the driver's
    /// multi-band entry points, `read_bands_into_typed` and
    /// `read_window_bands_into_typed`.
    ///
    /// That delegation is the whole point.  A chunky GeoTIFF
    /// (`PlanarConfiguration = 1`, which is what almost every RGB, RGBA or
    /// multispectral file is) stores all of a pixel's bands in the *same* block,
    /// so an interleave built out of one-band-at-a-time calls decompresses every
    /// block once per selected band and discards the rest of each decode.
    /// Decompression dominates the read, so that was an `n`× waste on the most
    /// common layout there is.  The driver decodes each block once and pulls
    /// every requested slot out of it before the block is discarded; on a planar
    /// file, where a block holds exactly one band and there is no shared decode
    /// to exploit, it runs one pass per *distinct* band and copies a repeated
    /// band from the slot that already holds it.
    ///
    /// Peak memory stays bounded by the block geometry rather than by the
    /// raster: the driver allocates a fixed handful of scratch buffers per read
    /// (per rayon worker under its `parallel` feature) whatever the raster size,
    /// and this side of the call allocates only the `bands.len()`-element index
    /// list the driver's signature wants.  That guarantee used to be provided
    /// here, by walking the destination in horizontal strips; it now lives where
    /// the decode does.
    #[cfg(feature = "geotiff")]
    fn read_interleaved_with<T: RasterElement>(
        &self,
        reader: &LocalTiffReader,
        bands: &[u32],
        window: SourceWindow,
        dst: &mut [T],
    ) -> Result<()> {
        // `resolve_bands` already rejects an empty selection; re-checking here
        // keeps this entry point self-contained, whoever calls it in future, and
        // makes the facade — not the driver — the one that words the error.
        if bands.is_empty() {
            return Err(OxiGeoError::InvalidParameter {
                parameter: "bands",
                message: "band selection must name at least one band".to_string(),
            });
        }

        let pixels = window.pixel_count()?;
        let slots = bands.len();
        let expected = pixels
            .checked_mul(slots)
            .ok_or_else(|| OxiGeoError::Internal {
                message: format!(
                    "{}×{} pixels × {slots} bands overflows the address space",
                    window.width, window.height
                ),
            })?;
        if dst.len() != expected {
            return Err(OxiGeoError::InvalidParameter {
                parameter: "dst",
                message: format!(
                    "destination buffer must hold exactly {expected} elements ({}×{} pixels × {slots} bands), got {}",
                    window.width,
                    window.height,
                    dst.len()
                ),
            });
        }

        // One band is not an interleave: hand it to the single-band path, which
        // decodes straight into `dst` with no index list and no weave at all.
        if let [band] = bands {
            return self.read_typed_with(reader, *band, window, dst);
        }

        // The driver indexes bands by `usize`; the facade's public surface uses
        // `u32`.  This is the only allocation on this side of the call, and it is
        // sized by the band count, not by the raster.
        let selection = bands
            .iter()
            .map(|&band| {
                usize::try_from(band).map_err(|_| OxiGeoError::Internal {
                    message: format!("band index {band} overflows the address space"),
                })
            })
            .collect::<Result<Vec<usize>>>()?;

        if window.full_band {
            reader.read_bands_into_typed(0, &selection, dst)
        } else {
            reader.read_window_bands_into_typed(
                0,
                &selection,
                window.x,
                window.y,
                window.width,
                window.height,
                dst,
            )
        }
    }
}

/// Lazy iterator over raster bands of a [`Dataset`].
///
/// Created by [`Dataset::bands`].  Each call to [`Iterator::next`] reads the
/// next band from the underlying file and returns `Ok(RasterBuffer)` on
/// success or an `Err` on I/O or format failure.
///
/// The underlying file is opened once, on the first `next()`, and the reader is
/// reused for every band — an N-band file costs one open and one header parse,
/// not N.
pub struct BandIter<'a> {
    /// Reference to the dataset being iterated.
    dataset: &'a Dataset,
    /// Index of the next band to yield.
    next_band: u32,
    /// Total number of bands (cached to avoid repeated accessors).
    band_count: u32,
    /// GeoTIFF reader, opened lazily on the first `next()` and reused after.
    #[cfg(feature = "geotiff")]
    reader: Option<LocalTiffReader>,
    /// Set once opening the reader has failed, so the iterator stops retrying
    /// the shared open and falls back to the per-band path (which reports the
    /// real error for every remaining band, keeping the item count exact).
    #[cfg(feature = "geotiff")]
    reader_unavailable: bool,
}

impl BandIter<'_> {
    /// Read `band` through the cached reader when one can be had.
    ///
    /// Returns `None` when this dataset has no shared-reader fast path (wrong
    /// format, or the open failed), leaving the caller to fall back to
    /// [`Dataset::read_band`].
    #[cfg(feature = "geotiff")]
    fn read_shared(&mut self, band: u32) -> Option<Result<RasterBuffer>> {
        if !matches!(self.dataset.info.format, DatasetFormat::GeoTiff) {
            return None;
        }
        if self.reader.is_none() && !self.reader_unavailable {
            match self.dataset.open_geotiff_reader() {
                Ok(reader) => self.reader = Some(reader),
                Err(_) => self.reader_unavailable = true,
            }
        }
        let reader = self.reader.as_ref()?;
        Some(
            self.dataset
                .resolve_source_window(reader, None)
                .and_then(|window| self.dataset.read_buffer_with(reader, band, window)),
        )
    }
}

impl Iterator for BandIter<'_> {
    type Item = Result<RasterBuffer>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.next_band >= self.band_count {
            return None;
        }
        let band = self.next_band;
        self.next_band += 1;

        #[cfg(feature = "geotiff")]
        if let Some(result) = self.read_shared(band) {
            return Some(result);
        }

        Some(self.dataset.read_band(band))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = (self.band_count.saturating_sub(self.next_band)) as usize;
        (remaining, Some(remaining))
    }
}

impl core::iter::ExactSizeIterator for BandIter<'_> {}

/// Decode one host-native sample of `data_type` from `bytes`.
///
/// VRT buffers carry samples in the host's byte order (the driver normalises
/// them on decode), so this is a plain reinterpret, not a byte-order swap.
#[cfg(feature = "vrt")]
fn read_native_sample(bytes: &[u8], data_type: RasterDataType) -> Option<f64> {
    let value = match data_type {
        RasterDataType::UInt8 => f64::from(*bytes.first()?),
        RasterDataType::Int8 => f64::from(*bytes.first()? as i8),
        RasterDataType::UInt16 => f64::from(u16::from_ne_bytes(bytes.try_into().ok()?)),
        RasterDataType::Int16 => f64::from(i16::from_ne_bytes(bytes.try_into().ok()?)),
        RasterDataType::UInt32 => f64::from(u32::from_ne_bytes(bytes.try_into().ok()?)),
        RasterDataType::Int32 => f64::from(i32::from_ne_bytes(bytes.try_into().ok()?)),
        RasterDataType::Float32 => f64::from(f32::from_ne_bytes(bytes.try_into().ok()?)),
        RasterDataType::Float64 => f64::from_ne_bytes(bytes.try_into().ok()?),
        _ => return None,
    };
    Some(value)
}

// Every test below drives a real GeoTIFF through `Dataset`, so the whole module
// — imports included — belongs to the `geotiff` feature; there is nothing left to
// compile without it.
#[cfg(all(test, feature = "geotiff"))]
mod tests {
    // House convention: test helper fns are not covered by clippy.toml's
    // `allow-expect-in-tests`, which only exempts `#[test]` fns themselves.
    #![allow(clippy::expect_used)]

    use super::*;
    use std::path::{Path, PathBuf};
    use std::sync::atomic::{AtomicU64, Ordering};

    /// Per-test scratch fixture inside the system temp dir (house policy: no
    /// hardcoded absolute paths).
    ///
    /// The leaf name embeds the process id and a monotonic counter, so no two
    /// test binaries — nor two concurrent runs of this one — can ever land on
    /// the same file.  Dropping the guard removes the fixture, so a panicking
    /// test leaks nothing.
    struct TempPath(PathBuf);

    impl TempPath {
        fn new(name: &str) -> Self {
            static COUNTER: AtomicU64 = AtomicU64::new(0);
            let seq = COUNTER.fetch_add(1, Ordering::Relaxed);
            Self(std::env::temp_dir().join(format!(
                "oxigeo_raster_read_{}_{seq}_{name}",
                std::process::id()
            )))
        }
    }

    impl std::ops::Deref for TempPath {
        type Target = Path;

        fn deref(&self) -> &Path {
            &self.0
        }
    }

    impl AsRef<Path> for TempPath {
        fn as_ref(&self) -> &Path {
            &self.0
        }
    }

    impl Drop for TempPath {
        fn drop(&mut self) {
            let _ = std::fs::remove_file(&self.0);
        }
    }
    use crate::{BoundingBox, DatasetInfo, GeoTransform};
    use oxigeo_core::types::RasterDataType;

    #[test]
    fn test_crop_interleaved_single_band() {
        // 4×4 single-band (1 byte/pixel) buffer 0..16, crop a 2×2 window at (1,1).
        let data: Vec<u8> = (0u8..16).collect();
        let window = PixelWindow {
            col: 1,
            row: 1,
            width: 2,
            height: 2,
        };
        let out = crop_interleaved(&data, 4, 4, window).expect("crop");
        // Rows 1 and 2, cols 1 and 2: {5,6, 9,10}
        assert_eq!(out, vec![5, 6, 9, 10]);
    }

    #[test]
    fn test_crop_interleaved_multiband_stride() {
        // 2×2 image, 3 bytes/pixel (RGB-like). Pixel (x,y) = [x, y, 0].
        let data: Vec<u8> = vec![
            0, 0, 0, /* (0,0) */ 1, 0, 0, /* (1,0) */
            0, 1, 0, /* (0,1) */ 1, 1, 0, /* (1,1) */
        ];
        let window = PixelWindow {
            col: 1,
            row: 0,
            width: 1,
            height: 2,
        };
        let out = crop_interleaved(&data, 2, 2, window).expect("crop");
        // Column x=1, both rows: [1,0,0] then [1,1,0]
        assert_eq!(out, vec![1, 0, 0, 1, 1, 0]);
    }

    #[test]
    fn test_crop_interleaved_out_of_bounds_none() {
        let data: Vec<u8> = (0u8..16).collect();
        let window = PixelWindow {
            col: 3,
            row: 3,
            width: 2,
            height: 2,
        };
        assert!(crop_interleaved(&data, 4, 4, window).is_none());
    }

    pub(crate) fn write_test_geotiff_4x4(path: &std::path::Path) {
        use crate::builder::{DatasetCreateBuilder, OutputFormat};
        let mut writer = DatasetCreateBuilder::new(path, OutputFormat::GeoTiff)
            .create()
            .expect("create writer");
        writer.set_dimensions(4, 4, 1).expect("dims");
        writer.set_data_type(RasterDataType::UInt8);
        writer.set_geo_transform(GeoTransform::north_up(0.0, 4.0, 1.0, 1.0));
        let data: Vec<u8> = (0u8..16).collect();
        writer.write_all_bands(&data).expect("write bands");
        writer.finalize().expect("finalize");
    }

    // Build a `Dataset` that points at a real on-disk 4×4 GeoTIFF and carries an
    // explicit geo-transform, so the clip window can be exercised against real
    // pixel reads independently of what the writer recorded on disk.
    pub(crate) fn dataset_over_4x4(path: &std::path::Path) -> Dataset {
        let gt = GeoTransform::north_up(0.0, 4.0, 1.0, 1.0);
        let info = DatasetInfo {
            format: crate::DatasetFormat::GeoTiff,
            path: Some(path.to_string_lossy().into_owned()),
            width: Some(4),
            height: Some(4),
            band_count: 1,
            geotransform: Some(gt),
            data_type: Some(RasterDataType::UInt8),
            ..DatasetInfo::default()
        };
        Dataset::from_info(path.to_string_lossy().into_owned(), info)
    }

    #[test]
    fn test_read_window_reads_real_pixels() {
        let path = TempPath::new("read_window_test.tif");
        write_test_geotiff_4x4(&path);

        let ds = Dataset::open(path.to_str().expect("path")).expect("open");
        let buf = ds.read_window(0, 1, 1, 2, 2).expect("read window");
        assert_eq!(buf.width(), 2);
        assert_eq!(buf.height(), 2);
        assert_eq!(buf.as_bytes(), &[5u8, 6, 9, 10]);

        // Out-of-bounds window is rejected.
        assert!(ds.read_window(0, 3, 3, 2, 2).is_err());
    }

    #[test]
    fn test_read_band_into_matches_read_band() {
        let path = TempPath::new("read_band_into_unit.tif");
        write_test_geotiff_4x4(&path);

        let ds = Dataset::open(path.to_str().expect("path")).expect("open");
        let buf = ds.read_band(0).expect("read band");

        let mut dst = vec![0u8; 16];
        ds.read_band_into(0, &mut dst).expect("read band into");
        assert_eq!(dst.as_slice(), buf.as_bytes());

        // Widening conversion is fused into the read.
        let mut wide = vec![0.0f64; 16];
        ds.read_band_into(0, &mut wide).expect("read band into f64");
        let expected: Vec<f64> = (0..16).map(|v| v as f64).collect();
        assert_eq!(wide, expected);

        // A wrong-length destination is an error naming the expected length.
        let mut short = vec![0.0f64; 15];
        let err = ds
            .read_band_into(0, &mut short)
            .expect_err("wrong length must error");
        assert!(
            err.to_string().contains("16"),
            "error should name the expected length: {err}"
        );
    }

    #[test]
    fn test_read_into_honours_clip_window() {
        let path = TempPath::new("read_into_clip_unit.tif");
        write_test_geotiff_4x4(&path);

        let ds = dataset_over_4x4(&path);
        let gt = ds.geotransform().copied().expect("gt");
        let (x0, y0) = gt.pixel_to_world(1.0, 1.0);
        let (x1, y1) = gt.pixel_to_world(3.0, 3.0);
        let bbox = BoundingBox::new(x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)).expect("bbox");
        let clipped = ds.clip(bbox).expect("clip");

        // `read_band_into` on a clipped dataset wants exactly the clipped extent
        // and yields exactly the clipped pixels.
        let mut dst = vec![0u8; clipped.width() as usize * clipped.height() as usize];
        clipped.read_band_into(0, &mut dst).expect("clipped read");
        assert_eq!(dst, vec![5u8, 6, 9, 10]);

        // The full-file length is now the wrong length.
        let mut full = vec![0u8; 16];
        assert!(clipped.read_band_into(0, &mut full).is_err());

        // Windows are relative to the clipped grid.
        let mut one = vec![0u8; 1];
        clipped
            .read_window_into(0, 1, 1, 1, 1, &mut one)
            .expect("clipped window");
        assert_eq!(one, vec![10u8]);
    }

    #[test]
    fn test_clip_is_honored_by_statistics() {
        let path = TempPath::new("clip_stats_test.tif");
        write_test_geotiff_4x4(&path);

        let ds = dataset_over_4x4(&path);
        // Full-raster statistics see all 16 pixels.
        let full = ds.statistics(0).expect("full stats");
        assert_eq!(full.valid_count, 16);

        // Build a bbox covering pixel window cols 1..3, rows 1..3 using the
        // dataset's own geo-transform, then clip.
        let gt = ds.geotransform().copied().expect("gt");
        let (x0, y0) = gt.pixel_to_world(1.0, 1.0);
        let (x1, y1) = gt.pixel_to_world(3.0, 3.0);
        let bbox = BoundingBox::new(x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)).expect("bbox");
        let clipped = ds.clip(bbox).expect("clip");
        assert_eq!(clipped.width(), 2, "clipped width");
        assert_eq!(clipped.height(), 2, "clipped height");

        // Statistics on the clipped dataset must reflect ONLY the 4 clipped
        // pixels {5,6,9,10}, not the full raster.
        let clip_stats = clipped.statistics(0).expect("clip stats");
        assert_eq!(clip_stats.valid_count, 4, "clip honored by statistics");
        assert_eq!(clip_stats.min, 5.0);
        assert_eq!(clip_stats.max, 10.0);
    }

    #[test]
    fn test_clip_is_honored_by_convert() {
        let path = TempPath::new("clip_convert_src.tif");
        write_test_geotiff_4x4(&path);
        let out = TempPath::new("clip_convert_out.tif");

        let ds = dataset_over_4x4(&path);
        let gt = ds.geotransform().copied().expect("gt");
        let (x0, y0) = gt.pixel_to_world(1.0, 1.0);
        let (x1, y1) = gt.pixel_to_world(3.0, 3.0);
        let bbox = BoundingBox::new(x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)).expect("bbox");
        let clipped = ds.clip(bbox).expect("clip");

        let converted = clipped
            .convert(
                &out,
                crate::DatasetFormat::GeoTiff,
                crate::ConversionOptions::default(),
            )
            .expect("convert");
        // The written output must carry the clipped 2×2 dimensions, not 4×4.
        assert_eq!(converted.width(), 2);
        assert_eq!(converted.height(), 2);
        let stats = converted.statistics(0).expect("stats");
        assert_eq!(stats.valid_count, 4);
    }
}