Skip to main content

sidereon_core/
terrain_store.rs

1//! Memory-mappable terrain tile store with an explicit vertical datum contract.
2//!
3//! The store is a single canonical container: a fixed header, a sorted tile
4//! index, and one aligned `i16` posting payload per tile. Payloads are decoded
5//! DTED posting values in orthometric metres, stored longitude-major with
6//! latitude as the inner index. The reader keeps the input bytes in place and
7//! indexes posting bytes directly, so an application can pass an mmap-backed
8//! slice through [`MmapTerrain::from_bytes`].
9//!
10//! DTED and SRTM postings are orthometric heights, `H`, above the EGM96 mean sea
11//! level geoid. Ellipsoidal height conversion is an explicit `h = H + N` step
12//! using [`TerrainGeoidModel`].
13
14use std::borrow::Cow;
15use std::collections::{HashMap, HashSet};
16use std::fs;
17use std::path::{Path, PathBuf};
18
19use crate::geoid::{egm96_undulation, GeoidError, GeoidGrid};
20use crate::terrain::{
21    self, terrain_grid_candidates, validate_lookup_coordinates, DtedInterpolation,
22    DtedLookupOptions, DtedTile,
23};
24use crate::{Error, Result};
25
26const STORE_MAGIC: &[u8; 8] = b"TMMAP001";
27const STORE_VERSION: u16 = 1;
28const STORE_ALIGNMENT: usize = 4096;
29const STORE_HEADER_LEN: usize = 64;
30const STORE_INDEX_RECORD_LEN: usize = 80;
31const HEADER_VERSION_OFFSET: usize = 8;
32const HEADER_DATUM_OFFSET: usize = 10;
33const HEADER_TILE_COUNT_OFFSET: usize = 12;
34const HEADER_INDEX_OFFSET_OFFSET: usize = 16;
35const HEADER_DATA_OFFSET_OFFSET: usize = 24;
36const HEADER_TOTAL_LEN_OFFSET: usize = 32;
37const INDEX_LAT_OFFSET: usize = 0;
38const INDEX_LON_OFFSET: usize = 4;
39const INDEX_LON_COUNT_OFFSET: usize = 8;
40const INDEX_LAT_COUNT_OFFSET: usize = 12;
41const INDEX_DATA_OFFSET_OFFSET: usize = 16;
42const INDEX_DATA_LEN_OFFSET: usize = 24;
43const INDEX_CHECKSUM_OFFSET: usize = 32;
44const INDEX_MIN_LAT_OFFSET: usize = 40;
45const INDEX_MIN_LON_OFFSET: usize = 48;
46const INDEX_MAX_LAT_OFFSET: usize = 56;
47const INDEX_MAX_LON_OFFSET: usize = 64;
48const INDEX_DATUM_OFFSET: usize = 72;
49const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
50const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
51const EGM96_DAC_REMEDIATION: &str =
52    "obtain the public NGA EGM96 15-arcminute WW15MGH.DAC file and load it with Egm96FifteenMinuteGeoid::from_ww15mgh_dac_path or Egm96FifteenMinuteGeoid::from_ww15mgh_dac_bytes";
53
54/// Vertical datum carried by terrain store tile index records.
55#[derive(Clone, Copy, Debug, PartialEq, Eq)]
56pub enum VerticalDatum {
57    /// Orthometric height `H` in metres above the EGM96 mean sea level geoid.
58    Egm96MslOrthometric,
59}
60
61impl VerticalDatum {
62    fn tag(self) -> u8 {
63        match self {
64            Self::Egm96MslOrthometric => 1,
65        }
66    }
67
68    fn from_tag(tag: u8) -> core::result::Result<Self, TerrainStoreError> {
69        match tag {
70            1 => Ok(Self::Egm96MslOrthometric),
71            other => Err(TerrainStoreError::UnsupportedDatum { tag: other }),
72        }
73    }
74}
75
76/// Orthometric height `H` in metres above the EGM96 mean sea level geoid.
77///
78/// DTED/SRTM terrain postings use this datum. Convert to ellipsoidal height
79/// only through [`Self::to_ellipsoidal_height_deg`] or
80/// [`Self::to_ellipsoidal_height_rad`], which require a pinned geoid tier.
81#[derive(Clone, Copy, Debug, PartialEq)]
82pub struct OrthometricHeightM {
83    /// Orthometric height `H` in metres.
84    pub value_m: f64,
85}
86
87impl OrthometricHeightM {
88    /// Build an orthometric height `H` in metres.
89    #[must_use]
90    pub const fn new(value_m: f64) -> Self {
91        Self { value_m }
92    }
93
94    /// Return the orthometric height `H` in metres.
95    #[must_use]
96    pub const fn metres(self) -> f64 {
97        self.value_m
98    }
99
100    /// Convert this orthometric height to ellipsoidal height `h = H + N`.
101    ///
102    /// Inputs are geodetic `(latitude_deg, longitude_deg)`, matching the geoid
103    /// module's axis order. Terrain lookup APIs use `(longitude_deg,
104    /// latitude_deg)`, so call sites should pass the axes deliberately.
105    ///
106    /// [`TerrainGeoidModel::Egm96OneDegree`] uses the embedded EGM96 1-degree
107    /// grid. It agrees with the full EGM96 15-arcminute grid to about 0.4 m RMS,
108    /// so byte-identical terrain heights do not imply byte-identical
109    /// ellipsoidal heights across geoid tiers.
110    pub fn to_ellipsoidal_height_deg(
111        self,
112        latitude_deg: f64,
113        longitude_deg: f64,
114        geoid: TerrainGeoidModel<'_>,
115    ) -> core::result::Result<EllipsoidalHeightM, TerrainDatumError> {
116        Ok(EllipsoidalHeightM::new(
117            self.value_m + geoid.undulation_deg(latitude_deg, longitude_deg),
118        ))
119    }
120
121    /// Convert this orthometric height to ellipsoidal height `h = H + N`.
122    ///
123    /// Inputs are geodetic `(latitude_rad, longitude_rad)`, matching the geoid
124    /// module's axis order. [`TerrainGeoidModel::Egm96OneDegree`] uses the
125    /// embedded EGM96 1-degree grid. It agrees with the full EGM96
126    /// 15-arcminute grid to about 0.4 m RMS, so byte-identical terrain heights
127    /// do not imply byte-identical ellipsoidal heights across geoid tiers.
128    pub fn to_ellipsoidal_height_rad(
129        self,
130        latitude_rad: f64,
131        longitude_rad: f64,
132        geoid: TerrainGeoidModel<'_>,
133    ) -> core::result::Result<EllipsoidalHeightM, TerrainDatumError> {
134        Ok(EllipsoidalHeightM::new(
135            self.value_m + geoid.undulation_rad(latitude_rad, longitude_rad),
136        ))
137    }
138}
139
140/// Ellipsoidal height `h` in metres above the WGS84 reference ellipsoid.
141#[derive(Clone, Copy, Debug, PartialEq)]
142pub struct EllipsoidalHeightM {
143    /// Ellipsoidal height `h` in metres.
144    pub value_m: f64,
145}
146
147impl EllipsoidalHeightM {
148    /// Build an ellipsoidal height `h` in metres.
149    #[must_use]
150    pub const fn new(value_m: f64) -> Self {
151        Self { value_m }
152    }
153
154    /// Return the ellipsoidal height `h` in metres.
155    #[must_use]
156    pub const fn metres(self) -> f64 {
157        self.value_m
158    }
159}
160
161/// Loaded EGM96 15-arcminute geoid grid for explicit terrain datum conversion.
162///
163/// This type never falls back to the embedded 1-degree grid. A missing
164/// `WW15MGH.DAC` file returns [`TerrainDatumError::MissingEgm96Dac`].
165#[derive(Clone, Debug, PartialEq)]
166pub struct Egm96FifteenMinuteGeoid {
167    grid: GeoidGrid,
168}
169
170impl Egm96FifteenMinuteGeoid {
171    /// Load `WW15MGH.DAC` bytes as an EGM96 15-arcminute geoid grid.
172    pub fn from_ww15mgh_dac_bytes(bytes: &[u8]) -> core::result::Result<Self, TerrainDatumError> {
173        let grid = GeoidGrid::from_egm96_dac(bytes).map_err(TerrainDatumError::Geoid)?;
174        Ok(Self { grid })
175    }
176
177    /// Read and load `WW15MGH.DAC` from disk as an EGM96 15-arcminute geoid
178    /// grid.
179    ///
180    /// If the file is absent, this returns
181    /// [`TerrainDatumError::MissingEgm96Dac`] with a remediation string naming
182    /// the required grid and loader. It does not fall back to the embedded
183    /// EGM96 1-degree grid.
184    pub fn from_ww15mgh_dac_path(
185        path: impl AsRef<Path>,
186    ) -> core::result::Result<Self, TerrainDatumError> {
187        let path = path.as_ref();
188        let bytes = match fs::read(path) {
189            Ok(bytes) => bytes,
190            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
191                return Err(TerrainDatumError::MissingEgm96Dac {
192                    path: path.to_path_buf(),
193                    remediation: EGM96_DAC_REMEDIATION,
194                });
195            }
196            Err(err) => {
197                return Err(TerrainDatumError::Io {
198                    path: path.to_path_buf(),
199                    message: err.to_string(),
200                });
201            }
202        };
203        Self::from_ww15mgh_dac_bytes(&bytes)
204    }
205
206    /// Borrow the loaded EGM96 15-arcminute geoid grid.
207    #[must_use]
208    pub const fn grid(&self) -> &GeoidGrid {
209        &self.grid
210    }
211}
212
213/// Geoid tier used to convert terrain orthometric height `H` to ellipsoidal
214/// height `h`.
215#[derive(Clone, Copy, Debug)]
216pub enum TerrainGeoidModel<'a> {
217    /// Embedded EGM96 1-degree grid, always available in-process.
218    ///
219    /// This tier agrees with the full EGM96 15-arcminute grid to about 0.4 m RMS.
220    /// It is the zero-setup path for `h = H + N` terrain conversion.
221    Egm96OneDegree,
222    /// Caller-supplied EGM96 15-arcminute `WW15MGH.DAC` grid.
223    ///
224    /// Build this with [`Egm96FifteenMinuteGeoid::from_ww15mgh_dac_path`] or
225    /// [`Egm96FifteenMinuteGeoid::from_ww15mgh_dac_bytes`]. Missing files fail
226    /// closed with [`TerrainDatumError::MissingEgm96Dac`].
227    Egm96FifteenMinute(&'a Egm96FifteenMinuteGeoid),
228}
229
230impl TerrainGeoidModel<'_> {
231    fn undulation_deg(self, latitude_deg: f64, longitude_deg: f64) -> f64 {
232        match self {
233            Self::Egm96OneDegree => {
234                egm96_undulation(latitude_deg.to_radians(), longitude_deg.to_radians())
235            }
236            Self::Egm96FifteenMinute(grid) => grid.grid.undulation_deg(latitude_deg, longitude_deg),
237        }
238    }
239
240    fn undulation_rad(self, latitude_rad: f64, longitude_rad: f64) -> f64 {
241        match self {
242            Self::Egm96OneDegree => egm96_undulation(latitude_rad, longitude_rad),
243            Self::Egm96FifteenMinute(grid) => grid.grid.undulation_rad(latitude_rad, longitude_rad),
244        }
245    }
246}
247
248/// Errors from vertical-datum conversion and optional geoid-grid loading.
249#[derive(Debug, Clone, PartialEq)]
250pub enum TerrainDatumError {
251    /// Terrain lookup failed before datum conversion.
252    Terrain(Error),
253    /// A geoid grid could not be parsed.
254    Geoid(GeoidError),
255    /// Reading a geoid grid failed for a reason other than absence.
256    Io {
257        /// Path that could not be read.
258        path: PathBuf,
259        /// I/O error text.
260        message: String,
261    },
262    /// The EGM96 15-arcminute `WW15MGH.DAC` grid was requested but is absent.
263    MissingEgm96Dac {
264        /// Path that was requested.
265        path: PathBuf,
266        /// Remediation text naming the required grid and loader.
267        remediation: &'static str,
268    },
269}
270
271impl core::fmt::Display for TerrainDatumError {
272    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
273        match self {
274            Self::Terrain(err) => write!(f, "terrain lookup failed: {err}"),
275            Self::Geoid(err) => write!(f, "geoid grid failed: {err}"),
276            Self::Io { path, message } => {
277                write!(f, "{} could not be read: {message}", path.display())
278            }
279            Self::MissingEgm96Dac { path, remediation } => {
280                write!(f, "{} is missing; {remediation}", path.display())
281            }
282        }
283    }
284}
285
286impl std::error::Error for TerrainDatumError {}
287
288impl From<Error> for TerrainDatumError {
289    fn from(value: Error) -> Self {
290        Self::Terrain(value)
291    }
292}
293
294/// Metadata for one tile index record in a memory-mappable terrain store.
295#[derive(Clone, Copy, Debug, PartialEq)]
296pub struct TerrainStoreTileIndex {
297    /// Integer latitude tile id, e.g. `36` for a tile covering `36..37` degrees.
298    pub lat_index: i32,
299    /// Integer longitude tile id, e.g. `-107` for a tile covering
300    /// `-107..-106` degrees.
301    pub lon_index: i32,
302    /// Western edge longitude in degrees.
303    pub min_longitude_deg: f64,
304    /// Southern edge latitude in degrees.
305    pub min_latitude_deg: f64,
306    /// Eastern edge longitude in degrees.
307    pub max_longitude_deg: f64,
308    /// Northern edge latitude in degrees.
309    pub max_latitude_deg: f64,
310    /// Number of longitude postings.
311    pub lon_count: u32,
312    /// Number of latitude postings.
313    pub lat_count: u32,
314    /// Byte offset of this tile's posting payload in the store.
315    pub data_offset: u64,
316    /// Byte length of this tile's posting payload in the store.
317    pub data_len: u64,
318    /// FNV-1a checksum of this tile's posting payload bytes.
319    pub checksum64: u64,
320    /// Vertical datum for the tile's posting payload.
321    pub vertical_datum: VerticalDatum,
322}
323
324/// Integer terrain tile id used by DTED and terrain-store accessors.
325#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
326pub struct TerrainTileId {
327    /// Integer latitude tile id, e.g. `36` for a tile covering `36..37` degrees.
328    pub lat_index: i32,
329    /// Integer longitude tile id, e.g. `-107` for a tile covering
330    /// `-107..-106` degrees.
331    pub lon_index: i32,
332}
333
334impl TerrainTileId {
335    /// Build an integer terrain tile id.
336    #[must_use]
337    pub const fn new(lat_index: i32, lon_index: i32) -> Self {
338        Self {
339            lat_index,
340            lon_index,
341        }
342    }
343}
344
345/// One explicit DTED tile source for list-based terrain-store conversion.
346#[derive(Clone, Debug, PartialEq, Eq)]
347pub struct DtedTileListEntry {
348    /// Expected integer tile id for `path`.
349    pub tile_id: TerrainTileId,
350    /// Path to the DTED `.dt2` tile bytes.
351    pub path: PathBuf,
352}
353
354impl DtedTileListEntry {
355    /// Build a tile-list entry from a tile id and DTED path.
356    #[must_use]
357    pub fn new(tile_id: TerrainTileId, path: impl Into<PathBuf>) -> Self {
358        Self {
359            tile_id,
360            path: path.into(),
361        }
362    }
363
364    /// Build a tile-list entry from integer tile indices and a DTED path.
365    #[must_use]
366    pub fn from_indices(lat_index: i32, lon_index: i32, path: impl Into<PathBuf>) -> Self {
367        Self::new(TerrainTileId::new(lat_index, lon_index), path)
368    }
369}
370
371/// Errors from terrain store conversion, serialization, and parsing.
372#[derive(Debug, Clone, PartialEq, Eq)]
373pub enum TerrainStoreError {
374    /// File or directory I/O failed.
375    Io {
376        /// Path being accessed.
377        path: PathBuf,
378        /// I/O error text.
379        message: String,
380    },
381    /// DTED or terrain store bytes could not be parsed.
382    Parse {
383        /// Human-readable parse reason.
384        reason: String,
385    },
386    /// The terrain store version is not supported.
387    UnsupportedVersion {
388        /// Version tag found in the store header.
389        version: u16,
390    },
391    /// The terrain store datum tag is not supported.
392    UnsupportedDatum {
393        /// Datum tag found in the store header or tile index.
394        tag: u8,
395    },
396    /// Two input DTED files resolved to the same integer tile id.
397    DuplicateTile {
398        /// Latitude tile id.
399        lat_index: i32,
400        /// Longitude tile id.
401        lon_index: i32,
402    },
403    /// A list-builder entry's supplied id did not match the DTED file origin.
404    TileIdMismatch {
405        /// Path whose parsed DTED origin did not match the supplied id.
406        path: PathBuf,
407        /// Expected tile id supplied by the caller.
408        expected: TerrainTileId,
409        /// Tile id parsed from the DTED file.
410        found: TerrainTileId,
411    },
412    /// A tile payload checksum did not match its index record.
413    Checksum {
414        /// Latitude tile id.
415        lat_index: i32,
416        /// Longitude tile id.
417        lon_index: i32,
418        /// Checksum stored in the index record.
419        expected: u64,
420        /// Checksum computed from the posting payload.
421        found: u64,
422    },
423}
424
425impl core::fmt::Display for TerrainStoreError {
426    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
427        match self {
428            Self::Io { path, message } => write!(f, "{} failed: {message}", path.display()),
429            Self::Parse { reason } => write!(f, "terrain store parse error: {reason}"),
430            Self::UnsupportedVersion { version } => {
431                write!(f, "terrain store version {version} is not supported")
432            }
433            Self::UnsupportedDatum { tag } => {
434                write!(f, "terrain store vertical datum tag {tag} is not supported")
435            }
436            Self::DuplicateTile {
437                lat_index,
438                lon_index,
439            } => write!(f, "duplicate terrain tile ({lat_index},{lon_index})"),
440            Self::TileIdMismatch {
441                path,
442                expected,
443                found,
444            } => write!(
445                f,
446                "{} tile id expected ({},{}) but DTED origin is ({},{})",
447                path.display(),
448                expected.lat_index,
449                expected.lon_index,
450                found.lat_index,
451                found.lon_index
452            ),
453            Self::Checksum {
454                lat_index,
455                lon_index,
456                expected,
457                found,
458            } => write!(
459                f,
460                "terrain tile ({lat_index},{lon_index}) checksum expected {expected:#x} but found {found:#x}"
461            ),
462        }
463    }
464}
465
466impl std::error::Error for TerrainStoreError {}
467
468#[derive(Clone, Debug)]
469struct MmapTile {
470    index: TerrainStoreTileIndex,
471}
472
473impl MmapTile {
474    fn contains(&self, longitude_deg: f64, latitude_deg: f64) -> bool {
475        latitude_deg >= self.index.min_latitude_deg
476            && latitude_deg <= self.index.max_latitude_deg
477            && longitude_deg >= self.index.min_longitude_deg
478            && longitude_deg <= self.index.max_longitude_deg
479    }
480
481    fn get_elevation(&self, bytes: &[u8], longitude_deg: f64, latitude_deg: f64) -> Result<i16> {
482        if !self.contains(longitude_deg, latitude_deg) {
483            return Err(Error::Parse(format!(
484                "point ({longitude_deg},{latitude_deg}) is outside terrain store tile ({},{})",
485                self.index.min_longitude_deg, self.index.min_latitude_deg
486            )));
487        }
488
489        let lat_count = self.index.lat_count as usize;
490        let lon_count = self.index.lon_count as usize;
491        let latitude_index = terrain::py_round_to_usize(
492            (latitude_deg - self.index.min_latitude_deg) * (lat_count - 1) as f64,
493        )
494        .map_err(Error::Parse)?;
495        let longitude_index = terrain::py_round_to_usize(
496            (longitude_deg - self.index.min_longitude_deg) * (lon_count - 1) as f64,
497        )
498        .map_err(Error::Parse)?;
499        if latitude_index >= lat_count || longitude_index >= lon_count {
500            return Err(Error::Parse(format!(
501                "posting index out of bounds lon={longitude_index} lat={latitude_index}"
502            )));
503        }
504
505        let sample_start =
506            self.index.data_offset as usize + 2 * (longitude_index * lat_count + latitude_index);
507        Ok(i16::from_le_bytes([
508            bytes[sample_start],
509            bytes[sample_start + 1],
510        ]))
511    }
512}
513
514/// Memory-mappable terrain reader backed by a terrain store byte span.
515///
516/// Scalar and batch terrain lookups return orthometric metres, `H`, above the
517/// EGM96 mean sea level geoid. Use [`Self::ellipsoidal_height_m`] or
518/// [`OrthometricHeightM::to_ellipsoidal_height_deg`] for the explicit
519/// `h = H + N` conversion to WGS84 ellipsoidal height.
520#[derive(Clone, Debug)]
521pub struct MmapTerrain<'a> {
522    bytes: Cow<'a, [u8]>,
523    tiles: Vec<MmapTile>,
524    by_grid: HashMap<(i32, i32), usize>,
525    tile_index: Vec<TerrainStoreTileIndex>,
526    tile_ids: Vec<TerrainTileId>,
527    vertical_datum: VerticalDatum,
528}
529
530impl MmapTerrain<'static> {
531    /// Parse an owned terrain store byte vector.
532    pub fn from_vec(bytes: Vec<u8>) -> core::result::Result<Self, TerrainStoreError> {
533        Self::from_cow(Cow::Owned(bytes))
534    }
535
536    /// Read and parse a terrain store file.
537    ///
538    /// This reads the file into memory. Applications that already manage an mmap
539    /// should pass the mmap slice to [`MmapTerrain::from_bytes`] to avoid the
540    /// file read copy.
541    pub fn from_path(path: impl AsRef<Path>) -> core::result::Result<Self, TerrainStoreError> {
542        let path = path.as_ref();
543        let bytes = fs::read(path).map_err(|err| TerrainStoreError::Io {
544            path: path.to_path_buf(),
545            message: err.to_string(),
546        })?;
547        Self::from_vec(bytes)
548    }
549}
550
551impl<'a> MmapTerrain<'a> {
552    /// Parse a borrowed terrain store byte span.
553    ///
554    /// The reader keeps the byte span in place and indexes posting payloads by
555    /// offset. Passing an mmap-backed slice gives a zero-copy reader.
556    pub fn from_bytes(bytes: &'a [u8]) -> core::result::Result<Self, TerrainStoreError> {
557        Self::from_cow(Cow::Borrowed(bytes))
558    }
559
560    fn from_cow(bytes: Cow<'a, [u8]>) -> core::result::Result<Self, TerrainStoreError> {
561        let parsed = parse_store(bytes.as_ref())?;
562        Ok(Self {
563            bytes,
564            tiles: parsed.tiles,
565            by_grid: parsed.by_grid,
566            tile_index: parsed.tile_index,
567            tile_ids: parsed.tile_ids,
568            vertical_datum: parsed.vertical_datum,
569        })
570    }
571
572    /// Borrow the original terrain store bytes.
573    #[must_use]
574    pub fn as_bytes(&self) -> &[u8] {
575        self.bytes.as_ref()
576    }
577
578    /// Return the store's file-level vertical datum.
579    #[must_use]
580    pub const fn vertical_datum(&self) -> VerticalDatum {
581        self.vertical_datum
582    }
583
584    /// Borrow the parsed tile index records.
585    #[must_use]
586    pub fn tile_index(&self) -> &[TerrainStoreTileIndex] {
587        &self.tile_index
588    }
589
590    /// Return the number of tiles present in this terrain store.
591    #[must_use]
592    pub fn tile_count(&self) -> usize {
593        self.tile_ids.len()
594    }
595
596    /// Borrow the sorted integer tile ids present in this terrain store.
597    #[must_use]
598    pub fn tile_ids(&self) -> &[TerrainTileId] {
599        &self.tile_ids
600    }
601
602    /// Return an FNV-1a checksum of the full terrain store byte span.
603    #[must_use]
604    pub fn checksum64(&self) -> u64 {
605        terrain_store_checksum64(self.bytes.as_ref())
606    }
607
608    /// Re-serialize this parsed terrain store into canonical bytes.
609    ///
610    /// A store accepted by [`Self::from_bytes`] is already canonical, so this
611    /// returns bytes identical to [`Self::as_bytes`].
612    #[must_use]
613    pub fn to_bytes(&self) -> Vec<u8> {
614        let pending = self
615            .tiles
616            .iter()
617            .map(|tile| PendingTile {
618                lat_index: tile.index.lat_index,
619                lon_index: tile.index.lon_index,
620                min_latitude_deg: tile.index.min_latitude_deg,
621                min_longitude_deg: tile.index.min_longitude_deg,
622                max_latitude_deg: tile.index.max_latitude_deg,
623                max_longitude_deg: tile.index.max_longitude_deg,
624                lon_count: tile.index.lon_count,
625                lat_count: tile.index.lat_count,
626                data: self.tile_payload(tile).to_vec(),
627                vertical_datum: tile.index.vertical_datum,
628            })
629            .collect();
630        build_store(pending).expect("parsed terrain store can be reserialized")
631    }
632
633    /// Return the bilinearly interpolated orthometric height `H` in metres at a
634    /// longitude-first geodetic position in degrees.
635    pub fn height_m(&mut self, longitude_deg: f64, latitude_deg: f64) -> Result<f64> {
636        self.height_m_with_options(longitude_deg, latitude_deg, DtedLookupOptions::default())
637    }
638
639    /// Return the orthometric height `H` in metres at a longitude-first geodetic
640    /// position in degrees using explicit lookup options.
641    pub fn height_m_with_options(
642        &mut self,
643        longitude_deg: f64,
644        latitude_deg: f64,
645        options: DtedLookupOptions,
646    ) -> Result<f64> {
647        self.orthometric_height_m_with_options(longitude_deg, latitude_deg, options)
648            .map(OrthometricHeightM::metres)
649    }
650
651    /// Return the bilinearly interpolated orthometric height `H` in metres as a
652    /// typed value at a longitude-first geodetic position in degrees.
653    pub fn orthometric_height_m(
654        &self,
655        longitude_deg: f64,
656        latitude_deg: f64,
657    ) -> Result<OrthometricHeightM> {
658        self.orthometric_height_m_with_options(
659            longitude_deg,
660            latitude_deg,
661            DtedLookupOptions::default(),
662        )
663    }
664
665    /// Return the orthometric height `H` in metres as a typed value at a
666    /// longitude-first geodetic position in degrees using explicit lookup
667    /// options.
668    pub fn orthometric_height_m_with_options(
669        &self,
670        longitude_deg: f64,
671        latitude_deg: f64,
672        options: DtedLookupOptions,
673    ) -> Result<OrthometricHeightM> {
674        validate_lookup_coordinates(longitude_deg, latitude_deg)?;
675        let Some(tile_idx) = self.resolve_grid(longitude_deg, latitude_deg) else {
676            return Err(missing_terrain_tile(longitude_deg, latitude_deg));
677        };
678        height_from_tile(
679            self.bytes.as_ref(),
680            &self.tiles[tile_idx],
681            longitude_deg,
682            latitude_deg,
683            options,
684        )
685        .map(OrthometricHeightM::new)
686    }
687
688    /// Evaluate `(longitude_deg, latitude_deg)` points in order as orthometric
689    /// heights `H` in metres.
690    ///
691    /// The tuple order is longitude-first, matching [`Self::height_m`]. Each
692    /// output element is independent, so an invalid point or parse failure is
693    /// returned only for that element.
694    pub fn height_batch(
695        &mut self,
696        points: &[(f64, f64)],
697        options: DtedLookupOptions,
698    ) -> Vec<Result<f64>> {
699        self.orthometric_height_batch(points, options)
700            .into_iter()
701            .map(|result| result.map(OrthometricHeightM::metres))
702            .collect()
703    }
704
705    /// Evaluate `(longitude_deg, latitude_deg)` points in order as typed
706    /// orthometric heights `H` in metres.
707    ///
708    /// The tuple order is longitude-first. Each output element is independent,
709    /// so an invalid point or parse failure is returned only for that element.
710    pub fn orthometric_height_batch(
711        &self,
712        points: &[(f64, f64)],
713        options: DtedLookupOptions,
714    ) -> Vec<Result<OrthometricHeightM>> {
715        let mut out = Vec::with_capacity(points.len());
716        let mut current = None;
717
718        for &(longitude_deg, latitude_deg) in points {
719            if let Err(err) = validate_lookup_coordinates(longitude_deg, latitude_deg) {
720                out.push(Err(err));
721                continue;
722            }
723
724            let primary_grid = terrain::terrain_grid(longitude_deg, latitude_deg);
725            if current == Some(primary_grid) {
726                if let Some(&tile_idx) = self.by_grid.get(&primary_grid) {
727                    let tile = &self.tiles[tile_idx];
728                    if tile.contains(longitude_deg, latitude_deg) {
729                        out.push(
730                            height_from_tile(
731                                self.bytes.as_ref(),
732                                tile,
733                                longitude_deg,
734                                latitude_deg,
735                                options,
736                            )
737                            .map(OrthometricHeightM::new),
738                        );
739                        continue;
740                    }
741                }
742            }
743
744            match self.resolve_grid(longitude_deg, latitude_deg) {
745                Some(tile_idx) => {
746                    let tile = &self.tiles[tile_idx];
747                    current = Some((tile.index.lat_index, tile.index.lon_index));
748                    out.push(
749                        height_from_tile(
750                            self.bytes.as_ref(),
751                            tile,
752                            longitude_deg,
753                            latitude_deg,
754                            options,
755                        )
756                        .map(OrthometricHeightM::new),
757                    );
758                }
759                None => {
760                    current = None;
761                    out.push(Err(missing_terrain_tile(longitude_deg, latitude_deg)));
762                }
763            }
764        }
765
766        out
767    }
768
769    /// Return ellipsoidal height `h` in metres using the embedded EGM96
770    /// 1-degree grid for `h = H + N`.
771    ///
772    /// The input position is terrain order `(longitude_deg, latitude_deg)`.
773    /// Internally, the geoid call is made with `(latitude_deg, longitude_deg)`.
774    /// The embedded EGM96 1-degree grid agrees with the full EGM96
775    /// 15-arcminute grid to about 0.4 m RMS.
776    pub fn ellipsoidal_height_m(
777        &self,
778        longitude_deg: f64,
779        latitude_deg: f64,
780    ) -> core::result::Result<EllipsoidalHeightM, TerrainDatumError> {
781        self.ellipsoidal_height_m_with_options(
782            longitude_deg,
783            latitude_deg,
784            DtedLookupOptions::default(),
785        )
786    }
787
788    /// Return ellipsoidal height `h` in metres using the embedded EGM96
789    /// 1-degree grid for `h = H + N` and explicit terrain lookup options.
790    ///
791    /// The input position is terrain order `(longitude_deg, latitude_deg)`.
792    /// Internally, the geoid call is made with `(latitude_deg, longitude_deg)`.
793    pub fn ellipsoidal_height_m_with_options(
794        &self,
795        longitude_deg: f64,
796        latitude_deg: f64,
797        options: DtedLookupOptions,
798    ) -> core::result::Result<EllipsoidalHeightM, TerrainDatumError> {
799        self.ellipsoidal_height_m_with_model(
800            longitude_deg,
801            latitude_deg,
802            options,
803            TerrainGeoidModel::Egm96OneDegree,
804        )
805    }
806
807    /// Return ellipsoidal height `h` in metres using an explicit geoid tier for
808    /// `h = H + N`.
809    ///
810    /// The input position is terrain order `(longitude_deg, latitude_deg)`.
811    /// Internally, the geoid call is made with `(latitude_deg, longitude_deg)`.
812    /// Choosing [`TerrainGeoidModel::Egm96FifteenMinute`] requires a loaded
813    /// `WW15MGH.DAC` grid and never falls back to the embedded EGM96 1-degree
814    /// grid.
815    pub fn ellipsoidal_height_m_with_model(
816        &self,
817        longitude_deg: f64,
818        latitude_deg: f64,
819        options: DtedLookupOptions,
820        geoid: TerrainGeoidModel<'_>,
821    ) -> core::result::Result<EllipsoidalHeightM, TerrainDatumError> {
822        let orthometric = self
823            .orthometric_height_m_with_options(longitude_deg, latitude_deg, options)
824            .map_err(TerrainDatumError::Terrain)?;
825        orthometric.to_ellipsoidal_height_deg(latitude_deg, longitude_deg, geoid)
826    }
827
828    fn resolve_grid(&self, longitude_deg: f64, latitude_deg: f64) -> Option<usize> {
829        for grid_idx in terrain_grid_candidates(longitude_deg, latitude_deg) {
830            if let Some(&tile_idx) = self.by_grid.get(&grid_idx) {
831                if self.tiles[tile_idx].contains(longitude_deg, latitude_deg) {
832                    return Some(tile_idx);
833                }
834            }
835        }
836        None
837    }
838
839    fn tile_payload(&self, tile: &MmapTile) -> &[u8] {
840        let start = tile.index.data_offset as usize;
841        let end = start + tile.index.data_len as usize;
842        &self.bytes[start..end]
843    }
844}
845
846/// Convert a DTED tile tree into canonical memory-mappable terrain store bytes.
847///
848/// Input `.dt2` files are discovered recursively below `root`, following
849/// symlinked directories and symlinked `.dt2` files. A symlinked file is treated
850/// as DTED when either the link name or the resolved target name ends in
851/// `.dt2`. Tiles are then sorted by integer tile id. DTED signed-magnitude
852/// postings are decoded once into `i16` orthometric metres. DTED negative zero
853/// and SRTM voids already encoded as zero remain zero, matching the existing
854/// lazy DTED reader.
855pub fn dted_tree_to_mmap_store(
856    root: impl AsRef<Path>,
857) -> core::result::Result<Vec<u8>, TerrainStoreError> {
858    let root = root.as_ref();
859    let mut paths = Vec::new();
860    collect_dted_tile_paths(root, &mut paths)?;
861    dted_paths_to_mmap_store(paths)
862}
863
864/// Convert an explicit DTED tile list into canonical memory-mappable terrain
865/// store bytes.
866///
867/// Each entry supplies the expected integer tile id and the DTED `.dt2` path.
868/// The converter parses each DTED header and fails if the file origin does not
869/// match the supplied id. The decoded payload, sorting, duplicate detection,
870/// alignment, and checksums are the same as [`dted_tree_to_mmap_store`].
871pub fn dted_tile_list_to_mmap_store(
872    entries: &[DtedTileListEntry],
873) -> core::result::Result<Vec<u8>, TerrainStoreError> {
874    let mut pending = Vec::with_capacity(entries.len());
875    for entry in entries {
876        pending.push(pending_tile_from_dted_path(
877            &entry.path,
878            Some(entry.tile_id),
879        )?);
880    }
881    build_store(pending)
882}
883
884/// Convert a DTED tile tree and write canonical memory-mappable terrain store
885/// bytes to `output_path`.
886///
887/// Symlinked directories and symlinked `.dt2` files below `root` are followed.
888/// A symlinked file is treated as DTED when either the link name or the resolved
889/// target name ends in `.dt2`.
890pub fn write_dted_tree_to_mmap_store(
891    root: impl AsRef<Path>,
892    output_path: impl AsRef<Path>,
893) -> core::result::Result<(), TerrainStoreError> {
894    let bytes = dted_tree_to_mmap_store(root)?;
895    let output_path = output_path.as_ref();
896    fs::write(output_path, &bytes).map_err(|err| TerrainStoreError::Io {
897        path: output_path.to_path_buf(),
898        message: err.to_string(),
899    })
900}
901
902/// Convert an explicit DTED tile list and write canonical memory-mappable
903/// terrain store bytes to `output_path`.
904pub fn write_dted_tile_list_to_mmap_store(
905    entries: &[DtedTileListEntry],
906    output_path: impl AsRef<Path>,
907) -> core::result::Result<(), TerrainStoreError> {
908    let bytes = dted_tile_list_to_mmap_store(entries)?;
909    let output_path = output_path.as_ref();
910    fs::write(output_path, &bytes).map_err(|err| TerrainStoreError::Io {
911        path: output_path.to_path_buf(),
912        message: err.to_string(),
913    })
914}
915
916/// Return an FNV-1a checksum for terrain store bytes.
917///
918/// This checksum is for deterministic local verification and is not a
919/// cryptographic digest.
920#[must_use]
921pub fn terrain_store_checksum64(bytes: &[u8]) -> u64 {
922    fnv1a64(bytes)
923}
924
925#[derive(Debug)]
926struct PendingTile {
927    lat_index: i32,
928    lon_index: i32,
929    min_latitude_deg: f64,
930    min_longitude_deg: f64,
931    max_latitude_deg: f64,
932    max_longitude_deg: f64,
933    lon_count: u32,
934    lat_count: u32,
935    data: Vec<u8>,
936    vertical_datum: VerticalDatum,
937}
938
939#[derive(Debug)]
940struct ParsedStore {
941    vertical_datum: VerticalDatum,
942    tiles: Vec<MmapTile>,
943    by_grid: HashMap<(i32, i32), usize>,
944    tile_index: Vec<TerrainStoreTileIndex>,
945    tile_ids: Vec<TerrainTileId>,
946}
947
948fn height_from_tile(
949    bytes: &[u8],
950    tile: &MmapTile,
951    longitude_deg: f64,
952    latitude_deg: f64,
953    options: DtedLookupOptions,
954) -> Result<f64> {
955    if options.interpolation == DtedInterpolation::NearestPosting {
956        return tile
957            .get_elevation(bytes, longitude_deg, latitude_deg)
958            .map(|v| v as f64);
959    }
960
961    let postings_per_deg_lon = tile.index.lon_count as usize - 1;
962    let postings_per_deg_lat = tile.index.lat_count as usize - 1;
963
964    let lon_idx = (longitude_deg - tile.index.min_longitude_deg) * postings_per_deg_lon as f64;
965    let lat_idx = (latitude_deg - tile.index.min_latitude_deg) * postings_per_deg_lat as f64;
966    let lon_lo = lon_idx.floor() as i64;
967    let lat_lo = lat_idx.floor() as i64;
968    let fx = lon_idx - lon_lo as f64;
969    let fy = lat_idx - lat_lo as f64;
970
971    let mut z = 0.0;
972    for (di, wx) in [(0i64, 1.0 - fx), (1i64, fx)] {
973        for (dj, wy) in [(0i64, 1.0 - fy), (1i64, fy)] {
974            let w = wx * wy;
975            if w == 0.0 {
976                continue;
977            }
978            let posting_lon =
979                tile.index.min_longitude_deg + (lon_lo + di) as f64 / postings_per_deg_lon as f64;
980            let posting_lat =
981                tile.index.min_latitude_deg + (lat_lo + dj) as f64 / postings_per_deg_lat as f64;
982            z += w * f64::from(tile.get_elevation(bytes, posting_lon, posting_lat)?);
983        }
984    }
985    Ok(z)
986}
987
988fn dted_paths_to_mmap_store(
989    mut paths: Vec<PathBuf>,
990) -> core::result::Result<Vec<u8>, TerrainStoreError> {
991    paths.sort();
992
993    let mut pending = Vec::with_capacity(paths.len());
994    for path in paths {
995        pending.push(pending_tile_from_dted_path(&path, None)?);
996    }
997    build_store(pending)
998}
999
1000fn pending_tile_from_dted_path(
1001    path: &Path,
1002    expected_id: Option<TerrainTileId>,
1003) -> core::result::Result<PendingTile, TerrainStoreError> {
1004    let tile = DtedTile::from_path(path).map_err(|reason| TerrainStoreError::Parse {
1005        reason: format!("{}: {reason}", path.display()),
1006    })?;
1007    let decoded = tile
1008        .decoded_postings_lon_major()
1009        .map_err(|reason| TerrainStoreError::Parse {
1010            reason: format!("{}: {reason}", path.display()),
1011        })?;
1012    let mut data = Vec::with_capacity(decoded.len() * 2);
1013    for posting in decoded {
1014        data.extend_from_slice(&posting.to_le_bytes());
1015    }
1016    let lat_index = tile.origin_latitude().floor() as i32;
1017    let lon_index = tile.origin_longitude().floor() as i32;
1018    let found = TerrainTileId::new(lat_index, lon_index);
1019    if let Some(expected) = expected_id {
1020        if expected != found {
1021            return Err(TerrainStoreError::TileIdMismatch {
1022                path: path.to_path_buf(),
1023                expected,
1024                found,
1025            });
1026        }
1027    }
1028    Ok(PendingTile {
1029        lat_index,
1030        lon_index,
1031        min_latitude_deg: tile.origin_latitude(),
1032        min_longitude_deg: tile.origin_longitude(),
1033        max_latitude_deg: tile.origin_latitude() + 1.0,
1034        max_longitude_deg: tile.origin_longitude() + 1.0,
1035        lon_count: u32::try_from(tile.lon_count()).map_err(|_| TerrainStoreError::Parse {
1036            reason: format!("{} longitude count exceeds u32", path.display()),
1037        })?,
1038        lat_count: u32::try_from(tile.lat_count()).map_err(|_| TerrainStoreError::Parse {
1039            reason: format!("{} latitude count exceeds u32", path.display()),
1040        })?,
1041        data,
1042        vertical_datum: VerticalDatum::Egm96MslOrthometric,
1043    })
1044}
1045
1046fn collect_dted_tile_paths(
1047    root: &Path,
1048    out: &mut Vec<PathBuf>,
1049) -> core::result::Result<(), TerrainStoreError> {
1050    let mut visited_dirs = HashSet::new();
1051    collect_dted_tile_paths_inner(root, out, &mut visited_dirs)
1052}
1053
1054fn collect_dted_tile_paths_inner(
1055    path: &Path,
1056    out: &mut Vec<PathBuf>,
1057    visited_dirs: &mut HashSet<PathBuf>,
1058) -> core::result::Result<(), TerrainStoreError> {
1059    let metadata = fs::metadata(path).map_err(|err| TerrainStoreError::Io {
1060        path: path.to_path_buf(),
1061        message: err.to_string(),
1062    })?;
1063
1064    if metadata.is_dir() {
1065        let canonical = fs::canonicalize(path).map_err(|err| TerrainStoreError::Io {
1066            path: path.to_path_buf(),
1067            message: err.to_string(),
1068        })?;
1069        if !visited_dirs.insert(canonical) {
1070            return Ok(());
1071        }
1072        let entries = fs::read_dir(path).map_err(|err| TerrainStoreError::Io {
1073            path: path.to_path_buf(),
1074            message: err.to_string(),
1075        })?;
1076        for entry in entries {
1077            let entry = entry.map_err(|err| TerrainStoreError::Io {
1078                path: path.to_path_buf(),
1079                message: err.to_string(),
1080            })?;
1081            collect_dted_tile_paths_inner(&entry.path(), out, visited_dirs)?;
1082        }
1083    } else if metadata.is_file() && is_dted_tile_source(path)? {
1084        out.push(path.to_path_buf());
1085    }
1086    Ok(())
1087}
1088
1089fn is_dted_tile_source(path: &Path) -> core::result::Result<bool, TerrainStoreError> {
1090    if is_dted_tile_path(path) {
1091        return Ok(true);
1092    }
1093
1094    let canonical = fs::canonicalize(path).map_err(|err| TerrainStoreError::Io {
1095        path: path.to_path_buf(),
1096        message: err.to_string(),
1097    })?;
1098    Ok(is_dted_tile_path(&canonical))
1099}
1100
1101fn is_dted_tile_path(path: &Path) -> bool {
1102    path.file_name()
1103        .and_then(|name| name.to_str())
1104        .is_some_and(|name| name.ends_with(".dt2"))
1105}
1106
1107fn parse_store(bytes: &[u8]) -> core::result::Result<ParsedStore, TerrainStoreError> {
1108    if bytes.len() < STORE_HEADER_LEN {
1109        return Err(TerrainStoreError::Parse {
1110            reason: format!(
1111                "store has {} bytes but needs at least {STORE_HEADER_LEN}",
1112                bytes.len()
1113            ),
1114        });
1115    }
1116    if &bytes[..STORE_MAGIC.len()] != STORE_MAGIC {
1117        return Err(TerrainStoreError::Parse {
1118            reason: "missing terrain store magic".to_string(),
1119        });
1120    }
1121    let version = read_u16(bytes, HEADER_VERSION_OFFSET)?;
1122    if version != STORE_VERSION {
1123        return Err(TerrainStoreError::UnsupportedVersion { version });
1124    }
1125    ensure_zero(bytes, 11, 12, "header reserved byte")?;
1126    ensure_zero(bytes, 40, STORE_HEADER_LEN, "header reserved bytes")?;
1127
1128    let vertical_datum = VerticalDatum::from_tag(bytes[HEADER_DATUM_OFFSET])?;
1129    let tile_count = read_u32(bytes, HEADER_TILE_COUNT_OFFSET)? as usize;
1130    let index_offset = read_u64(bytes, HEADER_INDEX_OFFSET_OFFSET)? as usize;
1131    let data_offset = read_u64(bytes, HEADER_DATA_OFFSET_OFFSET)? as usize;
1132    let total_len = read_u64(bytes, HEADER_TOTAL_LEN_OFFSET)? as usize;
1133    if total_len != bytes.len() {
1134        return Err(TerrainStoreError::Parse {
1135            reason: format!(
1136                "header total length {total_len} does not match {}",
1137                bytes.len()
1138            ),
1139        });
1140    }
1141    if index_offset != STORE_HEADER_LEN {
1142        return Err(TerrainStoreError::Parse {
1143            reason: format!("index offset must be {STORE_HEADER_LEN}, got {index_offset}"),
1144        });
1145    }
1146
1147    let index_len = tile_count
1148        .checked_mul(STORE_INDEX_RECORD_LEN)
1149        .ok_or_else(|| TerrainStoreError::Parse {
1150            reason: "tile index length overflows usize".to_string(),
1151        })?;
1152    let index_end =
1153        index_offset
1154            .checked_add(index_len)
1155            .ok_or_else(|| TerrainStoreError::Parse {
1156                reason: "tile index end overflows usize".to_string(),
1157            })?;
1158    if index_end > bytes.len() {
1159        return Err(TerrainStoreError::Parse {
1160            reason: "tile index extends past store length".to_string(),
1161        });
1162    }
1163    let expected_data_offset = align_up(index_end, STORE_ALIGNMENT)?;
1164    if data_offset != expected_data_offset {
1165        return Err(TerrainStoreError::Parse {
1166            reason: format!("data offset must be {expected_data_offset}, got {data_offset}"),
1167        });
1168    }
1169    ensure_zero(bytes, index_end, data_offset, "index padding")?;
1170
1171    let mut tiles = Vec::with_capacity(tile_count);
1172    let mut tile_index = Vec::with_capacity(tile_count);
1173    let mut tile_ids = Vec::with_capacity(tile_count);
1174    let mut by_grid = HashMap::with_capacity(tile_count);
1175    let mut previous_id = None;
1176    let mut expected_next = data_offset;
1177
1178    for idx in 0..tile_count {
1179        let record_offset = index_offset + idx * STORE_INDEX_RECORD_LEN;
1180        let record = &bytes[record_offset..record_offset + STORE_INDEX_RECORD_LEN];
1181        let lat_index = read_i32(record, INDEX_LAT_OFFSET)?;
1182        let lon_index = read_i32(record, INDEX_LON_OFFSET)?;
1183        let tile_id = (lat_index, lon_index);
1184        if previous_id.is_some_and(|previous| tile_id <= previous) {
1185            return Err(TerrainStoreError::Parse {
1186                reason: "tile index records are not strictly sorted".to_string(),
1187            });
1188        }
1189        previous_id = Some(tile_id);
1190
1191        let lon_count = read_u32(record, INDEX_LON_COUNT_OFFSET)?;
1192        let lat_count = read_u32(record, INDEX_LAT_COUNT_OFFSET)?;
1193        if lon_count < 2 || lat_count < 2 {
1194            return Err(TerrainStoreError::Parse {
1195                reason: format!(
1196                    "tile ({lat_index},{lon_index}) has invalid dimensions lon_count={lon_count} lat_count={lat_count}"
1197                ),
1198            });
1199        }
1200        let offset = read_u64(record, INDEX_DATA_OFFSET_OFFSET)? as usize;
1201        let data_len = read_u64(record, INDEX_DATA_LEN_OFFSET)? as usize;
1202        let expected_len = (lon_count as usize)
1203            .checked_mul(lat_count as usize)
1204            .and_then(|count| count.checked_mul(2))
1205            .ok_or_else(|| TerrainStoreError::Parse {
1206                reason: format!("tile ({lat_index},{lon_index}) data length overflows usize"),
1207            })?;
1208        if data_len != expected_len {
1209            return Err(TerrainStoreError::Parse {
1210                reason: format!(
1211                    "tile ({lat_index},{lon_index}) data length must be {expected_len}, got {data_len}"
1212                ),
1213            });
1214        }
1215
1216        let expected_offset = align_up(expected_next, STORE_ALIGNMENT)?;
1217        ensure_zero(bytes, expected_next, expected_offset, "tile padding")?;
1218        if offset != expected_offset {
1219            return Err(TerrainStoreError::Parse {
1220                reason: format!(
1221                    "tile ({lat_index},{lon_index}) data offset must be {expected_offset}, got {offset}"
1222                ),
1223            });
1224        }
1225        let end = offset
1226            .checked_add(data_len)
1227            .ok_or_else(|| TerrainStoreError::Parse {
1228                reason: format!("tile ({lat_index},{lon_index}) data end overflows usize"),
1229            })?;
1230        if end > bytes.len() {
1231            return Err(TerrainStoreError::Parse {
1232                reason: format!("tile ({lat_index},{lon_index}) data extends past store length"),
1233            });
1234        }
1235
1236        let checksum64 = read_u64(record, INDEX_CHECKSUM_OFFSET)?;
1237        let found = fnv1a64(&bytes[offset..end]);
1238        if found != checksum64 {
1239            return Err(TerrainStoreError::Checksum {
1240                lat_index,
1241                lon_index,
1242                expected: checksum64,
1243                found,
1244            });
1245        }
1246
1247        let min_latitude_deg = read_f64(record, INDEX_MIN_LAT_OFFSET)?;
1248        let min_longitude_deg = read_f64(record, INDEX_MIN_LON_OFFSET)?;
1249        let max_latitude_deg = read_f64(record, INDEX_MAX_LAT_OFFSET)?;
1250        let max_longitude_deg = read_f64(record, INDEX_MAX_LON_OFFSET)?;
1251        for (field, value) in [
1252            ("min_latitude_deg", min_latitude_deg),
1253            ("min_longitude_deg", min_longitude_deg),
1254            ("max_latitude_deg", max_latitude_deg),
1255            ("max_longitude_deg", max_longitude_deg),
1256        ] {
1257            if !value.is_finite() {
1258                return Err(TerrainStoreError::Parse {
1259                    reason: format!("tile ({lat_index},{lon_index}) {field} is not finite"),
1260                });
1261            }
1262        }
1263        let tile_datum = VerticalDatum::from_tag(record[INDEX_DATUM_OFFSET])?;
1264        if tile_datum != vertical_datum {
1265            return Err(TerrainStoreError::Parse {
1266                reason: format!("tile ({lat_index},{lon_index}) datum differs from header"),
1267            });
1268        }
1269        ensure_zero(
1270            record,
1271            INDEX_DATUM_OFFSET + 1,
1272            STORE_INDEX_RECORD_LEN,
1273            "tile index reserved bytes",
1274        )?;
1275
1276        let index = TerrainStoreTileIndex {
1277            lat_index,
1278            lon_index,
1279            min_longitude_deg,
1280            min_latitude_deg,
1281            max_longitude_deg,
1282            max_latitude_deg,
1283            lon_count,
1284            lat_count,
1285            data_offset: offset as u64,
1286            data_len: data_len as u64,
1287            checksum64,
1288            vertical_datum: tile_datum,
1289        };
1290        by_grid.insert(tile_id, tiles.len());
1291        tiles.push(MmapTile { index });
1292        tile_index.push(index);
1293        tile_ids.push(TerrainTileId::new(lat_index, lon_index));
1294        expected_next = end;
1295    }
1296
1297    if expected_next != bytes.len() {
1298        return Err(TerrainStoreError::Parse {
1299            reason: format!(
1300                "store has trailing bytes: expected length {expected_next}, got {}",
1301                bytes.len()
1302            ),
1303        });
1304    }
1305
1306    Ok(ParsedStore {
1307        vertical_datum,
1308        tiles,
1309        by_grid,
1310        tile_index,
1311        tile_ids,
1312    })
1313}
1314
1315fn missing_terrain_tile(longitude_deg: f64, latitude_deg: f64) -> Error {
1316    let (lat_index, lon_index) = terrain::terrain_grid(longitude_deg, latitude_deg);
1317    Error::MissingTerrainTile {
1318        lat_index,
1319        lon_index,
1320    }
1321}
1322
1323fn build_store(mut tiles: Vec<PendingTile>) -> core::result::Result<Vec<u8>, TerrainStoreError> {
1324    tiles.sort_by_key(|tile| (tile.lat_index, tile.lon_index));
1325    for pair in tiles.windows(2) {
1326        if (pair[0].lat_index, pair[0].lon_index) == (pair[1].lat_index, pair[1].lon_index) {
1327            return Err(TerrainStoreError::DuplicateTile {
1328                lat_index: pair[0].lat_index,
1329                lon_index: pair[0].lon_index,
1330            });
1331        }
1332    }
1333
1334    let index_end = STORE_HEADER_LEN
1335        .checked_add(
1336            tiles
1337                .len()
1338                .checked_mul(STORE_INDEX_RECORD_LEN)
1339                .ok_or_else(|| TerrainStoreError::Parse {
1340                    reason: "tile index length overflows usize".to_string(),
1341                })?,
1342        )
1343        .ok_or_else(|| TerrainStoreError::Parse {
1344            reason: "tile index end overflows usize".to_string(),
1345        })?;
1346    let data_offset = align_up(index_end, STORE_ALIGNMENT)?;
1347    let mut offsets = Vec::with_capacity(tiles.len());
1348    let mut cursor = data_offset;
1349    for tile in &tiles {
1350        cursor = align_up(cursor, STORE_ALIGNMENT)?;
1351        offsets.push(cursor);
1352        cursor = cursor
1353            .checked_add(tile.data.len())
1354            .ok_or_else(|| TerrainStoreError::Parse {
1355                reason: "store length overflows usize".to_string(),
1356            })?;
1357    }
1358
1359    let mut out = vec![0u8; cursor];
1360    out[..STORE_MAGIC.len()].copy_from_slice(STORE_MAGIC);
1361    write_u16(&mut out, HEADER_VERSION_OFFSET, STORE_VERSION);
1362    out[HEADER_DATUM_OFFSET] = VerticalDatum::Egm96MslOrthometric.tag();
1363    write_u32(
1364        &mut out,
1365        HEADER_TILE_COUNT_OFFSET,
1366        u32::try_from(tiles.len()).map_err(|_| TerrainStoreError::Parse {
1367            reason: "tile count exceeds u32".to_string(),
1368        })?,
1369    );
1370    write_u64(
1371        &mut out,
1372        HEADER_INDEX_OFFSET_OFFSET,
1373        STORE_HEADER_LEN as u64,
1374    );
1375    write_u64(&mut out, HEADER_DATA_OFFSET_OFFSET, data_offset as u64);
1376    write_u64(&mut out, HEADER_TOTAL_LEN_OFFSET, cursor as u64);
1377
1378    for (idx, tile) in tiles.iter().enumerate() {
1379        let record_offset = STORE_HEADER_LEN + idx * STORE_INDEX_RECORD_LEN;
1380        let offset = offsets[idx];
1381        let data_len = tile.data.len();
1382        let expected_len = (tile.lon_count as usize)
1383            .checked_mul(tile.lat_count as usize)
1384            .and_then(|count| count.checked_mul(2))
1385            .ok_or_else(|| TerrainStoreError::Parse {
1386                reason: format!(
1387                    "tile ({},{}) data length overflows usize",
1388                    tile.lat_index, tile.lon_index
1389                ),
1390            })?;
1391        if data_len != expected_len {
1392            return Err(TerrainStoreError::Parse {
1393                reason: format!(
1394                    "tile ({},{}) data length must be {expected_len}, got {data_len}",
1395                    tile.lat_index, tile.lon_index
1396                ),
1397            });
1398        }
1399
1400        let record = &mut out[record_offset..record_offset + STORE_INDEX_RECORD_LEN];
1401        write_i32(record, INDEX_LAT_OFFSET, tile.lat_index);
1402        write_i32(record, INDEX_LON_OFFSET, tile.lon_index);
1403        write_u32(record, INDEX_LON_COUNT_OFFSET, tile.lon_count);
1404        write_u32(record, INDEX_LAT_COUNT_OFFSET, tile.lat_count);
1405        write_u64(record, INDEX_DATA_OFFSET_OFFSET, offset as u64);
1406        write_u64(record, INDEX_DATA_LEN_OFFSET, data_len as u64);
1407        write_u64(record, INDEX_CHECKSUM_OFFSET, fnv1a64(&tile.data));
1408        write_f64(record, INDEX_MIN_LAT_OFFSET, tile.min_latitude_deg);
1409        write_f64(record, INDEX_MIN_LON_OFFSET, tile.min_longitude_deg);
1410        write_f64(record, INDEX_MAX_LAT_OFFSET, tile.max_latitude_deg);
1411        write_f64(record, INDEX_MAX_LON_OFFSET, tile.max_longitude_deg);
1412        record[INDEX_DATUM_OFFSET] = tile.vertical_datum.tag();
1413        out[offset..offset + data_len].copy_from_slice(&tile.data);
1414    }
1415
1416    Ok(out)
1417}
1418
1419fn align_up(value: usize, alignment: usize) -> core::result::Result<usize, TerrainStoreError> {
1420    let rem = value % alignment;
1421    if rem == 0 {
1422        Ok(value)
1423    } else {
1424        value
1425            .checked_add(alignment - rem)
1426            .ok_or_else(|| TerrainStoreError::Parse {
1427                reason: "aligned offset overflows usize".to_string(),
1428            })
1429    }
1430}
1431
1432fn ensure_zero(
1433    bytes: &[u8],
1434    start: usize,
1435    end: usize,
1436    context: &str,
1437) -> core::result::Result<(), TerrainStoreError> {
1438    if start > end || end > bytes.len() {
1439        return Err(TerrainStoreError::Parse {
1440            reason: format!("{context} range is out of bounds"),
1441        });
1442    }
1443    if bytes[start..end].iter().any(|&byte| byte != 0) {
1444        return Err(TerrainStoreError::Parse {
1445            reason: format!("{context} must be zero-filled"),
1446        });
1447    }
1448    Ok(())
1449}
1450
1451fn fnv1a64(bytes: &[u8]) -> u64 {
1452    bytes.iter().fold(FNV_OFFSET_BASIS, |hash, byte| {
1453        (hash ^ u64::from(*byte)).wrapping_mul(FNV_PRIME)
1454    })
1455}
1456
1457fn read_u16(bytes: &[u8], offset: usize) -> core::result::Result<u16, TerrainStoreError> {
1458    Ok(u16::from_le_bytes(read_array(bytes, offset)?))
1459}
1460
1461fn read_u32(bytes: &[u8], offset: usize) -> core::result::Result<u32, TerrainStoreError> {
1462    Ok(u32::from_le_bytes(read_array(bytes, offset)?))
1463}
1464
1465fn read_i32(bytes: &[u8], offset: usize) -> core::result::Result<i32, TerrainStoreError> {
1466    Ok(i32::from_le_bytes(read_array(bytes, offset)?))
1467}
1468
1469fn read_u64(bytes: &[u8], offset: usize) -> core::result::Result<u64, TerrainStoreError> {
1470    Ok(u64::from_le_bytes(read_array(bytes, offset)?))
1471}
1472
1473fn read_f64(bytes: &[u8], offset: usize) -> core::result::Result<f64, TerrainStoreError> {
1474    Ok(f64::from_le_bytes(read_array(bytes, offset)?))
1475}
1476
1477fn read_array<const N: usize>(
1478    bytes: &[u8],
1479    offset: usize,
1480) -> core::result::Result<[u8; N], TerrainStoreError> {
1481    let end = offset
1482        .checked_add(N)
1483        .ok_or_else(|| TerrainStoreError::Parse {
1484            reason: "numeric field offset overflows usize".to_string(),
1485        })?;
1486    let slice = bytes
1487        .get(offset..end)
1488        .ok_or_else(|| TerrainStoreError::Parse {
1489            reason: "numeric field extends past record".to_string(),
1490        })?;
1491    slice.try_into().map_err(|_| TerrainStoreError::Parse {
1492        reason: "numeric field has wrong length".to_string(),
1493    })
1494}
1495
1496fn write_u16(bytes: &mut [u8], offset: usize, value: u16) {
1497    bytes[offset..offset + 2].copy_from_slice(&value.to_le_bytes());
1498}
1499
1500fn write_u32(bytes: &mut [u8], offset: usize, value: u32) {
1501    bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
1502}
1503
1504fn write_i32(bytes: &mut [u8], offset: usize, value: i32) {
1505    bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
1506}
1507
1508fn write_u64(bytes: &mut [u8], offset: usize, value: u64) {
1509    bytes[offset..offset + 8].copy_from_slice(&value.to_le_bytes());
1510}
1511
1512fn write_f64(bytes: &mut [u8], offset: usize, value: f64) {
1513    bytes[offset..offset + 8].copy_from_slice(&value.to_le_bytes());
1514}