oxigdal-gpkg 0.1.5

Pure Rust GeoPackage (GPKG) reader for OxiGDAL - SQLite format parser without C dependencies
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
//! Tiled Gridded Coverage support for GeoPackage (OGC GeoPackage Extension §F.7).
//!
//! Implements reading and decoding of the two extension tables:
//! - `gpkg_2d_gridded_coverage_ancillary` — per-table coverage metadata
//! - `gpkg_2d_gridded_tile_ancillary` — per-tile scale/offset/statistics
//!
//! These tables are present only when the
//! `gpkg_2d_gridded_coverage` extension is in use (elevation/DEM data).  All
//! functions return `Ok(vec![])` or `Ok(…)` gracefully when the optional tables
//! are absent.

use std::str::FromStr;

use crate::error::GpkgError;
use crate::gpkg::{GeoPackage, cell_to_i64};

// ─────────────────────────────────────────────────────────────────────────────
// CoverageDatatype
// ─────────────────────────────────────────────────────────────────────────────

/// The data type stored in each tile of a gridded coverage.
///
/// Corresponds to the `datatype` column of `gpkg_2d_gridded_coverage_ancillary`.
#[derive(Debug, Clone, PartialEq)]
pub enum CoverageDatatype {
    /// 16-bit (or wider) integer samples — typical for elevation data.
    Integer,
    /// 32-bit IEEE-754 float samples.
    Float,
}

impl CoverageDatatype {
    /// Return the canonical string stored in `gpkg_2d_gridded_coverage_ancillary`.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Integer => "integer",
            Self::Float => "float",
        }
    }
}

impl FromStr for CoverageDatatype {
    type Err = GpkgError;

    /// Parse the `datatype` column string from `gpkg_2d_gridded_coverage_ancillary`.
    ///
    /// # Errors
    /// Returns [`GpkgError::InvalidCoverageDatatype`] for any unrecognised string.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "integer" => Ok(Self::Integer),
            "float" => Ok(Self::Float),
            other => Err(GpkgError::InvalidCoverageDatatype(other.to_string())),
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// GridCellEncoding
// ─────────────────────────────────────────────────────────────────────────────

/// Interpretation of how each grid sample relates to the cell boundary.
///
/// Corresponds to the `grid_cell_encoding` column of
/// `gpkg_2d_gridded_coverage_ancillary` (OGC §F.7).
#[derive(Debug, Clone, PartialEq)]
pub enum GridCellEncoding {
    /// `"grid-value-is-center"` — the value represents the cell centre.
    Grid,
    /// `"grid-value-is-area"` — the value is an average over the entire cell.
    PixelIsArea,
    /// `"grid-value-is-corner"` — the value is located at the cell corner.
    PixelIsPoint,
}

impl GridCellEncoding {
    /// Return the canonical encoding string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Grid => "grid-value-is-center",
            Self::PixelIsArea => "grid-value-is-area",
            Self::PixelIsPoint => "grid-value-is-corner",
        }
    }
}

impl FromStr for GridCellEncoding {
    /// The parse is infallible: unrecognised strings map to the default
    /// ([`GridCellEncoding::Grid`]) per OGC §F.7.
    type Err = std::convert::Infallible;

    /// Parse the `grid_cell_encoding` column string.
    ///
    /// Per OGC §F.7 the encoding is lenient: any unrecognised string maps to
    /// [`GridCellEncoding::Grid`] (the most common default).
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "grid-value-is-center" => Self::Grid,
            "grid-value-is-area" => Self::PixelIsArea,
            "grid-value-is-corner" => Self::PixelIsPoint,
            // Lenient: unknown strings default to Grid per §F.7 note.
            _ => Self::Grid,
        })
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// GriddedCoverage
// ─────────────────────────────────────────────────────────────────────────────

/// A parsed row from `gpkg_2d_gridded_coverage_ancillary`.
///
/// Column layout (0-indexed):
///
/// | # | Column                 | SQLite type |
/// |---|------------------------|-------------|
/// | 0 | `id`                   | INTEGER PK  |
/// | 1 | `tile_matrix_set_name` | TEXT        |
/// | 2 | `datatype`             | TEXT        |
/// | 3 | `scale`                | REAL        |
/// | 4 | `offset`               | REAL        |
/// | 5 | `precision`            | REAL        |
/// | 6 | `data_null`            | REAL NULL   |
/// | 7 | `grid_cell_encoding`   | TEXT        |
/// | 8 | `uom`                  | TEXT NULL   |
/// | 9 | `field_name`           | TEXT        |
/// |10 | `quantity_definition`  | TEXT NULL   |
#[derive(Debug, Clone)]
pub struct GriddedCoverage {
    /// Name of the tile matrix set / user data table this row describes.
    pub table_name: String,
    /// Sample data type (`integer` or `float`).
    pub datatype: CoverageDatatype,
    /// Scale factor applied to raw integer values: `phys = raw * scale + offset`.
    pub scale: f64,
    /// Offset added after scaling: `phys = raw * scale + offset`.
    pub offset: f64,
    /// Minimum meaningful difference between adjacent physical values.
    pub precision: f64,
    /// Raw value that represents a missing/void sample; `None` if not specified.
    pub data_null: Option<f64>,
    /// Interpretation of the grid cell sample position.
    pub grid_cell_encoding: GridCellEncoding,
    /// Unit of measure (e.g. `"metre"`), if present.
    pub uom: Option<String>,
    /// Short name identifying the measured field (e.g. `"Height"`).
    pub field_name: String,
    /// Human-readable description of the physical quantity, if present.
    pub quantity_definition: Option<String>,
}

// ─────────────────────────────────────────────────────────────────────────────
// TileGriddedAncillary
// ─────────────────────────────────────────────────────────────────────────────

/// A parsed row from `gpkg_2d_gridded_tile_ancillary`.
///
/// Column layout (0-indexed):
///
/// | # | Column       | SQLite type |
/// |---|--------------|-------------|
/// | 0 | `id`         | INTEGER PK  |
/// | 1 | `tpudt_name` | TEXT        |
/// | 2 | `tpudt_id`   | INTEGER     |
/// | 3 | `scale`      | REAL        |
/// | 4 | `offset`     | REAL        |
/// | 5 | `min`        | REAL NULL   |
/// | 6 | `max`        | REAL NULL   |
/// | 7 | `mean`       | REAL NULL   |
/// | 8 | `std_dev`    | REAL NULL   |
///
/// `tpudt_name` is the user tile pyramid table name; `tpudt_id` is the `id`
/// of the corresponding row in that tile table.
#[derive(Debug, Clone)]
pub struct TileGriddedAncillary {
    /// Primary key of this row.
    pub id: i64,
    /// Foreign key into the tile pyramid table (`id` column of that table).
    pub tpudt_id: i64,
    /// Per-tile scale override.  Typically `1.0`.
    pub scale: f64,
    /// Per-tile offset override.  Typically `0.0`.
    pub offset: f64,
    /// Minimum physical value in this tile (optional).
    pub min: Option<f64>,
    /// Maximum physical value in this tile (optional).
    pub max: Option<f64>,
    /// Mean physical value in this tile (optional).
    pub mean: Option<f64>,
    /// Standard deviation of physical values in this tile (optional).
    pub std_dev: Option<f64>,
}

// ─────────────────────────────────────────────────────────────────────────────
// Cell-value coercions (module-local helpers)
// ─────────────────────────────────────────────────────────────────────────────

use crate::btree::CellValue;

/// Coerce a [`CellValue`] to `f64`, returning `0.0` for non-numeric types.
fn cell_to_f64(v: &CellValue) -> f64 {
    match v {
        CellValue::Float(f) => *f,
        CellValue::Integer(i) => *i as f64,
        _ => 0.0,
    }
}

/// Coerce a [`CellValue`] to `Option<f64>`, returning `None` for SQL NULL.
fn cell_to_optional_f64(v: &CellValue) -> Option<f64> {
    match v {
        CellValue::Null => None,
        CellValue::Float(f) => Some(*f),
        CellValue::Integer(i) => Some(*i as f64),
        _ => None,
    }
}

/// Coerce a [`CellValue`] to a `String`, returning an empty string for NULL.
fn cell_to_string(v: &CellValue) -> String {
    match v {
        CellValue::Text(s) => s.clone(),
        CellValue::Integer(i) => i.to_string(),
        CellValue::Float(f) => f.to_string(),
        CellValue::Blob(b) => String::from_utf8_lossy(b).into_owned(),
        CellValue::Null => String::new(),
    }
}

/// Coerce a [`CellValue`] to `Option<String>`, returning `None` for SQL NULL.
fn cell_to_optional_string(v: &CellValue) -> Option<String> {
    match v {
        CellValue::Null => None,
        CellValue::Text(s) if s.is_empty() => None,
        other => Some(cell_to_string(other)),
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// load_gridded_coverages
// ─────────────────────────────────────────────────────────────────────────────

/// Read all rows from `gpkg_2d_gridded_coverage_ancillary`.
///
/// Returns `Ok(vec![])` when the table is not present in the GeoPackage
/// (the extension is optional).  Returns a descriptive error for malformed
/// B-tree data.
///
/// # Errors
/// Propagates any B-tree parse error from the underlying SQLite reader.
pub fn load_gridded_coverages(reader: &GeoPackage) -> Result<Vec<GriddedCoverage>, GpkgError> {
    let table_name = "gpkg_2d_gridded_coverage_ancillary";
    let rows = match reader.scan_table_by_name(table_name)? {
        Some(r) => r,
        None => return Ok(Vec::new()),
    };

    let mut out = Vec::with_capacity(rows.len());
    for (_rowid, values) in rows {
        // Minimum 11 columns expected (id + 10 data columns).
        if values.len() < 11 {
            continue;
        }

        // col 0: id (INTEGER) — skipped (not in the public struct)
        // col 1: tile_matrix_set_name (TEXT)
        let tbl = cell_to_string(&values[1]);
        // col 2: datatype (TEXT)
        let datatype_str = cell_to_string(&values[2]);
        let datatype = datatype_str.parse::<CoverageDatatype>()?;
        // col 3: scale (REAL)
        let scale = cell_to_f64(&values[3]);
        // col 4: offset (REAL)
        let offset = cell_to_f64(&values[4]);
        // col 5: precision (REAL)
        let precision = cell_to_f64(&values[5]);
        // col 6: data_null (REAL NULL)
        let data_null = cell_to_optional_f64(&values[6]);
        // col 7: grid_cell_encoding (TEXT)
        let encoding_str = cell_to_string(&values[7]);
        // GridCellEncoding::FromStr is infallible (returns Grid for unknown)
        let grid_cell_encoding = encoding_str
            .parse::<GridCellEncoding>()
            .unwrap_or(GridCellEncoding::Grid);
        // col 8: uom (TEXT NULL)
        let uom = cell_to_optional_string(&values[8]);
        // col 9: field_name (TEXT)
        let field_name = cell_to_string(&values[9]);
        // col 10: quantity_definition (TEXT NULL)
        let quantity_definition = cell_to_optional_string(&values[10]);

        out.push(GriddedCoverage {
            table_name: tbl,
            datatype,
            scale,
            offset,
            precision,
            data_null,
            grid_cell_encoding,
            uom,
            field_name,
            quantity_definition,
        });
    }

    Ok(out)
}

// ─────────────────────────────────────────────────────────────────────────────
// load_gridded_tile_ancillary
// ─────────────────────────────────────────────────────────────────────────────

/// Read rows from `gpkg_2d_gridded_tile_ancillary` for a specific tile table.
///
/// Only rows where `tpudt_name == table_name` are returned; all other rows
/// are filtered out.  Returns `Ok(vec![])` when the ancillary table is absent.
///
/// # Errors
/// Propagates any B-tree parse error from the underlying SQLite reader.
pub fn load_gridded_tile_ancillary(
    reader: &GeoPackage,
    table_name: &str,
) -> Result<Vec<TileGriddedAncillary>, GpkgError> {
    let sys_table = "gpkg_2d_gridded_tile_ancillary";
    let rows = match reader.scan_table_by_name(sys_table)? {
        Some(r) => r,
        None => return Ok(Vec::new()),
    };

    let mut out = Vec::new();
    for (_rowid, values) in rows {
        // Minimum 9 columns expected:
        // id, tpudt_name, tpudt_id, scale, offset, min, max, mean, std_dev
        if values.len() < 9 {
            continue;
        }

        // col 1: tpudt_name — filter by table_name
        let tpudt_name = cell_to_string(&values[1]);
        if tpudt_name != table_name {
            continue;
        }

        // col 0: id (INTEGER PK)
        let id = cell_to_i64(&values[0]);
        // col 2: tpudt_id (INTEGER FK into the tile table)
        let tpudt_id = cell_to_i64(&values[2]);
        // col 3: scale (REAL)
        let scale = cell_to_f64(&values[3]);
        // col 4: offset (REAL)
        let offset = cell_to_f64(&values[4]);
        // col 5: min (REAL NULL)
        let min = cell_to_optional_f64(&values[5]);
        // col 6: max (REAL NULL)
        let max = cell_to_optional_f64(&values[6]);
        // col 7: mean (REAL NULL)
        let mean = cell_to_optional_f64(&values[7]);
        // col 8: std_dev (REAL NULL)
        let std_dev = cell_to_optional_f64(&values[8]);

        out.push(TileGriddedAncillary {
            id,
            tpudt_id,
            scale,
            offset,
            min,
            max,
            mean,
            std_dev,
        });
    }

    Ok(out)
}

// ─────────────────────────────────────────────────────────────────────────────
// unscale_value
// ─────────────────────────────────────────────────────────────────────────────

/// Convert a raw sample value to its physical value using scale and offset.
///
/// The conversion formula is:
/// `phys = raw * effective_scale + effective_offset`
///
/// Scale/offset priority:
/// - If `tile_ancillary` is `Some`, use its `scale` and `offset`.
/// - Otherwise use `coverage.scale` and `coverage.offset`.
///
/// Null-data handling: if `coverage.data_null` is `Some(n)` and `raw == n`,
/// the function returns [`f64::NAN`] (the sample is void/missing).
pub fn unscale_value(
    raw: f64,
    coverage: &GriddedCoverage,
    tile_ancillary: Option<&TileGriddedAncillary>,
) -> f64 {
    // Null-data check before scaling — compare with coverage data_null.
    if let Some(null_val) = coverage.data_null {
        // IEEE equality is intentional: the raw value must exactly match the
        // sentinel defined in the coverage metadata.
        #[allow(clippy::float_cmp)]
        if raw == null_val {
            return f64::NAN;
        }
    }

    // Determine effective scale/offset: tile ancillary wins when present.
    let (effective_scale, effective_offset) = match tile_ancillary {
        Some(ta) => (ta.scale, ta.offset),
        None => (coverage.scale, coverage.offset),
    };

    raw * effective_scale + effective_offset
}

// ─────────────────────────────────────────────────────────────────────────────
// unscale_tile_buffer_u16
// ─────────────────────────────────────────────────────────────────────────────

/// Apply `unscale_value` to every sample in a u16 tile buffer.
///
/// Typical GeoPackage elevation tiles store 16-bit unsigned integers packed in
/// little-endian byte order (after PNG/TIFF decoding); this function accepts the
/// already-decoded integer slice and returns the physical float values.
///
/// The returned `Vec<f64>` has the same length as `raw_buf`.
pub fn unscale_tile_buffer_u16(
    raw_buf: &[u16],
    coverage: &GriddedCoverage,
    tile_ancillary: Option<&TileGriddedAncillary>,
) -> Vec<f64> {
    raw_buf
        .iter()
        .map(|&sample| unscale_value(sample as f64, coverage, tile_ancillary))
        .collect()
}

// ─────────────────────────────────────────────────────────────────────────────
// unscale_tile_buffer_i16
// ─────────────────────────────────────────────────────────────────────────────

/// Apply `unscale_value` to every sample in an i16 tile buffer.
///
/// Signed 16-bit elevation data is common in DTM datasets where terrain can
/// dip below the reference datum (negative elevations).  The conversion is
/// identical to [`unscale_tile_buffer_u16`] except the input type is `i16`.
///
/// The returned `Vec<f64>` has the same length as `raw_buf`.
pub fn unscale_tile_buffer_i16(
    raw_buf: &[i16],
    coverage: &GriddedCoverage,
    tile_ancillary: Option<&TileGriddedAncillary>,
) -> Vec<f64> {
    raw_buf
        .iter()
        .map(|&sample| unscale_value(sample as f64, coverage, tile_ancillary))
        .collect()
}

// ─────────────────────────────────────────────────────────────────────────────
// Unit tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
#[allow(clippy::float_cmp, clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;

    fn make_coverage(scale: f64, offset: f64, data_null: Option<f64>) -> GriddedCoverage {
        GriddedCoverage {
            table_name: "dem".to_string(),
            datatype: CoverageDatatype::Integer,
            scale,
            offset,
            precision: 1.0,
            data_null,
            grid_cell_encoding: GridCellEncoding::Grid,
            uom: None,
            field_name: "Height".to_string(),
            quantity_definition: None,
        }
    }

    fn make_tile_ancillary(scale: f64, offset: f64) -> TileGriddedAncillary {
        TileGriddedAncillary {
            id: 1,
            tpudt_id: 42,
            scale,
            offset,
            min: None,
            max: None,
            mean: None,
            std_dev: None,
        }
    }

    // ── CoverageDatatype ─────────────────────────────────────────────────────

    #[test]
    fn datatype_from_str_integer() {
        assert_eq!(
            "integer".parse::<CoverageDatatype>().unwrap(),
            CoverageDatatype::Integer
        );
    }

    #[test]
    fn datatype_from_str_float() {
        assert_eq!(
            "float".parse::<CoverageDatatype>().unwrap(),
            CoverageDatatype::Float
        );
    }

    #[test]
    fn datatype_from_str_invalid() {
        let err = "raster".parse::<CoverageDatatype>().unwrap_err();
        assert!(
            matches!(err, GpkgError::InvalidCoverageDatatype(ref s) if s == "raster"),
            "unexpected error: {err:?}"
        );
    }

    #[test]
    fn datatype_as_str_roundtrip() {
        assert_eq!(CoverageDatatype::Integer.as_str(), "integer");
        assert_eq!(CoverageDatatype::Float.as_str(), "float");
    }

    // ── GridCellEncoding ─────────────────────────────────────────────────────

    #[test]
    fn grid_cell_encoding_parses_all_three() {
        assert_eq!(
            "grid-value-is-center".parse::<GridCellEncoding>().unwrap(),
            GridCellEncoding::Grid
        );
        assert_eq!(
            "grid-value-is-area".parse::<GridCellEncoding>().unwrap(),
            GridCellEncoding::PixelIsArea
        );
        assert_eq!(
            "grid-value-is-corner".parse::<GridCellEncoding>().unwrap(),
            GridCellEncoding::PixelIsPoint
        );
    }

    #[test]
    fn grid_cell_encoding_unknown_defaults_to_grid() {
        // OGC §F.7: lenient parse — unknown strings → Grid
        assert_eq!(
            "unknown-encoding".parse::<GridCellEncoding>().unwrap(),
            GridCellEncoding::Grid
        );
    }

    // ── unscale_value ────────────────────────────────────────────────────────

    #[test]
    fn unscale_value_identity_passthrough() {
        let cov = make_coverage(1.0, 0.0, None);
        assert_eq!(unscale_value(42.0, &cov, None), 42.0);
    }

    #[test]
    fn unscale_value_scale_and_offset_applied() {
        // scale=0.1, offset=-100.0: raw=1000.0 → phys = 1000*0.1 + (-100) = 0.0
        let cov = make_coverage(0.1, -100.0, None);
        let phys = unscale_value(1000.0, &cov, None);
        assert!((phys - 0.0).abs() < 1e-10, "expected 0.0, got {phys}");
    }

    #[test]
    fn unscale_value_tile_ancillary_overrides_coverage() {
        // coverage scale=1.0 offset=0.0, tile scale=2.0 offset=5.0
        // raw=10.0 → tile wins → phys = 10*2 + 5 = 25.0
        let cov = make_coverage(1.0, 0.0, None);
        let ta = make_tile_ancillary(2.0, 5.0);
        let phys = unscale_value(10.0, &cov, Some(&ta));
        assert!((phys - 25.0).abs() < 1e-10, "expected 25.0, got {phys}");
    }

    #[test]
    fn unscale_value_data_null_returns_nan() {
        let cov = make_coverage(1.0, 0.0, Some(0.0));
        let result = unscale_value(0.0, &cov, None);
        assert!(result.is_nan(), "expected NAN, got {result}");
    }

    #[test]
    fn unscale_value_non_null_not_nan() {
        let cov = make_coverage(1.0, 0.0, Some(0.0));
        let result = unscale_value(1.0, &cov, None);
        assert!(!result.is_nan(), "should not be NAN for non-null value");
        assert!((result - 1.0).abs() < 1e-10);
    }

    // ── unscale_tile_buffer_u16 ──────────────────────────────────────────────

    #[test]
    fn unscale_buffer_u16_identity() {
        let cov = make_coverage(1.0, 0.0, None);
        let raw = [0u16, 1, 65535];
        let out = unscale_tile_buffer_u16(&raw, &cov, None);
        assert_eq!(out.len(), 3);
        assert!((out[0] - 0.0).abs() < 1e-10);
        assert!((out[1] - 1.0).abs() < 1e-10);
        assert!((out[2] - 65535.0).abs() < 1e-10);
    }

    #[test]
    fn unscale_buffer_u16_with_scale() {
        // scale=0.01, offset=0.0: [100, 200] → [1.0, 2.0]
        let cov = make_coverage(0.01, 0.0, None);
        let raw = [100u16, 200u16];
        let out = unscale_tile_buffer_u16(&raw, &cov, None);
        assert!((out[0] - 1.0).abs() < 1e-10, "got {}", out[0]);
        assert!((out[1] - 2.0).abs() < 1e-10, "got {}", out[1]);
    }

    // ── unscale_tile_buffer_i16 ──────────────────────────────────────────────

    #[test]
    fn unscale_buffer_i16_negative_elevations() {
        let cov = make_coverage(1.0, 0.0, None);
        let raw = [-100i16, 0i16, 100i16];
        let out = unscale_tile_buffer_i16(&raw, &cov, None);
        assert_eq!(out.len(), 3);
        assert!((out[0] - (-100.0)).abs() < 1e-10, "got {}", out[0]);
        assert!((out[1] - 0.0).abs() < 1e-10, "got {}", out[1]);
        assert!((out[2] - 100.0).abs() < 1e-10, "got {}", out[2]);
    }

    #[test]
    fn unscale_buffer_i16_with_scale_and_offset() {
        // scale=0.5, offset=10.0: [-2, 0, 4] → [9.0, 10.0, 12.0]
        let cov = make_coverage(0.5, 10.0, None);
        let raw = [-2i16, 0i16, 4i16];
        let out = unscale_tile_buffer_i16(&raw, &cov, None);
        assert!((out[0] - 9.0).abs() < 1e-10, "got {}", out[0]);
        assert!((out[1] - 10.0).abs() < 1e-10, "got {}", out[1]);
        assert!((out[2] - 12.0).abs() < 1e-10, "got {}", out[2]);
    }
}