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
3 changes: 2 additions & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ And alongside it:
([#995](https://github.com/opengeos/RustGIS/issues/995)); line labels that try other places
along the line (an offset above or below it is done) ([#1187](https://github.com/opengeos/RustGIS/issues/1187)).
- Kernel density (heat map) as a renderer; point clustering.
- QGIS SVG markers brought in by the style importer (PNG, JPEG and SVG pictures are done,
- QGIS SVG markers brought in by the style importer with the colours the style sets (`param(…)`),
or from QGIS's own SVG library (PNG, JPEG and other SVG marker images are done,
[#2266](https://github.com/opengeos/RustGIS/issues/2266)), image fill patterns and a symbol
library.
- A legend and colour bar on the map (layouts have both).
Expand Down
14 changes: 7 additions & 7 deletions crates/engine/src/cmd/qgis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ fn file_dir(p: &Value) -> Option<String> {
})
}

/// The most bytes of base64 of raster marker images one import reads: what a run keeps
/// The most bytes of base64 of raster and SVG marker images one import reads: what a run keeps
/// (`MAX_PICTURES_BYTES`), counting those that turn out not to draw too, so a project naming many
/// files that aren't images reads no more than one of images (in tests, a few small images').
const MARKER_READ_BYTES: usize = if cfg!(test) { 1 << 10 } else { rustgis_map::symbology::MAX_PICTURES_BYTES };

/// The importer's reader of raster image markers' images ([`qgis::ReadImage`]): [`marker_image`]
/// The importer's reader of raster image and SVG markers' images ([`qgis::ReadImage`]): [`marker_image`]
/// within [`MARKER_READ_BYTES`] for the import. On the import's thread, as the rest of it is
/// (`import_project` says why; `import_style` reads on a thread of its own in the desktop app,
/// [`style_ahead`]): each read is one file of at most 1.5 MiB, only a regular file is opened (a
Expand All @@ -84,7 +84,7 @@ fn marker_images() -> impl FnMut(&str) -> Result<(String, f32), &'static str> {
}
}

/// Reads a raster image marker's image for the importer: its `base64:` text, or its file's,
/// Reads a raster image or SVG marker's image for the importer: its `base64:` text, or its file's,
/// within what a picture marker keeps (`symbology::MAX_PICTURE_BYTES` of base64, 2 MiB), checked
/// as `layer.marker` checks an image; with its height over its width. Its base64's length is added
/// to `read`, or for a file that doesn't read (past the limit, or failing part way) what was read
Expand Down Expand Up @@ -113,7 +113,7 @@ fn marker_image(source: &str, read: &mut usize) -> Result<(String, f32), &'stati
// doesn't open).
*read += e.read().div_ceil(3) * 4;
return Err(match e {
PictureFileError::Unopened(_) => "whose image file doesn't open",
PictureFileError::Unopened(_) => qgis::IMAGE_UNOPENED,
PictureFileError::TooLarge(_) => TOO_LARGE,
PictureFileError::Unread(..) => "whose image file fails as it is read",
});
Expand Down Expand Up @@ -152,7 +152,7 @@ fn read_file(path: &str) -> Result<Vec<u8>, String> {
/// wait for its metadata, as Add Data adds one.) Its work is bounded by the project: at most
/// [`qgis::MAX_LAYERS`] layers, each read once by the command that adds it (a file's layers, and
/// a virtual layer on one, each read their file), archives added without waiting for their
/// metadata, annotation pictures within [`MAX_PICTURE_BYTES`], raster image markers' images within
/// metadata, annotation pictures within [`MAX_PICTURE_BYTES`], raster image and SVG markers' images within
/// [`MARKER_READ_BYTES`] (each file once, at most 1,024 of them, before any layer is added: see
/// [`marker_images`]). A local archive's file is looked
/// for on this thread (`is_file`), which a slow network share can hold up, and a web service's
Expand Down Expand Up @@ -2053,7 +2053,7 @@ fn notify_report(s: &mut Session, report: &Report) {
type StyleRead = Result<(qgis::Style, Report), String>;

/// Reads and parses the style file `p` names (or gives) for the layer called `layer`: its raster
/// image markers' relative files start from the style file's folder.
/// and SVG image markers' relative files start from the style file's folder.
fn read_style_file(p: &Value, layer: &str) -> StyleRead {
let text = qgis_text(p)?;
rustgis_data::progress::check().map_err(|e| e.to_string())?;
Expand Down Expand Up @@ -2091,7 +2091,7 @@ pub(crate) fn style_ahead(s: &Session, p: &Value) -> Option<super::reads::ReadWo
/// the style is applied only when they are done, so a cancelled import leaves the layer as it was);
/// run anywhere else (the command line, a script, the browser) it reads here, on the thread that
/// runs it. Its work is bounded by the style: one file of at most [`MAX_FILE`] bytes, its XML
/// parsed within [`MAX_XML`], and its raster image markers' images within [`MARKER_READ_BYTES`],
/// parsed within [`MAX_XML`], and its raster image and SVG markers' images within [`MARKER_READ_BYTES`],
/// each file read once and decoded once to check it (see [`marker_images`] and [`marker_image`]).
/// Cancel is looked at before each image: the file's one read and the XML's one parse don't.
fn import_style(s: &mut Session, p: &Value) -> CmdResult {
Expand Down
46 changes: 45 additions & 1 deletion crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4304,6 +4304,50 @@ fn qgis_raster_image_markers_read_their_files_from_the_style_folder() {
}
}

/// A QGIS style's and project's SVG markers draw their file through the importer's one reader, and
/// saving asks for `svg-markers`; one whose colours the style sets is a circle, noted.
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn qgis_svg_markers_draw_their_file_and_saving_requires_svg_markers() {
use base64::Engine;
use rustgis_map::{Marker, Picture};
let dir = std::env::temp_dir().join(format!("rustgis-qgis-svg-markers-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let svg = r##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 8 4"><rect width="8" height="4" fill="#c81e1e"/></svg>"##;
std::fs::write(dir.join("pin.svg"), svg).unwrap();
std::fs::write(dir.join("tinted.svg"), svg.replace("#c81e1e", "param(fill) #c81e1e")).unwrap();
let qml = |file: &str| {
format!(
r#"<qgis version="4.2.3"><renderer-v2 type="singleSymbol"><symbols><symbol type="marker" name="0" alpha="1">
<layer class="SvgMarker" enabled="1"><Option type="Map"><Option name="name" value="{file}"/><Option name="size" value="6"/>
<Option name="size_unit" value="Point"/></Option></layer></symbol></symbols></renderer-v2></qgis>"#
)
};
let marker = |s: &Session| s.map().layer_by_name("Cities").unwrap().symbol(0).marker;
let notes =
|r: &serde_json::Value| r["notes"].as_array().unwrap().iter().map(|n| n["text"].as_str().unwrap().to_string()).collect::<Vec<_>>();
let b64 = base64::engine::general_purpose::STANDARD.encode(svg);
let path = dir.join("pins.qml");
// From its file beside the style, and embedded in the text with no folder.
for params in
[json!({"layer": "Cities", "path": path.to_string_lossy()}), json!({"layer": "Cities", "text": qml(&format!("base64:{b64}"))})]
{
std::fs::write(&path, qml("pin.svg")).unwrap();
let mut s = world();
let r = s.run("style.importQgis", &params).unwrap();
assert_eq!(marker(&s), Marker::Picture(Picture::new(&b64)), "{params}");
assert!(notes(&r).is_empty(), "{:?}", notes(&r));
assert_eq!(serde_json::to_value(&s.project).unwrap()["requires"], json!(["picture-markers", "svg-markers"]));
}
std::fs::write(&path, qml("tinted.svg")).unwrap();
let mut s = world();
let r = s.run("style.importQgis", &json!({"layer": "Cities", "path": path.to_string_lossy()})).unwrap();
assert_eq!(marker(&s), Marker::Circle);
assert_eq!(notes(&r), ["SVG markers whose colours the style sets, which RustGIS doesn't apply yet, drawn as circles"]);
let _ = std::fs::remove_dir_all(&dir);
}

/// A QGIS style's file and its raster image markers' images are read on a thread before the
/// command lands (#2620): the landing needs neither file, says what the inline import says, can be
/// undone, and a style read for a layer that was renamed meanwhile is refused. A cancelled read
Expand Down Expand Up @@ -6450,7 +6494,7 @@ fn a_qgis_project_opens_as_a_map_and_a_qgis_style_styles_a_layer() {
<maplayer type="raster" hasScaleBasedVisibilityFlag="1" minScale="100000" maxScale="1000"><id>w</id><datasource>url=https://wms.example/wms&amp;layers=a</datasource><layername>Web</layername><provider>wms</provider>
<pipe><rasterrenderer opacity="0.5" type="singlebandcolordata"/></pipe><blendMode>6</blendMode></maplayer></projectlayers></qgis>"#;
let mut report = rustgis_map::qgis::Report::default();
let project = rustgis_map::qgis::read_project(web, "", &mut |_| Err("whose image file doesn't open"), &mut report).unwrap();
let project = rustgis_map::qgis::read_project(web, "", &mut |_| Err(rustgis_map::qgis::IMAGE_UNOPENED), &mut report).unwrap();
let mut w = world();
let before = w.active_layer;
w.active_layer = w.map().layers.iter().map(|l| l.id).find(|id| Some(*id) != before);
Expand Down
22 changes: 13 additions & 9 deletions crates/map/src/qgis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use serde::Serialize;

use crate::Rgba;
use crate::effects::Blend;
use crate::symbology::{Labeling, LineStroke, MarkerRotation, Picture, Renderer};
use crate::symbology::{Labeling, LineStroke, MarkerRotation, Renderer};
pub use annotation::{Annotation, MAX_ITEMS as MAX_ANNOTATIONS};
pub use expr::{reads_measure, to_sql};
use rustgis_data::loss::LossCode;
Expand Down Expand Up @@ -64,18 +64,22 @@ pub struct Report {
/// Where each closing note is in `notes`, kept in step by [`Report::forget`].
#[serde(skip)]
closing: HashMap<(NoteKind, Option<LossCode>), usize>,
/// Raster image markers' images by their `imageFile`, read before the symbols showing them
/// (`symbol::read_images`): each kept picture with its height over its width, or why it isn't
/// drawn. Carried here because every symbol reader already takes the report; passing them
/// beside it would add a parameter to each reader and to each of their callers.
/// Raster image and SVG markers' images by their path (`imageFile`, `name`), read before the
/// symbols showing them (`symbol::read_images`): each kept image, or why it isn't drawn.
/// Carried here because every symbol reader already takes the report; passing them beside it
/// would add a parameter to each reader and to each of their callers.
#[serde(skip)]
images: HashMap<String, Result<(Picture, f32), String>>,
images: HashMap<String, Result<symbol::MarkerImage, String>>,
}

/// Reads a raster image marker's image for the importer, which opens no file itself: given its
/// A [`ReadImage`]'s reason for a file that doesn't open.
pub const IMAGE_UNOPENED: &str = "whose image file doesn't open";

/// Reads a raster image or SVG marker's image for the importer, which opens no file itself: given its
/// `base64:` text or its file's path (made absolute from the project's folder), the image as
/// base64 and its height over its width, or why it isn't drawn, as the end of a note's
/// "raster image markers …, drawn as circles" (such as "whose file doesn't open").
/// "raster image markers …, drawn as circles" or "SVG markers …, drawn as circles" (such as
/// [`IMAGE_UNOPENED`]).
pub type ReadImage<'a> = dyn FnMut(&str) -> Result<(String, f32), &'static str> + 'a;

/// One thing an import couldn't carry over as QGIS has it.
Expand Down Expand Up @@ -569,7 +573,7 @@ pub struct ProjectLayer {
}

/// Reads a QGIS project. `dir` is the folder the project is in, which its relative paths start
/// from; `images` reads its raster image markers' images. Notes on what can't be carried over go
/// from; `images` reads its raster image and SVG markers' images. Notes on what can't be carried over go
/// to `report`.
pub fn read_project(text: &str, dir: &str, images: &mut ReadImage, report: &mut Report) -> Result<Project, String> {
let doc = parse(text, "a QGIS project")?;
Expand Down
Loading
Loading