Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions earthgrid-core/src/openeo/geoprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::process::Command;
use chrono::{Datelike, NaiveDate};
use serde_json::Value;

use crate::reconstruct::{parse_allowed_crs, AllowedCrs};

// ---------------------------------------------------------------------------
// Reducer + temporal period labels
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -206,6 +208,20 @@ pub fn gdal_warp_geotiff(src_tiff: &[u8], cfg: &ResampleSpatialConfig) -> Result
);
}

// gdalwarp hands `-t_srs` to OSRSetFromUserInput, which would also open a
// URL or a path, so the projection text goes through the CRS allowlist —
// before any temp file is written.
let t_srs = if let Some(epsg) = cfg.epsg {
Some(format!("EPSG:{epsg}"))
} else if let Some(ref wkt) = cfg.wkt {
Some(match parse_allowed_crs(wkt).map_err(|e| format!("resample_spatial: {e}"))? {
AllowedCrs::Epsg(code) => format!("EPSG:{code}"),
AllowedCrs::Wkt(wkt) => wkt.to_string(),
})
} else {
None
};

let id = uuid::Uuid::new_v4();
let tmp_dir = std::env::temp_dir();
let src_path = tmp_dir.join(format!("eg_warp_src_{id}.tif"));
Expand All @@ -221,10 +237,8 @@ pub fn gdal_warp_geotiff(src_tiff: &[u8], cfg: &ResampleSpatialConfig) -> Result
.arg("-r")
.arg(gdal_resample_flag(&cfg.method));

if let Some(epsg) = cfg.epsg {
cmd.arg("-t_srs").arg(format!("EPSG:{epsg}"));
} else if let Some(ref wkt) = cfg.wkt {
cmd.arg("-t_srs").arg(wkt);
if let Some(t_srs) = t_srs {
cmd.arg("-t_srs").arg(t_srs);
}

if cfg.change_resolution {
Expand Down
52 changes: 45 additions & 7 deletions earthgrid-core/src/openeo/output.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! GeoTIFF / netCDF packaging and decode helpers for openEO results.

use gdal::raster::{Buffer, RasterCreationOptions};
use gdal::spatial_ref::SpatialRef;
use gdal::DriverManager;

use crate::openeo::types::RasterMeta;
use crate::reconstruct::parse_allowed_crs;
use super::geoprocess::{self, ResampleSpatialConfig};

pub fn canonical_format(fmt: &str) -> &str {
Expand Down Expand Up @@ -46,6 +46,9 @@ pub fn wrap_output(
}

pub fn wrap_geotiff(pixels: &[u8], meta: &RasterMeta) -> Result<Vec<u8>, String> {
// `meta.crs` can be item-provided text; refuse it, or a value GDAL cannot
// build, before the dataset exists.
let crs = parse_allowed_crs(&meta.crs)?.build()?;
let bands = meta.band_count.max(1);
let pixel_count = (meta.width * meta.height) as usize;
let per_band = pixel_count * 4;
Expand Down Expand Up @@ -78,9 +81,7 @@ pub fn wrap_geotiff(pixels: &[u8], meta: &RasterMeta) -> Result<Vec<u8>, String>

ds.set_geo_transform(&meta.transform)
.map_err(|e| format!("GDAL set geotransform: {e}"))?;
if let Ok(srs) = SpatialRef::from_definition(&meta.crs) {
let _ = ds.set_spatial_ref(&srs);
}
crs.assign_to(&mut ds)?;

for b in 0..bands {
let off = b * per_band;
Expand Down Expand Up @@ -110,6 +111,9 @@ pub fn wrap_netcdf(pixels: &[u8], meta: &RasterMeta) -> Result<Vec<u8>, String>
"netCDF export with multiple bands is not yet supported; use GTiff.".to_string(),
);
}
// `meta.crs` can be item-provided text; refuse it, or a value GDAL cannot
// build, before the dataset exists.
let crs = parse_allowed_crs(&meta.crs)?.build()?;
let pixel_count = meta.width * meta.height;
let expected = pixel_count * 4;
if pixels.len() < expected {
Expand Down Expand Up @@ -143,9 +147,7 @@ pub fn wrap_netcdf(pixels: &[u8], meta: &RasterMeta) -> Result<Vec<u8>, String>
})?;

let _ = ds.set_geo_transform(&meta.transform);
if let Ok(srs) = SpatialRef::from_definition(&meta.crs) {
let _ = ds.set_spatial_ref(&srs);
}
crs.assign_to(&mut ds)?;

let mut band = ds.rasterband(1).map_err(|e| format!("GDAL rasterband: {e}"))?;
let mut buf = Buffer::new((w, h), floats);
Expand Down Expand Up @@ -251,3 +253,39 @@ pub fn apply_resample_if_needed(
let (px, m2) = decode_geotiff_bsq_f32(&warped)?;
Ok((px, Some(m2)))
}

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

fn meta_1x1(crs: &str) -> RasterMeta {
RasterMeta {
width: 1,
height: 1,
crs: crs.to_string(),
transform: [0.0, 1.0, 0.0, 1.0, 0.0, -1.0],
dtype: "float32".to_string(),
band_count: 1,
}
}

/// `EPSG:999999` passes the lexical gate and GDAL cannot build it. Both
/// writers must return that as an error naming the value, not write an
/// output without a CRS.
#[test]
fn wrap_output_rejects_unbuildable_crs() {
let pixels = 1.0f32.to_le_bytes();
let meta = meta_1x1("EPSG:999999");

for result in [wrap_geotiff(&pixels, &meta), wrap_netcdf(&pixels, &meta)] {
let err = result.unwrap_err();
assert!(err.contains("Cannot build CRS"), "unexpected error: {err}");
assert!(err.contains("EPSG:999999"), "error must name the value: {err}");
}

// A CRS GDAL can build still comes out with that CRS.
let tiff = wrap_geotiff(&pixels, &meta_1x1("EPSG:32632")).unwrap();
let (_, decoded) = decode_geotiff_bsq_f32(&tiff).unwrap();
assert_eq!(decoded.crs, "EPSG:32632");
}
}
Loading
Loading