use rstiff::GeoTiff;
use std::error::Error;
use std::path::Path;
fn main() -> Result<(), Box<dyn Error>> {
let raster_path = Path::new("./data/Hawaiin_part.tif");
let kml_path = Path::new("./data/hilo.kml");
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);
}
println!("Reading raster: {:?}", raster_path);
let tif = GeoTiff::read(raster_path)?;
println!(
"Raster Loaded. Size: {:?}, Type: {:?}",
tif.data.dim(),
tif.original_type
);
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)?; 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).");
}
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);
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(())
}