geolith 0.2.2

Shared library of the geolith builders: source readers, conflation, admin areas, tile encoding and the PMTiles and MBTiles writers
Documentation
//! The `GeoTIFF` readers: COG decoding, DEM reading, CRS transforms and tile
//! ranges. Used by the raster datasets and by the terrain tiler.

pub mod cog;
pub mod crs;
pub mod dem;
pub mod range;

pub use cog::Cog;
pub use crs::Crs;
pub use dem::Dem;
pub use range::tile_range_for_bounds;

/// The most pixels a file of `file_len` bytes can hold: deflate and LZW expand
/// a byte by at most about 1032 times, and a bilevel image packs 8 pixels in a
/// byte. A header that claims more is not a raster that file holds.
const MAX_PIXELS_PER_FILE_BYTE: u64 = 8 * 1032;

/// Room for the headers of a tiny file.
const HEADER_SLACK_BYTES: u64 = 64 * 1024;

/// Refuse dimensions the file cannot hold, before a decoder allocates for
/// them (the decoders run with no memory limit, because a DEM is read whole).
///
/// # Errors
///
/// [`crate::error::GeolithError::GeoTiff`] naming `path` when the pixel count
/// is more than `file_len` bytes can decode to, or does not fit in a `u64`.
pub(crate) fn check_raster_size(
    path: &std::path::Path,
    file_len: u64,
    width: u32,
    height: u32,
) -> crate::error::Result<()> {
    let pixels = u64::from(width) * u64::from(height);
    let most = file_len
        .saturating_add(HEADER_SLACK_BYTES)
        .saturating_mul(MAX_PIXELS_PER_FILE_BYTE);
    if pixels > most {
        return Err(crate::error::GeolithError::GeoTiff(format!(
            "{}: the header claims {width}x{height} pixels, more than a file of {file_len} bytes can hold",
            path.display()
        )));
    }
    Ok(())
}

/// Refuse a tile or strip of no pixels: a division by it panics.
///
/// # Errors
///
/// [`crate::error::GeolithError::GeoTiff`] naming `path` when either side is 0.
pub(crate) fn check_chunk_size(
    path: &std::path::Path,
    chunk_width: u32,
    chunk_height: u32,
) -> crate::error::Result<()> {
    if chunk_width == 0 || chunk_height == 0 {
        return Err(crate::error::GeolithError::GeoTiff(format!(
            "{}: the header declares a {chunk_width}x{chunk_height} tile or strip",
            path.display()
        )));
    }
    Ok(())
}

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

    #[test]
    fn a_tiny_file_cannot_claim_a_continent() {
        let path = Path::new("dem.tif");
        // 200 bytes of file and a header for 100000 x 100000 pixels.
        let err = check_raster_size(path, 200, 100_000, 100_000).expect_err("10^10 pixels");
        let text = err.to_string();
        assert!(
            text.contains("dem.tif") && text.contains("100000x100000"),
            "{text}"
        );
        assert!(check_raster_size(path, 200, u32::MAX, u32::MAX).is_err());
    }

    #[test]
    fn a_plausible_header_passes() {
        let path = Path::new("dem.tif");
        // 3601 x 3601 SRTM tile, uncompressed 2 bytes a pixel: 26 MB.
        assert!(check_raster_size(path, 25_934_402, 3601, 3601).is_ok());
        // An all-nodata ocean tile deflates about 1000 : 1.
        assert!(check_raster_size(path, 14_000, 3601, 3601).is_ok());
        // The most a megabyte can hold, plus the slack for headers.
        let limit = (1_048_576 + HEADER_SLACK_BYTES) * MAX_PIXELS_PER_FILE_BYTE;
        assert!(check_raster_size(path, 1_048_576, 65_536, (limit / 65_536) as u32).is_ok());
        assert!(check_raster_size(path, 1_048_576, 65_536, (limit / 65_536) as u32 + 1).is_err());
    }

    #[test]
    fn a_chunk_of_no_pixels_is_refused() {
        let path = Path::new("cog.tif");
        assert!(check_chunk_size(path, 0, 256).is_err());
        assert!(check_chunk_size(path, 256, 0).is_err());
        assert!(check_chunk_size(path, 256, 256).is_ok());
    }
}