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
2 changes: 1 addition & 1 deletion crates/data/src/raster/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const MAX_TILES: usize = 4 << 20;
const MAX_TOTAL_TILES: usize = 8 << 20;
/// The largest tile, in bytes once read: each value as the `f32` it is kept as, a 64-bit one as
/// the 8 bytes it is decoded to before that.
const MAX_TILE_BYTES: usize = 32 << 20;
pub(crate) const MAX_TILE_BYTES: usize = 32 << 20;
/// The most bytes held from a file's header (its IFDs and their tag arrays).
pub const MAX_HEADER: u64 = 32 << 20;

Expand Down
577 changes: 466 additions & 111 deletions crates/data/src/raster/cogwrite.rs

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions crates/data/src/raster/geotiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,17 @@ impl Span {
}
}

/// The columns and rows of a chunk (a tile, or a strip of the image's width) of the full image of
/// the GeoTIFF at `path`, as its tags give them; `None` when it can't be read.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn chunk_size(path: &std::path::Path) -> Option<(u32, u32)> {
let file = std::io::BufReader::new(std::fs::File::open(path).ok()?);
let mut dec = Decoder::new(reader(file).ok()?).ok()?.with_limits(Limits::default());
let (w, h) = dec.dimensions().ok()?;
let c = Chunks::new(&mut dec, w, h, 1, false);
Some((c.cw, c.ch))
}

/// How an image is cut into chunks (strips or tiles), and how they're laid out.
#[derive(Clone, Copy)]
struct Chunks {
Expand Down
7 changes: 7 additions & 0 deletions crates/data/src/raster/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,13 @@ pub struct PartRead {
pub full: (u32, u32),
}

/// The columns and rows of a tile of the GeoTIFF at `path` (a strip's: the image's width), which a
/// part read decodes whole ([`read_path_part`]); `None` when they can't be read.
#[cfg(not(target_arch = "wasm32"))]
pub fn chunk_size(path: &Path) -> Option<(u32, u32)> {
geotiff::chunk_size(path)
}

/// `part` of the GeoTIFF at `path`, as [`read_path`] reads the file but holding only that part,
/// at the finest level whose part fits `max` samples: the full image when it does, read from the
/// strips or tiles that hold it alone. The `.aux.xml` beside it is applied as it is to the whole
Expand Down
569 changes: 435 additions & 134 deletions crates/engine/src/cmd/imagery.rs

Large diffs are not rendered by default.

254 changes: 166 additions & 88 deletions crates/engine/src/cmd/in_blocks.rs

Large diffs are not rendered by default.

59 changes: 55 additions & 4 deletions crates/engine/src/tests/tiled_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,16 +651,24 @@ fn raster_functions_and_resamples_run_a_block_at_a_time() {
let want = raster(&whole, "out").data.clone().unwrap();
let said_whole: Vec<String> = said(&whole, mark).into_iter().filter(|n| n.contains("matches no")).collect();
assert_eq!(said_whole.is_empty(), p["function"] != "remap", "{command}: a range of the remap is empty, and said");
// Held, then (one band past the limit) written, a block at a time.
// Held, then (one band past the limit, or for a resample its three bands) written, a block
// at a time.
let resample = *command == "raster.process";
for (past, written) in [(1_000_000, false), (900_000, !resample)] {
for (past, written) in [(1_000_000, false), (if resample { 300_000 } else { 900_000 }, true)] {
let why = format!("{command} {p}, written {written}");
let (_held, _blocks) = (HeldWhole::at(1_000_000), BlocksPast::at(past));
let mut s = Session::new(Project::default());
s.path = Some(d.join("Survey.rgis"));
open(&mut s);
assert!(raster(&s, "checks").tiled);
let (before, mark) = (reads(), s.notice_mark());
// The Assistant asks before a run that writes a file, and only then.
let scope = crate::policy::Scope::assistant(8, std::sync::Arc::new(Live));
match s.review(&scope, &crate::policy::Call::command(command, p.clone())) {
crate::policy::Outcome::NeedsApproval(_) => assert!(written, "{why}: the Assistant asks only before a file is written"),
crate::policy::Outcome::Allowed => assert!(!written, "{why}: the Assistant asks before a file is written"),
crate::policy::Outcome::Refused(e) => panic!("{why}: refused: {e}"),
}
let got = s.run(command, p).unwrap();
assert_eq!(reads(), before, "{why}: the file isn't read whole");
let rl = raster(&s, "out");
Expand All @@ -670,12 +678,27 @@ fn raster_functions_and_resamples_run_a_block_at_a_time() {
} else {
rl.data.as_deref().unwrap().clone()
};
// A resample's file holds its means as floats.
let kind = if resample && written { "Float32" } else { want.data_type.as_str() };
assert_eq!(
(got_raster.width, got_raster.height, got_raster.transform, got_raster.nodata, got_raster.data_type.as_str()),
(want.width, want.height, want.transform, want.nodata, want.data_type.as_str()),
(
got_raster.width,
got_raster.height,
got_raster.transform,
got_raster.nodata,
got_raster.data_type.as_str(),
&got_raster.color
),
(want.width, want.height, want.transform, want.nodata, kind, &want.color),
"{why}"
);
assert_eq!(got_raster.bands.len(), want.bands.len(), "{why}: its bands");
// A written resample of bytes is drawn on their 0 to 255, as the held one is, and of an
// image without alpha says nothing of a no-data value.
if resample && written {
assert!(!said(&s, mark).iter().any(|n| n.contains("isn't written to its file")), "{why}");
assert_eq!(raster(&s, "out").style.stretch, rustgis_map::Stretch::Custom { min: 0.0, max: 255.0, bands: None }, "{why}");
}
for (g, w) in got_raster.bands.iter().zip(&want.bands) {
assert!(g.data == w.data, "{why}: band {} is the whole raster's", w.name);
assert_eq!(g.name, w.name, "{why}");
Expand Down Expand Up @@ -781,9 +804,37 @@ fn a_resampled_pixel_with_no_value_takes_the_no_data_value() {
let mut s = Session::new(Project::default());
s.run("layer.addData", &json!({"path": path.display().to_string()})).unwrap();
assert_eq!(raster(&s, "gaps").tiled, in_blocks);
let mark = s.notice_mark();
s.run("raster.process", &p).unwrap();
let out = raster(&s, "out").data.clone().unwrap();
assert_eq!((out.width, out.nodata), (N / 2, Some(-9999.0)), "in blocks {in_blocks}");
assert_eq!(out.bands[0].data[..2], [-9999.0, 6.0], "in blocks {in_blocks}");
assert!(!said(&s, mark).iter().any(|n| n.contains("colour table")), "in blocks {in_blocks}: no palette, nothing said");
}
}

/// A palette raster's resample says its colour table isn't kept (its indexes are averaged as grey
/// values), whether the file is held whole or read a block at a time.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_resampled_palette_says_its_colours_go() {
let d = dir("resample-palette");
const N: u32 = 600;
let band: Vec<f32> = (0..N * N).map(|i| (i % 4) as f32).collect();
let gt = GeoTransform { origin_x: 10.0, origin_y: 46.0, pixel_width: 0.001, pixel_height: -0.001 };
let colours = (0..256u32).map(|i| [i as u8, 0, 255 - i as u8, 255]).collect();
let r = Raster::new("classes", N, N, vec![band], gt, Crs::LonLat, None, ColorHint::Palette(colours), "UInt8").unwrap();
let path = d.join("classes.tif");
rustgis_data::raster::cogwrite::write_file(&r, 0, &path).unwrap();
let p = json!({"layer": "classes", "operation": "resample", "factor": 2, "name": "out"});
for in_blocks in [false, true] {
let (_held, _blocks) = (HeldWhole::at(if in_blocks { 1000 } else { 1_000_000 }), BlocksPast::at(100_000));
let mut s = Session::new(Project::default());
s.run("layer.addData", &json!({"path": path.display().to_string()})).unwrap();
assert_eq!(raster(&s, "classes").tiled, in_blocks);
let mark = s.notice_mark();
s.run("raster.process", &p).unwrap();
let notes: Vec<String> = said(&s, mark).into_iter().filter(|n| n.contains("colour table isn't kept")).collect();
assert_eq!(notes.len(), 1, "in blocks {in_blocks}: {notes:?}");
}
}
13 changes: 7 additions & 6 deletions docs/user-guide/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -1691,14 +1691,15 @@ at the file's full resolution when that part fits. **Indices**, **Raster Functio
NDVI, remap, threshold, slope, aspect and hillshade, those over a cell's neighbours reading each
block with the cells around it), the **Function Editor** with those steps, and **Classification
Tools** (each class's mean summed from the rows of the file under the training polygons, as
from the whole raster) work a block of tiles at a time at the file's full resolution. A result
of up to 120 million pixels is held, as any other; a larger one is written to a tiled GeoTIFF in
from the whole raster) work a block of tiles at a time at the file's full resolution, as does
**Process**'s resample. A result of up to 120 million pixels is held, as any other (a
resample's, 120 million values of all its bands); a larger one is written to a tiled GeoTIFF in
the project's results folder beside it (`Survey.rgis.data` for `Survey.rgis`), or in RustGIS's
own folder for a project not saved yet, and the new layer reads it in place; a notice says where
it is. **Process**'s resample is made from the file a block at a time when its result fits. A
chain that remaps or thresholds after a step over neighbours, or has a slope in percent before
its last step, the other commands, and a larger resample read the finest level of the file they
can hold, and say which.
it is. A resample's file holds its means as 32-bit floats, every band. A chain that remaps or
thresholds after a step over neighbours, or has a slope in percent before its last step, a
resample the computer can't give the memory for, one of more than 32 bands too large to hold,
and the other commands read the finest level of the file they can hold, and say which.

The web app opens a picked or dropped file of up to 256 MB whole. A larger GeoTIFF is read a part
at a time instead: its header, then only the tiles of the overview it opens from. A
Expand Down
Loading