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
47 changes: 38 additions & 9 deletions crates/analysis/src/warp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,8 @@ impl Canvas {

/// How `r` is held: as a `picture` (a scan being georeferenced), a grey byte image gains
/// alpha as a colour one does; as data, it keeps its one band, and a cell with no value is
/// its no-data, else NaN in a floating-point type and 0 in a whole-number one, as GDAL leaves
/// it.
/// its no-data, else NaN for floating-point or named-class data and 0 in other whole-number
/// data. Named class zero must remain distinct from cells outside the coverage.
fn of(r: &Raster, picture: bool) -> Canvas {
let nb = r.bands.len();
let alpha_src = match r.color {
Expand All @@ -606,7 +606,8 @@ impl Canvas {
let add_alpha = ((rgb && nb == 3) || gray) && alpha_src.is_none() && r.nodata.is_none();
// Cells with no value: no-data where the raster has some. Colours with alpha are zero there.
let clear = alpha_src.is_some() || add_alpha;
let none = if picture || rgbi || r.data_type.starts_with("Float") { f32::NAN } else { 0.0 };
let labelled = r.bands.iter().any(|b| !b.classes.is_empty());
let none = if picture || rgbi || labelled || r.data_type.starts_with("Float") { f32::NAN } else { 0.0 };
Comment thread
giswqs marked this conversation as resolved.
Resolved
Comment thread
giswqs marked this conversation as resolved.
Resolved
let outside = if clear { 0.0 } else { r.nodata.map_or(none, |d| d as f32) };
let color = match (add_alpha, rgb) {
(true, true) => ColorHint::Rgb { alpha: true },
Expand Down Expand Up @@ -635,8 +636,8 @@ impl Canvas {
}
}

/// The output raster on `g`, its bands named as the source's.
fn finish(&self, r: &Raster, g: &Grid, bands: Vec<Vec<f32>>, name: String) -> Result<Raster, String> {
/// The output on `g`, keeping band labels and credits, and class names when values are preserved.
fn finish(&self, r: &Raster, g: &Grid, bands: Vec<Vec<f32>>, method: Method, name: String) -> Result<Raster, String> {
let mut out = Raster::new(
name,
g.width as u32,
Expand All @@ -651,7 +652,14 @@ impl Canvas {
.map_err(|e| e.to_string())?;
for (i, band) in out.bands.iter_mut().enumerate() {
band.name = r.bands.get(i).map_or_else(|| "Alpha".to_string(), |b| b.name.clone());
if matches!(method, Method::Sample(Resampling::Nearest) | Method::Mode)
&& let Some(source) = r.bands.get(i)
{
band.classes = source.classes.clone();
Comment thread
giswqs marked this conversation as resolved.
Resolved
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
out.copyright = r.copyright.clone();
out.keep_rounded_mark(r);
Comment thread
giswqs marked this conversation as resolved.
Resolved
Ok(out)
}
}
Expand Down Expand Up @@ -756,7 +764,7 @@ fn sampled(
let sampler = Sampler { r, method, alpha: canvas.alpha_src, round: !r.data_type.starts_with("Float") };
let mut bands = canvas.bands(ow, oh)?;
if ow == 0 {
return canvas.finish(r, g, bands, name);
return canvas.finish(r, g, bands, Method::Sample(method), name);
}
let x_at = |ox: usize| g.min_x + (ox as f64 + 0.5) * g.cell_x;
// A cell's work in `MIN_ROW_WORK`'s units (some 30 ns): its pixels read per band, and its
Expand Down Expand Up @@ -787,7 +795,7 @@ fn sampled(
}
},
)?;
canvas.finish(r, g, bands, name)
canvas.finish(r, g, bands, Method::Sample(method), name)
}

/// `r` on the grid `g`, each cell the mean (or, with `mode`, the most frequent value, with GDAL's
Expand All @@ -811,7 +819,7 @@ fn aggregated(
let nearest = Sampler { r, method: Resampling::Nearest, alpha: None, round };
let mut bands = canvas.bands(ow, oh)?;
if ow == 0 {
return canvas.finish(r, g, bands, name);
return canvas.finish(r, g, bands, if mode { Method::Mode } else { Method::Average }, name);
}
let x_at = |i: usize| g.min_x + i as f64 * g.cell_x;
let each = Aggregate { r, canvas: &canvas, nearest, mode, round };
Expand Down Expand Up @@ -847,7 +855,7 @@ fn aggregated(
each.row(s, &mut line);
},
)?;
canvas.finish(r, g, bands, name)
canvas.finish(r, g, bands, if mode { Method::Mode } else { Method::Average }, name)
}

/// A thread's scratch in [`aggregated`].
Expand Down Expand Up @@ -1435,6 +1443,27 @@ mod tests {
assert_eq!((out.band_count(), out.value(0, 0, 0), out.value(0, 3, 1)), (1, None, Some(3.5)));
}

#[test]
fn projected_classes_keep_zero_distinct_from_outside_cells() {
let gt = GeoTransform { origin_x: 0.0, origin_y: 2.0, pixel_width: 1.0, pixel_height: -1.0 };
let mut r = Raster::new("c", 2, 2, vec![vec![0.0, 1.0, 1.0, 0.0]], gt, Crs::LonLat, None, ColorHint::Gray, "UInt8").unwrap();
r.bands[0].classes = vec!["water".into(), "land".into()];
let g = Grid::snapped(r.crs, [-1.0, 0.0, 2.0, 2.0], (1.0, 1.0), (0.0, 0.0)).unwrap();
for method in [Method::Sample(Resampling::Nearest), Method::Mode] {
let out = project(&r, &g, method, "p".into()).unwrap();
assert_eq!(out.value(0, 0, 0), None, "outside must not become class zero");
assert_eq!(out.value(0, 1, 0), Some(0.0));
assert_eq!(out.bands[0].class_name(0.0), Some("water"));
assert_eq!(out.bands[0].classes, r.bands[0].classes);
for bytes in [rustgis_data::raster::geotiff::write(&out, 0).unwrap(), rustgis_data::raster::cogwrite::write(&out, 0).unwrap()] {
let written = rustgis_data::raster::read_bytes("p.tif", &bytes).unwrap();
assert_eq!(written.value(0, 0, 0), None);
assert_eq!(written.value(0, 1, 0), Some(0.0));
assert_eq!(written.bands[0].classes, r.bands[0].classes);
}
}
}

/// Grids with the same cell and origin line up wherever their areas are; a grid too large to
/// hold is refused before anything is made.
#[test]
Expand Down
20 changes: 19 additions & 1 deletion crates/data/src/raster/cogwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ fn plan(r: &Raster, band: usize) -> Result<Plan> {
};
let max = if sample == Sample::Byte { 255.0 } else { 65_535.0 };
// A few distinct whole numbers are classes; photographs and rasters with a range are not.
let categorical = sample != Sample::Float && b.stats.categories.as_ref().is_some_and(|c| c.len() <= 64);
let categorical = !b.classes.is_empty() || (sample != Sample::Float && b.stats.categories.as_ref().is_some_and(|c| c.len() <= 64));
Ok(Plan {
bands: vec![band],
sample,
Expand Down Expand Up @@ -750,6 +750,24 @@ mod tests {
assert_eq!(at(0, 3, 1), 14);
}

#[test]
fn raster_export_review_named_classes_keep_codes_in_float_overviews() {
let (w, h) = (700u32, 4u32);
let data = (0..w * h).map(|i| if i % w < 2 { f32::NAN } else { (i % 2) as f32 }).collect();
let mut r = utm(w, h, vec![data], ColorHint::Gray, "UInt8", None);
r.bands[0].classes = vec!["Water".into(), "Land".into()];
let bytes = write(&r, 0).unwrap();
let ov = geotiff::read_limited(geotiff::reader(Cursor::new(bytes.as_slice())).unwrap(), "o", 1500, None).unwrap();
assert_eq!((ov.width, ov.height), (350, 2));
assert_eq!(ov.value(0, 0, 0), None);
assert!(ov.bands[0].data.iter().all(|v| v.is_nan() || *v == 0.0 || *v == 1.0), "no averaged class codes");
assert_eq!(ov.bands[0].classes, r.bands[0].classes);
r.bands[0].classes.clear();
let bytes = write(&r, 0).unwrap();
let ov = geotiff::read_limited(geotiff::reader(Cursor::new(bytes.as_slice())).unwrap(), "o", 1500, None).unwrap();
assert_eq!(ov.value(0, 2, 0), Some(0.5), "continuous float overviews still average");
}

#[test]
fn edges_of_odd_sizes_average_what_is_there() {
// 1025 × 3 floats: the second overview's last column averages one pixel column, and the
Expand Down
57 changes: 47 additions & 10 deletions crates/engine/src/cmd/imagery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ fn warp_to_fit(s: &mut Session, p: &Value, layer: &str, r: &Raster) -> CmdResult
let new_layer = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()).map(|n| n.chars().take(200).collect::<String>());
let out_name = new_layer.clone().unwrap_or_else(|| r.name.clone());
let mut out = warp::warp(r, &fit, &warp::Options { crs, points_crs, resampling, cell, name: out_name })?;
let used = warp::Method::Sample(resampling).used_on(r);
report_resampled_classes(s, r, &out, used.id());
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
giswqs marked this conversation as resolved.
Resolved
// Resampled from values rounded when the file was read, some or all of them.
out.keep_rounded_mark(r);
if let Some((fw, fh)) = r.overview {
Expand All @@ -331,7 +333,7 @@ fn warp_to_fit(s: &mut Session, p: &Value, layer: &str, r: &Raster) -> CmdResult
s.notify(note, false);
}
let mut info = json!({
"transform": kind.id(), "crs": crs.label(), "pointsCrs": points_crs.label(), "resampling": resampling.id(),
"transform": kind.id(), "crs": crs.label(), "pointsCrs": points_crs.label(), "resampling": used.id(),
"width": out.width, "height": out.height, "bands": out.band_count(), "cellSize": out.transform.pixel_width,
});
if let Some((fw, fh)) = r.overview {
Expand All @@ -340,10 +342,9 @@ fn warp_to_fit(s: &mut Session, p: &Value, layer: &str, r: &Raster) -> CmdResult
// A file written is the layer's source; without one the pixels are kept in the project.
let rl = match &path {
Some(path) => {
let cog =
rustgis_data::raster::cogwrite::write_file(&out, 0, std::path::Path::new(path)).map_err(|e| format!("{path}: {e}"))?;
let wrote = super::raster::write_and_report(s, &out, 0, true, path)?;
info["path"] = json!(path);
info["cog"] = json!({"overviews": cog.levels.len().saturating_sub(1), "bigTiff": cog.big, "bytes": cog.bytes});
info["cog"] = wrote.get("cog").cloned().unwrap_or(Value::Null);
let e = out.extent();
info["extent"] = json!([e.min_x, e.min_y, e.max_x, e.max_y]);
RasterLayer::from_path(out, path.clone())
Expand Down Expand Up @@ -383,8 +384,10 @@ fn project_raster(s: &mut Session, p: &Value) -> CmdResult {
let path = tif_path(p)?;
let out_name = name(p, format!("{} projected", r.name));
let out = warp::project(&r, &grid, method, out_name.clone())?;
Comment thread
giswqs marked this conversation as resolved.
Resolved
let used = method.used_on(&r);
report_resampled_classes(s, &r, &out, used.id());
let mut info = json!({
"name": out_name, "crs": grid.crs.label(), "resampling": method.used_on(&r).id(), "width": out.width, "height": out.height, "bands": out.band_count(),
"name": out_name, "crs": grid.crs.label(), "resampling": used.id(), "width": out.width, "height": out.height, "bands": out.band_count(),
"cellSize": [grid.cell_x, grid.cell_y], "corner": [grid.min_x, grid.max_y],
});
if let Some((fw, fh)) = r.overview {
Expand All @@ -396,6 +399,31 @@ fn project_raster(s: &mut Session, p: &Value) -> CmdResult {
add_output(s, out, path, false, info)
}

/// Report categorical labels removed by value-mixing resampling, with bounded band numbers.
fn report_resampled_classes(s: &mut Session, source: &Raster, out: &Raster, method: &str) {
let mut count = 0usize;
let mut bands = Vec::new();
for (i, band) in source.bands.iter().enumerate() {
if band.classes.iter().any(|name| !name.is_empty()) && out.bands.get(i).is_some_and(|b| b.classes.is_empty()) {
count += 1;
if bands.len() < 16 {
bands.push((i + 1).to_string());
}
}
}
if count > 0 {
let more = if count > bands.len() { format!(", … ({} more)", count - bands.len()) } else { String::new() };
s.notify(
format!(
"{}: class names of {count} band(s) ({}{more}) not kept under {method}: resampling can mix their values",
source.name,
bands.join(", ")
),
false,
);
}
}

/// The largest whole number a raster's 32-bit float pixels hold exactly.
const EXACT_WHOLE: f64 = 16_777_216.0;

Expand Down Expand Up @@ -607,10 +635,9 @@ fn add_output(s: &mut Session, out: Raster, path: Option<String>, classes: bool,
// A file written is the layer's source; without one the pixels are kept in the project.
let mut rl = match &path {
Some(path) => {
let cog =
rustgis_data::raster::cogwrite::write_file(&out, 0, std::path::Path::new(path)).map_err(|e| format!("{path}: {e}"))?;
let wrote = super::raster::write_and_report(s, &out, 0, true, path)?;
info["path"] = json!(path);
info["cog"] = json!({"overviews": cog.levels.len().saturating_sub(1), "bigTiff": cog.big, "bytes": cog.bytes});
info["cog"] = wrote.get("cog").cloned().unwrap_or(Value::Null);
RasterLayer::from_path(out, path.clone())
}
None => layer_of(out)?,
Expand Down Expand Up @@ -1287,6 +1314,17 @@ fn raster_mosaic(s: &mut Session, p: &Value) -> CmdResult {
o.name = i.name.clone();
o.classes = i.classes.clone();
});
out.copyright = first.copyright.clone();
// The first input's metadata accompanies its pixels; report other credits rather than
// silently dropping them or attributing every input to the first one's owner.
super::raster::say_losses(
s,
rs.iter()
.skip(1)
.filter(|r| r.copyright.is_some() && r.copyright != first.copyright)
.map(|r| (r.name.clone(), "its copyright differs from the first input's and is not kept in the mosaic".to_string()))
.collect(),
);
Comment thread
giswqs marked this conversation as resolved.
Resolved
// Cells rounded when an input was read may be among those the mosaic keeps.
out.keep_rounded_marks(rs.iter().map(|r| &**r));
let other: Vec<&str> = rs
Expand All @@ -1300,8 +1338,7 @@ fn raster_mosaic(s: &mut Session, p: &Value) -> CmdResult {
}
let path = ps(p, "path").map(str::trim).filter(|p| !p.is_empty()).map(str::to_string);
if let Some(path) = &path {
let bytes = rustgis_data::raster::geotiff::write(&out, 0).map_err(|e| e.to_string())?;
rustgis_data::io::write_atomic(std::path::Path::new(path), &bytes).map_err(|e| format!("{path}: {e}"))?;
super::raster::write_and_report(s, &out, 0, false, path)?;
}
let mut v = add(s, out, false)?;
if let (Some(o), Some(path)) = (v.as_object_mut(), path) {
Expand Down
71 changes: 47 additions & 24 deletions crates/engine/src/cmd/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,11 +954,16 @@ fn info(s: &mut Session, p: &Value) -> CmdResult {

/// GeoTIFF bytes of a raster layer (band is 1-based; RGB rasters keep their colour bands; `cog`
/// makes it a cloud-optimised one).
pub fn geotiff_bytes(s: &Session, p: &Value) -> Result<(String, Vec<u8>), String> {
pub fn geotiff_bytes(s: &mut Session, p: &Value) -> Result<(String, Vec<u8>), String> {
let (_, r) = get(s, p)?;
let bi = band(p, "band", &r)?.unwrap_or(0);
let bytes = if b(p, "cog") == Some(true) { raster::cogwrite::write(&r, bi) } else { raster::geotiff::write(&r, bi) };
Ok((r.name.clone(), bytes.map_err(|e| e.to_string())?))
let cog = b(p, "cog") == Some(true);
let bytes = if cog { raster::cogwrite::write(&r, bi) } else { raster::geotiff::write(&r, bi) }.map_err(|e| e.to_string())?;
let note = geotiff_note(&r, bi, cog, "give \"band\" for another")?;
if !note.is_empty() {
s.notify(format!("Exported {}.tif{note}", r.name), false);
Comment thread
giswqs marked this conversation as resolved.
Resolved
}
Ok((r.name.clone(), bytes))
}

/// `share.exportRaster`'s inputs, and the work that writes the raster as a GeoTIFF (a
Expand Down Expand Up @@ -986,6 +991,11 @@ pub(crate) fn export_work(s: &Session, p: &Value) -> Result<super::exports::Work

/// Writes raster `r` (band `bi`, or its colour bands) to `path`, and says what the file holds.
pub(super) fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Result<super::exports::Wrote, String> {
write_geotiff_with_guidance(r, bi, cog, path, "give \"band\" for another")
}

/// Builds the band instruction before combining the note with any caller-supplied path or name.
fn write_geotiff_with_guidance(r: &Raster, bi: usize, cog: bool, path: &str, band_hint: &str) -> Result<super::exports::Wrote, String> {
let cog = if cog {
// Tiles go to the file as they are made: the whole file is never held in memory.
Some(raster::cogwrite::write_file(r, bi, Path::new(path)).map_err(|e| format!("{path}: {e}"))?)
Expand All @@ -994,17 +1004,44 @@ pub(super) fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Res
rustgis_data::io::write_atomic(Path::new(path), &bytes).map_err(|e| format!("{path}: {e}"))?;
None
};
let note = geotiff_note(r, bi, cog.is_some(), band_hint)?;
let mut v = match r.overview {
Some((fw, fh)) => json!({"path": path, "width": r.width, "height": r.height, "overviewOf": [fw, fh]}),
None => json!({"path": path}),
};
if let (Some(o), Some(c)) = (v.as_object_mut(), cog) {
o.insert("cog".into(), json!({"overviews": c.levels.len().saturating_sub(1), "bigTiff": c.big, "bytes": c.bytes}));
}
Ok(super::exports::Wrote {
answer: v,
notes: vec![format!("Exported {path}{note}")],
written_path: Some(path.to_string()),
then: None,
#[cfg(not(target_arch = "wasm32"))]
owner: None,
})
}

/// Writes a tool's raster file and reports the same export notes as `share.exportRaster`.
pub(super) fn write_and_report(s: &mut Session, r: &Raster, bi: usize, cog: bool, path: &str) -> Result<Value, String> {
let wrote =
write_geotiff_with_guidance(r, bi, cog, path, "export the resulting layer with share.exportRaster and give \"band\" for another")?;
for note in wrote.notes {
s.notify(note, false);
}
Ok(wrote.answer)
}

/// What the selected GeoTIFF writer keeps, changes or leaves out, shared by files and downloads.
fn geotiff_note(r: &Raster, bi: usize, cog: bool, band_hint: &str) -> Result<String, String> {
let mut note = match r.overview {
Some((fw, fh)) => format!(" (the {} × {} overview of the {fw} × {fh} raster)", r.width, r.height),
None => String::new(),
};
// Values changed when the file was read are written as they are held: those of the bands the
// writer writes, as it chooses them.
let bands = if cog.is_some() {
raster::cogwrite::written_bands(r, bi).map_err(|e| e.to_string())?
} else {
raster::geotiff::written_bands(r, bi)
};
let bands =
if cog { raster::cogwrite::written_bands(r, bi).map_err(|e| e.to_string())? } else { raster::geotiff::written_bands(r, bi) };
let written: u64 = bands.iter().filter_map(|b| r.rounded.get(*b)).sum();
if written > 0 {
let (values, verb) = values_were(written);
Expand All @@ -1018,7 +1055,7 @@ pub(super) fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Res
// The bands the writer leaves out (it writes `band`, or a colour raster's colour bands, which
// `band` doesn't change) are said, names and all.
if bands.len() < r.bands.len() {
let which = if bands == [bi] { "give \"band\" for another" } else { "its colour bands" };
let which = if bands == [bi] { band_hint } else { "its colour bands" };
note.push_str(&format!("; {} of its {} bands written ({which})", bands.len(), r.bands.len()));
}
// The file keeps the first written band's class names only (as dataset `CLASS_` items).
Expand All @@ -1035,21 +1072,7 @@ pub(super) fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Res
if spaced > 0 {
note.push_str(&format!("; {spaced} band or class name(s) held control characters, written as spaces"));
}
let mut v = match r.overview {
Some((fw, fh)) => json!({"path": path, "width": r.width, "height": r.height, "overviewOf": [fw, fh]}),
None => json!({"path": path}),
};
if let (Some(o), Some(c)) = (v.as_object_mut(), cog) {
o.insert("cog".into(), json!({"overviews": c.levels.len().saturating_sub(1), "bigTiff": c.big, "bytes": c.bytes}));
}
Ok(super::exports::Wrote {
answer: v,
notes: vec![format!("Exported {path}{note}")],
written_path: Some(path.to_string()),
then: None,
#[cfg(not(target_arch = "wasm32"))]
owner: None,
})
Ok(note)
}

/// Pixel values of the topmost visible raster under a longitude/latitude point.
Expand Down
Loading
Loading