geolith-dataset-builder 0.4.2

Raster datasets for geolith: district areas, normals, numbers and a map layer per period, and the steps that stage, verify and publish them to any S3-compatible bucket
Documentation
#![warn(missing_docs)]

//! Raster datasets: district numbers and a map layer per period.
//!
//! `geolith dataset` turns a folder of gridded rasters (CHIRPS rainfall
//! first) into per-district values and a PNG map layer. Its engine steps are
//! `areas` (district polygons from Overture, India cut with the Survey of
//! India outline), `normals` (per-district baseline averages), `values` (the
//! area values of one raster, cached for `normals`), `build` (numbers, map
//! layer and files for every period on disk), `inspect` (a raster's layout
//! and whether each read path decodes it) and `downloads` (the Parquet and
//! `GeoParquet` files of a stage). The operator steps are `stage` (an
//! upload-ready tree of the object keys), `verify` (the checks a stage must
//! pass), `status` (the newest stage and its staleness) and `keys` (the keys
//! an upload would write, from local files).

pub mod accuracy;
pub mod areas;
pub mod areas_tiles;
pub mod baseline;
pub mod bounds;
pub mod cdse;
pub mod cli;
pub mod config;
pub mod country;
pub mod coverage;
pub mod crosswalk;
pub mod crosswalk_gen;
pub mod downloads;
pub mod error;
mod exact_json;
pub mod exit;
pub mod fetch;
pub mod files;
pub mod inputs;
pub mod inspect;
pub mod keys;
pub mod layer;
pub mod layout;
pub mod lgd;
pub mod lists;
pub mod normals;
pub mod numbers;
pub mod official_areas;
pub mod period;
pub mod prune;
pub mod rasters;
pub mod refresh;
pub mod remote;
pub mod run;
pub mod s3;
pub mod schedule;
pub mod series;
pub mod settings;
pub mod slots;
pub mod source;
pub mod stage;
pub mod status;
pub mod topology;
pub mod verify;

use crate::cli::{DatasetAction, DatasetArgs};
use crate::error::Result;

/// Run the dataset step selected by `args`: the deployment settings are
/// read once (the process environment, the `.env` file, then the JSON file of
/// `--config` or `GEOLITH_CONFIG`), then the step runs.
///
/// # Errors
///
/// Fails when the deployment settings cannot be read, and propagates the
/// error of the selected step.
pub fn run(args: &DatasetArgs) -> Result<()> {
    settings::init(settings::Deployment::load(args.config.as_deref())?);
    dispatch(args)
}

/// The step selected by `args`, over the deployment settings already fixed.
fn dispatch(args: &DatasetArgs) -> Result<()> {
    match &args.action {
        DatasetAction::Areas(a) => areas::run(a),
        DatasetAction::Normals(a) => normals::run(a),
        DatasetAction::Build(a) => files::run(a),
        DatasetAction::Values(a) => normals::run_values(a),
        DatasetAction::Inspect(a) => inspect::run(a),
        DatasetAction::Downloads(a) => downloads::run(a),
        DatasetAction::Stage(a) => stage::run(a).map(|dir| {
            println!("{}", dir.display());
        }),
        DatasetAction::Verify(a) => verify::run(a),
        DatasetAction::Status(a) => status::run(a),
        DatasetAction::Keys(a) => keys::run(a),
        DatasetAction::Upload(a) => remote::upload(a),
        DatasetAction::Publish(a) => remote::publish(a),
        DatasetAction::Lists(a) => lists::run(a),
        DatasetAction::Fetch(a) => fetch::run(a),
        DatasetAction::BaselinePass(a) => baseline::run(a),
        DatasetAction::Cdse(a) => cdse::run(a),
        DatasetAction::Lgd(a) => lgd::run(a),
        DatasetAction::Crosswalk(a) => crosswalk_gen::run(a),
        DatasetAction::Inputs(a) => inputs::run(a),
        DatasetAction::Run(a) => run::run(a),
        DatasetAction::Prune(a) => prune::run(a),
        DatasetAction::Bounds(a) => bounds::run(a),
        DatasetAction::Slots(a) => slots::run(a),
        DatasetAction::Accuracy(a) => accuracy::run(a),
        DatasetAction::Schedule(a) => schedule::run(a),
        DatasetAction::Refresh(a) => refresh::run(a),
    }
}

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

    #[test]
    fn the_build_step_is_dispatched_to_files() {
        let args = <DatasetArgs as clap::Parser>::try_parse_from([
            "geolith dataset",
            "build",
            "--config",
            "/nonexistent/build-config.json",
            "--rasters",
            "/r",
            "--areas",
            "/a",
            "--out",
            "/d",
        ])
        .expect("dataset argv parses");
        let err = dispatch(&args).expect_err("the config does not exist");
        assert!(
            geolith::error::report(&err).contains("/nonexistent/build-config.json"),
            "{err:#}"
        );
    }

    #[test]
    fn the_settings_file_flag_goes_before_the_step_and_leaves_the_step_config_alone() {
        let args = <DatasetArgs as clap::Parser>::try_parse_from([
            "geolith dataset",
            "--config",
            "/etc/geolith.json",
            "refresh",
            "--dry-run",
        ])
        .expect("the settings flag parses before the step");
        assert_eq!(
            args.config,
            Some(std::path::PathBuf::from("/etc/geolith.json"))
        );
        assert!(matches!(args.action, DatasetAction::Refresh(_)));
        // `normals` keeps its own `--config`, the dataset config.
        let args = <DatasetArgs as clap::Parser>::try_parse_from([
            "geolith dataset",
            "normals",
            "--config",
            "/etc/rainfall.json",
            "--rasters",
            "/r",
            "--areas",
            "/a",
            "--slots",
            "01-1",
            "--out",
            "/o",
        ])
        .expect("normals parses");
        assert_eq!(args.config, None);
        let DatasetAction::Normals(normals) = args.action else {
            panic!("expected normals");
        };
        assert_eq!(
            normals.config,
            std::path::PathBuf::from("/etc/rainfall.json")
        );
    }

    #[test]
    fn the_values_step_is_dispatched_to_normals() {
        let args = <DatasetArgs as clap::Parser>::try_parse_from([
            "geolith dataset",
            "values",
            "--config",
            "/nonexistent/values-config.json",
            "--raster",
            "/r/x.tif",
            "--areas",
            "/a",
            "--cache",
            "/c",
        ])
        .expect("dataset values argv parses");
        let err = dispatch(&args).expect_err("the config does not exist");
        assert!(
            geolith::error::report(&err).contains("/nonexistent/values-config.json"),
            "{err:#}"
        );
    }
}