geolith-tiles-builder 0.4.2

Builds tiles from OpenStreetMap and Overture Maps: protomaps-schema vector PMTiles, Terrain-RGB, raster archives and 3D Tiles from Gaussian splats
Documentation
//! The error of the vector tile build.

use std::path::PathBuf;

use geolith::error::GeolithError;

/// Why a vector tile build failed.
///
/// Errors from the shared readers and writers arrive as
/// [`TilesError::Geolith`]. The other variants name the step of the build
/// that failed and carry the path or thread involved.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum TilesError {
    /// A reader, writer or encoder of the `geolith` library failed.
    #[error(transparent)]
    Geolith(#[from] GeolithError),

    /// A file operation failed.
    #[error("i/o error")]
    Io(#[from] std::io::Error),

    /// `--bbox` is not `west,south,east,north`.
    #[error("invalid --bbox")]
    InvalidBbox(#[source] GeolithError),

    /// A sort run is cut, damaged, of another version, or cannot be written.
    #[error(transparent)]
    SortRun(#[from] crate::sort::error::SortRunError),

    /// A volume the build writes to has less free space than the build needs.
    #[error(
        "not enough free disk on the volume of {}: the build needs about {need_gib} GiB there \
         ({what}) and {free_gib} GiB are free. Free the space, point --tmpdir or --output at a \
         larger volume{}, or pass --no-disk-check to skip this check",
        path.display(),
        if *reclaim_chunks { "" } else { ", or pass --reclaim-chunks to lower the peak" }
    )]
    InsufficientDisk {
        /// A path on the volume.
        path: PathBuf,
        /// GiB the build needs on the volume at its peak.
        need_gib: u64,
        /// GiB free on the volume.
        free_gib: u64,
        /// What the build puts on the volume.
        what: &'static str,
        /// Whether `--reclaim-chunks` is already set.
        reclaim_chunks: bool,
    },

    /// `--reclaim-source-after-read` with a data folder that lacks an Overture
    /// type the enabled layers read.
    #[error(
        "{} holds no Parquet file of {}; with --reclaim-source-after-read the build would delete \
         what it reads and ship an incomplete archive. Download the missing types, or pass \
         --allow-missing-type",
        data.display(),
        types.join(", ")
    )]
    MissingOvertureTypes {
        /// The data folder.
        data: PathBuf,
        /// The `theme/type` names.
        types: Vec<String>,
    },

    /// The merge opens every sort run at once, and the open-file limit is
    /// lower than that and cannot be raised.
    #[error(
        "the merge needs {need} open files for {runs} sort runs, and the limit is {limit}: \
         raise it with `ulimit -n {need}` (or a higher hard limit) and run again with --resume"
    )]
    TooManyRuns {
        /// Sort runs to merge.
        runs: usize,
        /// The open-file limit in force.
        limit: u64,
        /// Files the merge needs.
        need: u64,
    },

    /// The scratch directory for the sort chunks cannot be created.
    #[error("failed to create chunk dir: {}", path.display())]
    CreateChunkDir {
        /// The directory.
        path: PathBuf,
        /// What the operating system said.
        #[source]
        source: std::io::Error,
    },

    /// The DEM cannot be opened or read: the inputs of `terrain --dem`.
    #[error("opening the DEM {}", dems.iter().map(|d| d.display().to_string()).collect::<Vec<_>>().join(", "))]
    OpenDem {
        /// The `--dem` values.
        dems: Vec<PathBuf>,
        /// The reader error.
        #[source]
        source: GeolithError,
    },

    /// The splat PLY cannot be read.
    #[error("reading {}", path.display())]
    ReadPly {
        /// The `--ply` value.
        path: PathBuf,
        /// The reader error.
        #[source]
        source: GeolithError,
    },

    /// The build area touches Survey of India land and `--india-boundary`
    /// was not given.
    #[error(
        "the tile build area touches Survey of India land (the whole world without --bbox): \
         pass --india-boundary <Survey of India lines GeoJSON>"
    )]
    IndiaBoundaryRequired,

    /// `--india-boundary` holds no closed ring.
    #[error("--india-boundary has no closed ring, so the Survey of India land cannot be built")]
    IndiaBoundaryWithoutRing,

    /// A lock was poisoned by a panic in another worker.
    #[error("lock poisoned: {what}")]
    LockPoisoned {
        /// The data the lock guarded.
        what: &'static str,
    },

    /// The OSM PBF cannot be opened, read or decoded.
    #[error("OSM PBF {step}")]
    OsmPbf {
        /// What the reader was doing: `blob reader`, `blob read` or `decode`.
        step: &'static str,
        /// The reader error.
        #[source]
        source: osmpbf::Error,
    },

    /// A worker thread panicked.
    #[error("{thread} thread panicked: {message}")]
    ThreadPanicked {
        /// The worker: `land polygon` or `water polygon`.
        thread: &'static str,
        /// The panic message.
        message: String,
    },

    /// The thread pool of `--threads` could not be built.
    #[error("cannot build a thread pool for --threads: {0}")]
    ThreadPool(String),

    /// `--resume` points at a directory that does not exist.
    #[error(
        "--resume requires --tmpdir to point at existing sort chunks, but {} does not exist",
        path.display()
    )]
    ResumeDirMissing {
        /// The `--tmpdir` value.
        path: PathBuf,
    },

    /// `--resume` points at chunks that a `--reclaim-chunks` merge already
    /// began to consume.
    #[error(
        "the sort chunks in {} cannot be resumed: an earlier merge with --reclaim-chunks began \
         to consume them, so runs are missing or hollow and a merge now would leave tiles out. \
         Delete the folder and build again from phase 1",
        path.display()
    )]
    ChunksConsumed {
        /// The `--tmpdir` value.
        path: PathBuf,
    },

    /// `--resume` points at chunks of a phase 1 that did not finish.
    #[error(
        "the sort chunks in {} cannot be resumed: phase 1 did not finish (there is no valid \
         phase1.complete marker; the chunks may also come from an older version). Delete the \
         folder and build again from phase 1",
        path.display()
    )]
    Phase1Incomplete {
        /// The `--tmpdir` value.
        path: PathBuf,
    },

    /// `--resume` found other runs than phase 1 wrote.
    #[error(
        "the sort chunks in {} cannot be resumed: phase 1 wrote {expected} runs and {found} are \
         there; a merge now would leave tiles out. Delete the folder and build again from phase 1",
        path.display()
    )]
    RunCountMismatch {
        /// The `--tmpdir` value.
        path: PathBuf,
        /// The runs phase 1 wrote.
        expected: usize,
        /// The runs found.
        found: usize,
    },

    /// `--resume` found no chunk file to continue from.
    #[error("No chunk files found in {} — nothing to resume from", path.display())]
    NoChunksToResume {
        /// The `--tmpdir` value.
        path: PathBuf,
    },

    /// The sources gave no tile feature.
    #[error("No tile features generated — nothing to write")]
    NothingToWrite,

    /// The `PMTiles` metadata JSON cannot be serialised.
    #[error("serializing PMTiles metadata")]
    Metadata(#[source] serde_json::Error),

    /// A shapefile directory holds no `.shp` file.
    #[error("No .shp file found in {}", path.display())]
    NoShapefile {
        /// The directory.
        path: PathBuf,
    },

    /// The shapefile path does not exist.
    #[error("Shapefile path does not exist: {}", path.display())]
    ShapefileMissing {
        /// The path.
        path: PathBuf,
    },

    /// `mlt-core` panicked while it encoded a tile.
    #[error("mlt-core panicked during MVT→MLT encoding: {message}")]
    MltPanic {
        /// The panic message.
        message: String,
    },

    /// An MVT tile cannot be parsed for the MLT conversion.
    #[error("failed to parse MVT tile")]
    MltParse(#[source] mlt_core::MltError),

    /// An MLT layer cannot be encoded.
    #[error("failed to encode MLT layer")]
    MltEncode(#[source] mlt_core::MltError),

    /// A tile of the archive cannot be encoded as MLT.
    #[error("MLT encode of tile {z}/{x}/{y}")]
    TileEncode {
        /// Zoom of the tile.
        z: u8,
        /// Column of the tile.
        x: u32,
        /// Row of the tile.
        y: u32,
        /// Why the tile failed.
        #[source]
        source: Box<TilesError>,
    },

    /// The merge of the features of one tile panicked. A tile cannot be
    /// skipped, so the build stops; the panic message is in the log.
    #[error("merging the features of tile {z}/{x}/{y} panicked; see the log for the panic")]
    TileMerge {
        /// Zoom of the tile.
        z: u8,
        /// Column of the tile.
        x: u32,
        /// Row of the tile.
        y: u32,
    },

    /// More features lost a piece to a caught clipper panic than the build
    /// may lose. The pieces were skipped; the count is a sign of bad source
    /// geometry, not of a bug to ignore.
    #[error(
        "{count} features lost geometry to a caught clipping panic, above the limit of \
         {allowed}{first}. Look for invalid rings (a NaN vertex, for one) in the sources"
    )]
    GeometryFailures {
        /// Failed features in this build.
        count: u64,
        /// The limit.
        allowed: u64,
        /// The first failure, as `"; first: ..."`, or empty.
        first: String,
    },
}

/// Result of the vector tile build.
pub type Result<T, E = TilesError> = std::result::Result<T, E>;