From bfd3338e8ece1f2289971708fd83c74eeec5c14a Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Tue, 1 Dec 2026 09:23:39 -0300 Subject: [PATCH 1/2] feat(engine): cut a PDF group's name, say ignored options, document PDF layers Part 3 of #3442. The group a PDF's layers open in is named like a layer (at most MAX_LAYER_NAME characters, cut with an ellipsis and said), the options that don't apply to the file are said with the group's notices, the PDF layer test cleans its directory through a drop guard, and the user guide describes PDF layers and "pdfLayers". Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/layer.rs | 2 +- crates/engine/src/cmd/raster.rs | 16 ++++++++++++---- crates/engine/src/tests.rs | 29 ++++++++++++++++++++++++++--- docs/user-guide/data-formats.md | 2 +- docs/user-guide/data.md | 2 +- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index 11235b387e..2e0256d0e8 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -21,7 +21,7 @@ pub const MAX_LAYER_NAME: usize = 200; pub fn commands() -> Vec { vec![ C::new("layer.addData", "Add Data", "Home › Add", add_data).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) - .params(r#"{"path": string, "name"?: string, "layer"?: "GeoPackage, GDAL, DuckDB or DXF layer, or a PDF's page number (the first when left out or blank)", "pdfLayers"?: "bool: a PDF page with layers (optional content groups) as a group of them, one raster each, true by default; false opens the page as one picture", "paperSpace"?: "bool: a DXF's layout entities too", "blocks"?: "bool: a DXF's block (图块) contents, true by default", "stream"?: "bool: true links the file instead of copying it (read in place, a view at a time; the project keeps its path, not its features), false copies it into the project; left out, decided by its size or feature count", "as"?: "scan" (a TIFF, PNG, JPEG or PDF page, even a photo, a GeoTIFF or a GeoPDF, as a picture with no coordinates), "crs"?: "EPSG:… (or WKT) for Shapefile coordinates (overrides .prj), a CSV, a DXF (which never says), a CityJSON or CityGML model (it replaces the system a model file names; in a zip, it is used for a model naming none RustGIS knows), or a GDAL or DuckDB file that doesn't say (GDAL takes an authority code such as EPSG:2263 or the exact names WGS84, WGS72, NAD27, NAD83, not WKT or a file)", "x"?: "CSV or workbook x/longitude column (in a zip, for each CSV and workbook member that has it)", "y"?: "CSV or workbook y/latitude column (as x)", "wkt"?: "CSV or workbook WKT geometry column (as x)", "delimiter"?: ","|";"|"\\t"|"|"}"#), + .params(r#"{"path": string, "name"?: "string; for a PDF opened as its layers, the group's (its layers keep the PDF's names)", "layer"?: "GeoPackage, GDAL, DuckDB or DXF layer, or a PDF's page number (the first when left out or blank)", "pdfLayers"?: "bool: a PDF page with layers (optional content groups) as a group of them, one raster each, true by default; false opens the page as one picture", "paperSpace"?: "bool: a DXF's layout entities too", "blocks"?: "bool: a DXF's block (图块) contents, true by default", "stream"?: "bool: true links the file instead of copying it (read in place, a view at a time; the project keeps its path, not its features), false copies it into the project; left out, decided by its size or feature count", "as"?: "scan" (a TIFF, PNG, JPEG or PDF page, even a photo, a GeoTIFF or a GeoPDF, as a picture with no coordinates), "crs"?: "EPSG:… (or WKT) for Shapefile coordinates (overrides .prj), a CSV, a DXF (which never says), a CityJSON or CityGML model (it replaces the system a model file names; in a zip, it is used for a model naming none RustGIS knows), or a GDAL or DuckDB file that doesn't say (GDAL takes an authority code such as EPSG:2263 or the exact names WGS84, WGS72, NAD27, NAD83, not WKT or a file)", "x"?: "CSV or workbook x/longitude column (in a zip, for each CSV and workbook member that has it)", "y"?: "CSV or workbook y/latitude column (as x)", "wkt"?: "CSV or workbook WKT geometry column (as x)", "delimiter"?: ","|";"|"\\t"|"|"}"#), C::new("layer.addPhotos", "Add Photos", "Home › Add", add_photos).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r#"{"paths"?: [JPEG photo files], "folder"?: "a folder of photos (every JPEG in it)", "name"?: "the layer (default Photos; photos join it when it exists)"}"#, ), diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 41c9d08500..3cce61fb46 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -671,7 +671,7 @@ pub(crate) fn pdf_key(path: &str, page: Option<&str>) -> String { /// has none, saying why when they couldn't be read. pub(crate) fn add_pdf(s: &mut Session, path: &str, name: Option<&str>, page: Option<&str>, ignored: Option) -> CmdResult { let (lost, picture) = match super::reads::take::(s, &pdf_key(path, page)).unwrap_or_else(|| read_pdf(path, page)) { - (Ok(all), None) => return add_pdf_layers(s, path, name, all), + (Ok(all), None) => return add_pdf_layers(s, path, name, all, ignored), (Ok(all), Some(picture)) => (all.lost, picture), (Err(e), Some(picture)) => (vec![format!("{e}: the page is read as one picture")].into(), picture), (Err(e), None) => return Err(format!("{path}: {e}")), @@ -684,13 +684,21 @@ pub(crate) fn add_pdf(s: &mut Session, path: &str, name: Option<&str>, page: Opt } /// Adds a PDF page's layers as a group named `name` or after the file: the PDF's first on top, -/// each shown or hidden as the PDF opens it, and what was read of them said. -fn add_pdf_layers(s: &mut Session, path: &str, name: Option<&str>, all: raster::PdfLayers) -> CmdResult { +/// each shown or hidden as the PDF opens it, and what was read of them said, with `ignored` (the +/// options given that don't apply to it). +fn add_pdf_layers(s: &mut Session, path: &str, name: Option<&str>, mut all: raster::PdfLayers, ignored: Option) -> CmdResult { if s.map().groups.len() >= rustgis_map::MAX_GROUPS { return Err(format!("a map holds at most {} groups", rustgis_map::MAX_GROUPS)); } let stem = Path::new(path).file_stem().and_then(|s| s.to_str()).unwrap_or("PDF"); - let title = name.unwrap_or(stem).to_string(); + // Cut as a layer's name would be (layer.rename takes at most MAX_LAYER_NAME), and said. + let mut title = name.unwrap_or(stem).to_string(); + let most = super::layer::MAX_LAYER_NAME; + if title.chars().count() > most { + title = format!("{}\u{2026}", title.chars().take(most - 1).collect::()); + all.lost.push(format!("the group's name is cut to {most} characters")); + } + all.lost.extend(ignored); let full = std::path::absolute(path).map(|p| p.display().to_string()).unwrap_or_else(|_| path.to_string()); let names: Vec = all.layers.iter().map(|l| l.name.clone()).collect(); let mut ids = Vec::with_capacity(names.len()); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 754f3119e2..4ab024f039 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -56609,8 +56609,16 @@ fn a_pdf_with_layers_is_added_as_a_group_of_them() { let mut s = world(); s.run("layout.new", &json!({"page": "a4", "orientation": "landscape", "template": "map"})).unwrap(); let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos()); - let dir = std::env::temp_dir().join(format!("rustgis-pdf-layers-{}-{nanos}", std::process::id())); - std::fs::create_dir_all(&dir).unwrap(); + // Removed when the test ends, though an assertion fails. + struct Dir(std::path::PathBuf); + impl Drop for Dir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + let dir = Dir(std::env::temp_dir().join(format!("rustgis-pdf-layers-{}-{nanos}", std::process::id()))); + std::fs::create_dir_all(&dir.0).unwrap(); + let dir = &dir.0; let path = dir.join("camadas.pdf"); s.run("layout.export", &json!({"path": path, "dpi": 36})).unwrap(); let mut t = Session::new(Project::default()); @@ -56732,7 +56740,22 @@ fn a_pdf_with_layers_is_added_as_a_group_of_them() { probe.1.load(std::sync::atomic::Ordering::Relaxed) }; assert_eq!(looks(json!({"path": picture})), looks(json!({"path": picture, "pdfLayers": false}))); - let _ = std::fs::remove_dir_all(&dir); + // A group's name is cut as a layer's is (layer.rename takes at most MAX_LAYER_NAME), and said. + let most = crate::cmd::layer::MAX_LAYER_NAME; + let mut w = Session::new(Project::default()); + let mark = w.notice_mark(); + let r = w.run("layer.addData", &json!({"path": path, "name": "g".repeat(most + 50)})).unwrap(); + let group = w.map().groups.iter().find(|g| Some(u64::from(g.id)) == r["group"].as_u64()).unwrap(); + assert_eq!(group.name.chars().count(), most, "{}", group.name); + assert!(group.name.ends_with('\u{2026}') && group.name.starts_with("ggg"), "{}", group.name); + let said: Vec = w.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert!(said.iter().any(|t| t.contains(&format!("the group's name is cut to {most} characters"))), "{said:?}"); + // What the options given don't apply to is said with the group's notices, as for one picture. + let mut w = Session::new(Project::default()); + let mark = w.notice_mark(); + crate::cmd::raster::add_pdf(&mut w, path.to_str().unwrap(), None, None, Some("the \"crs\" given is ignored".into())).unwrap(); + let said: Vec = w.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert!(said.iter().any(|t| t.starts_with("camadas") && t.contains("the \"crs\" given is ignored")), "{said:?}"); } /// A frame drawn over another (an inset on the main map) is cut out of the other's viewport, so diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index d31d34fdb8..9cce8358e7 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -697,7 +697,7 @@ databases](data.md#sql-queries-and-duckdb-databases). | --- | --- | --- | --- | | GeoTIFF / Cloud-optimised GeoTIFF | `.tif`, `.tiff` | Add Data, or upload | Regular or cloud-optimised, striped or tiled; grey, RGB(A), palette (with its colour table) or multiband; Deflate, LZW, PackBits, JPEG, Zstandard or LERC compression (LERC alone or wrapped in Deflate or Zstandard, as GDAL writes it, in the browser too). Supported EPSG systems or the file's own GeoKey projection are reprojected as it draws. Rotated or skewed images are resampled north-up. Large files open from an overview, or one made while reading. A GDAL internal transparency mask (a 1-bit image in its own IFD, as written for JPEG/YCbCr RGB) is read as transparency: masked pixels are see-through, as no-data is, using the mask of the image or overview opened; one that can't be read is said, and the image still opens. Tiles read in place from a cloud-optimised file don't apply the mask, and say so; external `.msk` sidecars aren't read. Browser uploads over 256 MiB read only the parts needed for a fitting overview, provided those parts fit the upload limit; the project keeps those parts, not the full-resolution file. | | JPEG 2000 | `.jp2`, `.j2k` | Add Data, byte upload (`layer.addDataBytes`), or From URL… (desktop, with [GDAL](https://gdal.org) installed) | Converted through GDAL. Uploads and downloads keep the converted GeoTIFF in the project, so reopening it needs neither GDAL nor the original file or address; their output and workspace limits are described under [Files on the web](#files-on-the-web). The web app rejects JPEG 2000 uploads and downloads with an explicit GDAL limitation: use GeoTIFF instead. | -| PDF and GeoPDF | `.pdf` | Add Data, upload or From URL… (desktop, command line and Python; the browser and phones say to open it there) | A page drawn as a picture at 150 dots an inch (less when that wouldn't fit in memory, and a notice says so). A GeoPDF page is placed by its map frame's coordinates (ISO 32000 viewports, as layouts export them), which run across the whole page, its title and legend included; a turned frame is resampled north up. A page with several map frames is placed by the largest, the others counted in a notice. A page without coordinates opens as a scan to georeference, as does any page with `"as": "scan"`. A world file beside a PDF isn't read: only its own coordinates place it. The first page is added and the others named in a notice; `"layer": "2"` adds page 2, which a project keeps. Encrypted PDFs aren't read. | +| PDF and GeoPDF | `.pdf` | Add Data, upload or From URL… (desktop, command line and Python; the browser and phones say to open it there) | A page drawn as a picture at 150 dots an inch (less when that wouldn't fit in memory, and a notice says so). A GeoPDF page is placed by its map frame's coordinates (ISO 32000 viewports, as layouts export them), which run across the whole page, its title and legend included; a turned frame is resampled north up. A page with several map frames is placed by the largest, the others counted in a notice. A page without coordinates opens as a scan to georeference, as does any page with `"as": "scan"`. A world file beside a PDF isn't read: only its own coordinates place it. The first page is added and the others named in a notice; `"layer": "2"` adds page 2, which a project keeps. A page with PDF layers (optional content groups) added by its path opens as a group of them, named after the file or `"name"`: one raster each (red, green, blue and alpha), the PDF's first on top, each shown or hidden as the PDF opens it, and **Not in a PDF layer** for what is in none. A layer nested in another is named after it (`Mapa / Rios`) and lies over it. Each layer holds what changes on the page when it is turned on, so where it draws the very colour already under it, that part stays with the layer under it. The first 64 are layers of their own, and the others are drawn in **Not in a PDF layer** when the PDF shows them; a layer that draws nothing on the page is left out, and a notice names it. A project opens each layer again by its page and name. `"pdfLayers": false` opens the page as one picture, as an uploaded or downloaded PDF opens for now. Encrypted PDFs aren't read. | | Scans with no coordinates | an untagged `.tif` or `.png`, or a PDF page without coordinates; any `.tif`, `.png`, `.jpg`/`.jpeg` or `.pdf` with `"as": "scan"` | Add Data, or upload (`layer.addData`, `layer.addDataBytes`) | Grey or RGB(A), 8- or 16-bit pixels. An untagged TIFF or PNG opens as a scan by default; a JPEG needs `"as": "scan"` (otherwise it is a photo). `"as": "scan"` also discards a GeoTIFF's coordinates. A world file beside a TIFF, PNG or JPEG (`.tfw`, `.jgw`, `.pgw`, the four-letter `.tifw`, `.jpgw`, `.pngw`, or `.wld`), with a `.prj` beside it for the coordinate system, places it instead (a JPEG too, which is then not a photo): a turned or skewed one is resampled north up; without a `.prj`, with one RustGIS can't use, or with a world file that can't be read or places the picture nowhere it can be drawn (UTM meters with a longitude/latitude `.prj`), it stays a scan and a notice says why. `"as": "scan"` passes the world file over. World files are read for files added by their path (an upload doesn't bring the files beside it). A project reopens a picture as it was added: one that opened as a scan stays one (add it again once its `.prj` is there), and one a world file placed still opens if its `.prj` or world file is gone, at a stand-in place, with a notice saying it has no coordinates now. A world file is compared with a GeoTIFF's own tags only when both are north up (a turned GeoTIFF is resampled as it is read). A GeoTIFF keeps its own tags, and a notice says when a world file places it differently. The scan initially covers the current view and is marked not georeferenced (`georeferenced: false`) until **Georeference** places it. Export and tools needing ground coordinates refuse it until then. PNG/JPEG decoding is bounded and may reduce the image; large TIFFs can be read at reduced resolution. | | ASCII grid | `.asc` (with an optional `.prj` on desktop) | Add Data, or upload | A single numeric band with square `CELLSIZE` or separate `DX` and `DY` pixel sizes. Desktop files use the neighbouring `.prj`; uploaded and downloaded grids have no sidecar and must have coordinates that fit WGS 84 longitude/latitude. A grid with no `.prj` is read as WGS 84, and a notice says so. | | Zarr | a store's folder (`.zarr`, desktop) or web address (desktop and browser), format 2 (consolidated) or 3 | Home › Add › Add Data › Zarr… (`layer.addZarr`), Add Data on a local folder, or a `?zarr=` link | A variable's latitude/longitude (or y/x) map at the steps picked of its other dimensions (time, level), read chunk by chunk in pure Rust (blosc, zlib, gzip, zstd, LZ4; sharded arrays too). See [Zarr stores](data.md#zarr-stores). | diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index b9043e15fa..982b964ee8 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -29,7 +29,7 @@ Formats](data-formats.md). | GRIB editions 1 and 2, simple packing and GRIB2's complex packing on latitude/longitude grids (`.grb`, `.grib`, `.grb2`, `.grib2`) | Yes | No | | SRTM height tiles (`.hgt`) and DTED (`.dt0`–`.dt2`) | Yes | No | | Unreferenced TIFF and PNG scans; JPEG with `"as": "scan"` | Yes | GeoTIFF after georeferencing | -| PDF pages: GeoPDFs placed by their coordinates, other pages as scans | Yes (desktop, command line and Python) | GeoTIFF after georeferencing | +| PDF pages: GeoPDFs placed by their coordinates, other pages as scans; a page with PDF layers as a group of them | Yes (desktop, command line and Python) | GeoTIFF after georeferencing | | Through GDAL: JPEG 2000, ERDAS Imagine, NetCDF, HDF, ENVI, SAGA… | Yes (desktop, when GDAL is installed) | No | | Web tiles: XYZ/TMS/WMS rasters, Mapbox Vector Tiles, MapLibre styles, PMTiles and MBTiles archives | Yes | Yes (Imagery › Create Hosted Imagery) | | RustGIS project (`.rgis`; older `.rgisx` files open too) | Yes | Yes | From 78bb0a94451ec40e3db618c9c228248f1400d413 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Tue, 1 Dec 2026 09:49:19 -0300 Subject: [PATCH 2/2] test(engine): clean cases for the PDF group's name and ignored notes; say which PDFs open as a group Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/raster.rs | 2 +- crates/engine/src/tests.rs | 15 ++++++++++++++- docs/user-guide/data.md | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 3cce61fb46..f99a9ab1d7 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -691,7 +691,7 @@ fn add_pdf_layers(s: &mut Session, path: &str, name: Option<&str>, mut all: rast return Err(format!("a map holds at most {} groups", rustgis_map::MAX_GROUPS)); } let stem = Path::new(path).file_stem().and_then(|s| s.to_str()).unwrap_or("PDF"); - // Cut as a layer's name would be (layer.rename takes at most MAX_LAYER_NAME), and said. + // Cut to MAX_LAYER_NAME characters and said. layer.rename refuses a longer name instead (#1817). let mut title = name.unwrap_or(stem).to_string(); let most = super::layer::MAX_LAYER_NAME; if title.chars().count() > most { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 4ab024f039..41dbf6e9ff 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -56740,10 +56740,17 @@ fn a_pdf_with_layers_is_added_as_a_group_of_them() { probe.1.load(std::sync::atomic::Ordering::Relaxed) }; assert_eq!(looks(json!({"path": picture})), looks(json!({"path": picture, "pdfLayers": false}))); - // A group's name is cut as a layer's is (layer.rename takes at most MAX_LAYER_NAME), and said. + // A group's name past MAX_LAYER_NAME characters is cut, and said; one of that many is kept whole. let most = crate::cmd::layer::MAX_LAYER_NAME; let mut w = Session::new(Project::default()); let mark = w.notice_mark(); + let r = w.run("layer.addData", &json!({"path": path, "name": "h".repeat(most)})).unwrap(); + let group = w.map().groups.iter().find(|g| Some(u64::from(g.id)) == r["group"].as_u64()).unwrap(); + assert_eq!(group.name, "h".repeat(most)); + let said: Vec = w.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert!(!said.iter().any(|t| t.contains("name is cut")), "{said:?}"); + let mut w = Session::new(Project::default()); + let mark = w.notice_mark(); let r = w.run("layer.addData", &json!({"path": path, "name": "g".repeat(most + 50)})).unwrap(); let group = w.map().groups.iter().find(|g| Some(u64::from(g.id)) == r["group"].as_u64()).unwrap(); assert_eq!(group.name.chars().count(), most, "{}", group.name); @@ -56756,6 +56763,12 @@ fn a_pdf_with_layers_is_added_as_a_group_of_them() { crate::cmd::raster::add_pdf(&mut w, path.to_str().unwrap(), None, None, Some("the \"crs\" given is ignored".into())).unwrap(); let said: Vec = w.notices_since(mark).iter().map(|n| n.text.clone()).collect(); assert!(said.iter().any(|t| t.starts_with("camadas") && t.contains("the \"crs\" given is ignored")), "{said:?}"); + // Nothing ignored, nothing said of it. + let mut w = Session::new(Project::default()); + let mark = w.notice_mark(); + crate::cmd::raster::add_pdf(&mut w, path.to_str().unwrap(), None, None, None).unwrap(); + let said: Vec = w.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert!(!said.iter().any(|t| t.contains("ignored")), "{said:?}"); } /// A frame drawn over another (an inset on the main map) is cut out of the other's viewport, so diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 982b964ee8..cade556269 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -29,7 +29,7 @@ Formats](data-formats.md). | GRIB editions 1 and 2, simple packing and GRIB2's complex packing on latitude/longitude grids (`.grb`, `.grib`, `.grb2`, `.grib2`) | Yes | No | | SRTM height tiles (`.hgt`) and DTED (`.dt0`–`.dt2`) | Yes | No | | Unreferenced TIFF and PNG scans; JPEG with `"as": "scan"` | Yes | GeoTIFF after georeferencing | -| PDF pages: GeoPDFs placed by their coordinates, other pages as scans; a page with PDF layers as a group of them | Yes (desktop, command line and Python) | GeoTIFF after georeferencing | +| PDF pages: GeoPDFs placed by their coordinates, other pages as scans; a page with PDF layers opened by its path as a group of them (uploaded or downloaded, still as one picture) | Yes (desktop, command line and Python) | GeoTIFF after georeferencing | | Through GDAL: JPEG 2000, ERDAS Imagine, NetCDF, HDF, ENVI, SAGA… | Yes (desktop, when GDAL is installed) | No | | Web tiles: XYZ/TMS/WMS rasters, Mapbox Vector Tiles, MapLibre styles, PMTiles and MBTiles archives | Yes | Yes (Imagery › Create Hosted Imagery) | | RustGIS project (`.rgis`; older `.rgisx` files open too) | Yes | Yes |