rstiff 0.2.0

A Rust library for high-precision, type-preserving GeoTiff I/O powered by GDAL.
use rstiff::GeoTiff;
use std::error::Error;
use std::path::Path;

fn main() -> Result<(), Box<dyn Error>> {
    // Input Files
    let raster_path = Path::new("./data/Hawaiin_part.tif");
    let kml_path = Path::new("./data/hilo.kml");

    // Check availability
    if !raster_path.exists() {
        println!("Error: Raster file not found: {:?}", raster_path);
        return Ok(());
    }

    if !kml_path.exists() {
        println!("Error: KML file not found: {:?}", kml_path);
        // We continue just to show the logic, but it will fail at runtime
    }

    // 1. Read Raster
    println!("Reading raster: {:?}", raster_path);
    let tif = GeoTiff::read(raster_path)?;
    println!(
        "Raster Loaded. Size: {:?}, Type: {:?}",
        tif.data.dim(),
        tif.original_type
    );

    // 2. Crop by KML (using Mask)
    // apply_mask: true means pixels outside the vector polygon will be set to NoData (Transparent)
    println!("\n--- Scenario 1: Crop by KML with Mask ---");
    if kml_path.exists() {
        println!("Cropping by KML: {:?}", kml_path);
        let cropped_tif = tif.crop_by_vector(kml_path, true)?; // true = Mask out background
        let output_path = Path::new("./data/Hawaiin_clipbykml.tif");
        cropped_tif.write(output_path)?;
        println!("Saved KML crop to: {:?}", output_path);
    } else {
        println!("Skipping KML crop (file missing).");
    }

    // 3. Crop by Shapefile (Hypothetical Example)
    // The code is identical for .shp files, as GDAL handles the driver automatically.
    println!("\n--- Scenario 2: Crop by Shapefile (Code Example) ---");
    let shp_path = Path::new("./data/study_area.shp");
    if shp_path.exists() {
        println!("Cropping by Shapefile: {:?}", shp_path);
        // apply_mask: false means we only crop to the bounding box of the vector,
        // but we keep all pixels inside that box (rectangular crop).
        let cropped_tif_bbox = tif.crop_by_vector(shp_path, false)?;
        let output_path = Path::new("./data/Hawaiin_clip_bbox.tif");
        cropped_tif_bbox.write(output_path)?;
        println!("Saved SHP bbox crop to: {:?}", output_path);
    } else {
        println!(
            "Tips: To crop with a Shapefile, simply replace the path: \n \
             `tif.crop_by_vector(\"path/to/file.shp\", true)?`"
        );
    }

    Ok(())
}