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
83 changes: 66 additions & 17 deletions crates/engine/src/cmd/qgis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! `style.importQgis` gives a layer a `.qml` style. Both say what they couldn't carry over
//! (`rustgis_map::qgis::Report`).

use rustgis_data::loss::LossCode;
use rustgis_geom::{Coord, Extent};
use rustgis_map::qgis::{self, NoteKind, Report, Source, listed};
use serde_json::{Value, json};
Expand Down Expand Up @@ -268,7 +269,10 @@ fn import_project(s: &mut Session, p: &Value) -> CmdResult {
}
}
Ok(None) => {}
Err(e) => report.skipped(&layer.name, format!("the layer: {e}")),
// A layer whose data wasn't read is data the import left out; an annotation layer is
// drawing only.
Err(e) if layer.kind == "annotation" => report.skipped(&layer.name, format!("the layer: {e}")),
Err(e) => report.lost(&layer.name, NoteKind::Skipped, LossCode::DroppedFeature, format!("the layer: {e}")),
}
}
note_texts_under_layers_above(&drawn, &mut report);
Expand Down Expand Up @@ -427,12 +431,22 @@ fn add_layer(
let name = layer.name.as_str();
if let Source::File { path, .. } | Source::Csv { path, .. } | Source::Archive { path, .. } = &layer.source {
if !has_dir && qgis::is_relative(path) {
report.skipped(name, format!("the file {path}, relative to the project's folder, which the import wasn't given (pass dir)"));
report.lost(
name,
NoteKind::Skipped,
LossCode::DroppedFeature,
format!("the file {path}, relative to the project's folder, which the import wasn't given (pass dir)"),
);
return Ok(None);
}
// `C:roads.gpkg`: relative to the working directory on drive C, which no folder fixes.
if qgis::is_drive_relative(path) {
report.skipped(name, format!("the file {path}, relative to a drive's working directory (give a full path in QGIS)"));
report.lost(
name,
NoteKind::Skipped,
LossCode::DroppedFeature,
format!("the file {path}, relative to a drive's working directory (give a full path in QGIS)"),
);
return Ok(None);
}
}
Expand Down Expand Up @@ -487,8 +501,10 @@ fn add_layer(
let share = path.starts_with("file://") || path.starts_with("\\\\") || path.starts_with("//");
if share || (*kind == "mbtiles" && path.contains("://")) {
let at = if share { "on a network share" } else { "at a web address" };
report.skipped(
report.lost(
name,
NoteKind::Skipped,
LossCode::DroppedFeature,
format!(
"the tile archive {at}: RustGIS reads MBTiles files on this computer, and PMTiles archives there or on the web"
),
Expand All @@ -499,13 +515,17 @@ fn add_layer(
// no file by its path.
#[cfg(not(target_arch = "wasm32"))]
if !path.contains("://") && !std::path::Path::new(path).is_file() {
report.skipped(name, format!("the tile archive {path}: no file there"));
report.lost(name, NoteKind::Skipped, LossCode::DroppedFeature, format!("the tile archive {path}: no file there"));
return Ok(None);
}
#[cfg(target_arch = "wasm32")]
if !path.contains("://") {
report
.skipped(name, format!("the tile archive {path}, a file the browser doesn't read by its path (add it with Add Data)"));
report.lost(
name,
NoteKind::Skipped,
LossCode::DroppedFeature,
format!("the tile archive {path}, a file the browser doesn't read by its path (add it with Add Data)"),
);
return Ok(None);
}
let before = s.active_layer;
Expand Down Expand Up @@ -539,7 +559,8 @@ fn add_layer(
return Ok(Some(name.to_string()));
}
Source::Memory { geometry, crs, fields, as_text: (as_text, texts), cut, unnamed, taken, zm, curved, unknown_geometry } => {
// QGIS keeps none of a memory layer's features: it opens empty, with its fields.
// QGIS keeps none of a memory layer's features: it opens empty, with its fields, so
// what these notes leave out held no data, and they stay plain.
let mut ds = rustgis_data::Dataset::new(name, *geometry, fields.clone());
if let Some(c) = layer.crs.as_deref().or(crs.as_deref()) {
match rustgis_data::raster::crs::Crs::parse(c) {
Expand Down Expand Up @@ -589,7 +610,7 @@ fn add_layer(
}
None => "RustGIS reads one that selects every column of one layer, with or without a WHERE clause, as that layer".into(),
};
report.skipped(name, format!("the virtual layer `{shown}{more}`: {why}"));
report.lost(name, NoteKind::Skipped, LossCode::DroppedFeature, format!("the virtual layer `{shown}{more}`: {why}"));
return Ok(None);
}
Source::Wms { url, layer: wms, wmts, options, .. } => {
Expand Down Expand Up @@ -709,7 +730,13 @@ fn add_layer(
Source::Other { provider } => {
// A layer of no provider (an annotation layer, a group layer) is told by its kind.
let what = if provider.is_empty() { format!("{} layers", layer.kind) } else { format!("{provider} sources") };
report.skipped(name, format!("the layer: RustGIS doesn't read QGIS's {what}"));
// An annotation or group layer holds no data; any other layer's data is left out,
// with or without a provider named.
if matches!(layer.kind.as_str(), "annotation" | "group") {
report.skipped(name, format!("the layer: RustGIS doesn't read QGIS's {what}"));
} else {
report.lost(name, NoteKind::Skipped, LossCode::DroppedFeature, format!("the layer: RustGIS doesn't read QGIS's {what}"));
}
return Ok(None);
}
}
Expand Down Expand Up @@ -841,7 +868,7 @@ fn database_note(
format!("the {what} table {table} ({connection}): connect to the database to add it{refused}{}", left("the connection text"))
}
};
report.skipped(name, text);
report.lost(name, NoteKind::Skipped, LossCode::DroppedFeature, text);
}

/// The name of the saved connection to the database `connection` names (QGIS's `key=value`
Expand Down Expand Up @@ -1931,7 +1958,11 @@ impl Geographic {
}

fn note_json(n: &qgis::Note) -> Value {
json!({"layer": n.layer, "kind": n.kind, "text": n.text, "count": n.count})
let mut note = json!({"layer": n.layer, "kind": n.kind, "text": n.text, "count": n.count});
if let (Some(code), Some(note)) = (n.loss, note.as_object_mut()) {
note.insert("loss".into(), json!(code.spec().id));
}
note
}

/// The returned report serves CLI and Python callers; the app also needs every coalesced
Expand All @@ -1945,15 +1976,33 @@ fn notify_report(s: &mut Session, report: &Report) {
}
text
};
// A notice is about one kind of note and one loss code, so a note that reports a loss never
// shares a notice with a plain one (or with another code's): plain notes first, then each
// code in the order its first note came.
let mut groups: Vec<(NoteKind, Option<LossCode>)> = Vec::new();
for kind in [NoteKind::Approximated, NoteKind::Skipped] {
let notes: Vec<_> = report.notes.iter().filter(|note| note.kind == kind).collect();
for note in report.notes.iter().filter(|note| note.kind == kind) {
if !groups.contains(&(kind, note.loss)) {
groups.push((kind, note.loss));
}
}
}
groups.sort_by_key(|(kind, loss)| (*kind == NoteKind::Skipped, loss.is_some()));
let say = |s: &mut Session, loss: Option<LossCode>, text: crate::notice_text::NoticeText| match loss {
Some(code) => s.notify_loss(code, text),
None => s.notify_text(text, false),
};
for (kind, loss) in groups {
let notes: Vec<_> = report.notes.iter().filter(|note| note.kind == kind && note.loss == loss).collect();
let Some(first) = notes.first() else { continue };
if notes.len() == 1 {
let template = match kind {
NoteKind::Approximated => "QGIS import, {layer}: {detail} (drawn differently; count {count})",
NoteKind::Skipped => "QGIS import, {layer}: {detail} (left out; count {count})",
};
s.notify_text(
say(
s,
loss,
crate::notice_text::NoticeText::format(
template,
[
Expand All @@ -1962,7 +2011,6 @@ fn notify_report(s: &mut Session, report: &Report) {
("count", first.count.to_string().into()),
],
),
false,
);
continue;
}
Expand Down Expand Up @@ -1990,12 +2038,13 @@ fn notify_report(s: &mut Session, report: &Report) {
NoteKind::Approximated => "QGIS import: {count} approximations; affected layers: {layers}. {samples}",
NoteKind::Skipped => "QGIS import: {count} omissions; affected layers: {layers}. {samples}",
};
s.notify_text(
say(
s,
loss,
crate::notice_text::NoticeText::format(
template,
[("count", count.to_string().into()), ("layers", layers.to_string().into()), ("samples", samples.into())],
),
false,
);
}
}
Expand Down
39 changes: 39 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5254,6 +5254,45 @@ fn qgis_import_notices_group_layers_and_bound_the_sample_without_dropping_return
assert!(said[0].text.len() < 4096);
}

#[test]
fn qgis_import_codes_only_the_notes_that_lose_data() {
use rustgis_data::loss::LossCode;
let mut s = world();
let layer = |name: &str, provider: &str, source: &str| {
format!(
r#"<maplayer type="vector"><id>{name}</id><layername>{name}</layername><provider>{provider}</provider><datasource>{source}</datasource></maplayer>"#
)
};
let layers = [
layer("Unsupported", "not-a-provider", "x"),
r#"<maplayer type="vector"><id>Unnamed</id><layername>Unnamed</layername><datasource>x</datasource></maplayer>"#.to_string(),
layer("Heights", "memory", "PointZ?crs=EPSG:4326&amp;field=a:string"),
layer("Arcs", "memory", "CompoundCurve?crs=EPSG:4326&amp;field=a:string"),
]
.concat();
let text = format!(r#"<qgis><projectlayers>{layers}</projectlayers><Layouts><Layout name="page"/></Layouts></qgis>"#);
let mark = s.notice_mark();
let result = s.run("project.importQgis", &json!({"text": text})).unwrap();
let notes = result["notes"].as_array().unwrap();
let json_loss = |layer: &str, text: &str| {
notes.iter().find(|n| n["layer"] == layer && n["text"].as_str().unwrap().contains(text)).map(|n| n["loss"].as_str())
};
assert_eq!(json_loss("Unsupported", "RustGIS doesn't read"), Some(Some("dropped.feature")));
assert_eq!(json_loss("Unnamed", "RustGIS doesn't read"), Some(Some("dropped.feature")), "a vector layer naming no provider");
// A memory layer opens empty, so its Z values and curves held no data: plain.
assert_eq!(json_loss("Heights", "Z and M"), Some(None));
assert_eq!(json_loss("Arcs", "curves read as lines"), Some(None));
assert_eq!(json_loss("", "print layouts"), Some(None), "a layout is drawing, not data: no code");
let said = s.notices_since(mark);
let with = |code| said.iter().filter(|n| n.loss() == Some(code)).collect::<Vec<_>>();
assert!(with(LossCode::DroppedFeature)[0].text.contains("Unsupported"), "{said:?}");
assert!(with(LossCode::DroppedZm).is_empty() && with(LossCode::ApproximatedCurve).is_empty(), "{said:?}");
let layouts: Vec<_> = said.iter().filter(|n| n.text.contains("print layouts")).collect();
assert_eq!(layouts.len(), 1);
assert_eq!(layouts[0].loss(), None);
assert!(said.iter().filter(|n| n.loss().is_some()).all(|n| !n.text.contains("print layouts")), "no coded notice holds a plain note");
}

/// A QGIS categorized renderer by an expression classes by it where the expression reads on the
/// layer; where it doesn't, every feature draws with the first category's symbol, noted, as before.
#[test]
Expand Down
10 changes: 7 additions & 3 deletions crates/engine/src/tests/qgis_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,16 @@ fn a_qgis_projects_web_service_layers_come_in_from_their_services() {
}
assert!(notes.contains("the same tiles as Topo, which takes its place"), "{notes}");
// A type the service doesn't list: noted, the others still in.
assert!(notes.contains(r#""kind":"skipped","layer":"Missing","text":"the layer: "#) && notes.contains("no layer 'nope'"), "{notes}");
assert!(
notes.contains(r#""kind":"skipped","layer":"Missing","loss":"dropped.feature","text":"the layer: "#)
&& notes.contains("no layer 'nope'"),
"{notes}"
);
assert!(s.map().layer_by_name("Missing").is_none());
assert_eq!(out["layers"].as_array().map(Vec::len), Some(3), "{out}");
assert!(!notes.contains("abc1234"), "{notes}");
// A map service that doesn't answer is left out: no fall back to its whole map.
assert!(notes.contains(r#""kind":"skipped","layer":"Gone","text":"the layer: "#), "{notes}");
assert!(notes.contains(r#""kind":"skipped","layer":"Gone","loss":"dropped.feature","text":"the layer: "#), "{notes}");
assert!(!notes.contains("its map layer 1 alone"), "{notes}");
// Two layers of it: its whole map, said as what is drawn, not as an unread setting.
assert!(
Expand Down Expand Up @@ -201,7 +205,7 @@ fn ogc_api_wmts_and_wcs_layers_come_in_from_their_services() {
assert_eq!(subsets, ["Long(5.1,5.3)", "Lat(49.5,49.9)"], "{get:?}");
// The WMTS layer was asked as one (its answer here isn't one: noted).
assert!(asked.iter().any(|r| r.path == "/wmts" && r.query.iter().any(|(k, v)| k == "SERVICE" && v == "WMTS")), "{asked:?}");
assert!(notes.contains(r#""layer":"Tiles","text":"the layer: "#), "{notes}");
assert!(notes.contains(r#""layer":"Tiles","loss":"dropped.feature","text":"the layer: "#), "{notes}");
// Its tile matrix set is RustGIS's choice, not QGIS's: said for the WMTS layer, and for no other.
assert!(notes.contains(r#""layer":"Tiles","text":"its tile matrix set: RustGIS takes the service's Web Mercator one"#), "{notes}");
assert_eq!(notes.matches("its tile matrix set").count(), 1, "{notes}");
Expand Down
Loading
Loading