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
30 changes: 30 additions & 0 deletions crates/engine/src/tests/qgis_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,33 @@ fn a_layouts_legends_name_the_imported_layers() {
exported.unwrap();
assert!(written > 0);
}

/// The corpus's grids come in on their frames as read (a measured grid in the project's system,
/// a graticule), and the layout exports.
#[test]
fn a_layouts_grids_come_in_on_their_frames() {
use rustgis_map::layout::GridKind;
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../map/testdata/qgis/generated/qgs/layout-grids.qgs");
let mut s = Session::new(Project::default());
s.run("project.importQgis", &json!({"path": path.display().to_string()})).unwrap();
let l = s.project.layouts.iter().find(|l| l.name == "Grids").unwrap();
let mut grids: Vec<(f64, GridKind, f64, Option<String>)> = l
.elements
.iter()
.filter_map(|e| match &e.kind {
ElementKind::MapFrame { grid: Some(g), .. } => Some((e.x, g.kind, g.step, g.crs.clone())),
_ => None,
})
.collect();
grids.sort_by(|a, b| a.0.total_cmp(&b.0));
let kinds: Vec<_> = grids.into_iter().map(|(_, k, step, crs)| (k, step, crs)).collect();
assert_eq!(kinds, [(GridKind::Measured, 500.0, Some("EPSG:2056".to_string())), (GridKind::Graticule, 0.005, None)]);
let dir = std::env::temp_dir().join(format!("rustgis-qgis-grids-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let png = dir.join("grids.png");
let exported = s.run("layout.export", &json!({"layout": "Grids", "path": png.display().to_string(), "dpi": 36}));
let written = std::fs::metadata(&png).map(|m| m.len()).unwrap_or(0);
let _ = std::fs::remove_dir_all(&dir);
exported.unwrap();
assert!(written > 0);
}
33 changes: 17 additions & 16 deletions crates/map/src/qgis/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
//! rectangles and ellipses those shapes, and its lines and polygons straight lines along their
//! edges. Items are drawn in QGIS's order (its `zValue`), placed where QGIS draws them on their
//! page in millimetres, a turned label turned about its centre as QGIS turns it. Its scale bars,
//! north arrows, map overviews ([`surround`]) and legends ([`legend`]) show their map's frame
//! (piece 12b).
//! north arrows, map overviews ([`surround`]) and legends ([`legend`]) show their map's frame, and
//! a map item's first grid becomes its frame's ([`grid`]) (piece 12b).
//!
//! What a RustGIS layout has no place for is noted, counted over all the layouts: tables, HTML
//! and the other item kinds, map grids, the atlas, groups, blending, and settings set by
//! expressions. A QGIS 2 composer isn't read.
//! and the other item kinds, the atlas, groups, blending, and settings set by expressions. A QGIS 2 composer isn't read.

use roxmltree::Node;

Expand Down Expand Up @@ -82,7 +81,9 @@ pub(super) struct Tally {
other_kinds: Vec<String>,
groups: usize,
map_backgrounds: usize,
grids: usize,
grid: grid::Counts,
/// The project's coordinate system, which a map item without one of its own shows.
project_crs: Option<String>,
atlases: usize,
locked_layers: usize,
turned_items: usize,
Expand Down Expand Up @@ -231,6 +232,7 @@ pub(super) struct Tally {
bar_text_sizes: usize,
}

mod grid;
mod legend;
mod surround;

Expand All @@ -246,7 +248,7 @@ fn as_written(text: &str) -> String {
pub fn read_layouts(
root: Node,
dir: &str,
(title, layers, annotations): (&str, &[super::ProjectLayer], Option<&super::ProjectLayer>),
(title, crs, layers, annotations): (&str, Option<&str>, &[super::ProjectLayer], Option<&super::ProjectLayer>),
report: &mut Report,
) -> Vec<ProjectLayout> {
let composers = root.children().filter(|c| c.has_tag_name("Composer")).count();
Expand All @@ -260,6 +262,7 @@ pub fn read_layouts(
let title = Title { text: as_written(&title.chars().take(MAX_TEXT).collect::<String>()), cut, braced: title.contains(['{', '}']) };
tally.title_len = title.text.len();
tally.layers = legend::project_layers(layers, annotations);
tally.project_crs = child(root, "projectCrs").and_then(grid::readable_crs).or_else(|| crs.map(str::to_string));
let mut out = Vec::new();
let mut names = Vec::new();
let all: Vec<Node> = list.children().filter(|c| c.has_tag_name("Layout")).collect();
Expand Down Expand Up @@ -790,14 +793,12 @@ fn map_item(item: Node, place: &Placed, id: Option<String>, target: &mut Project
};
let crs = child(item, "crs").and_then(super::crs_of);
tally.own_crs += usize::from(crs.is_some());
for g in item.children().filter(|c| c.has_tag_name("ComposerMapGrid")) {
// A grid switched off is said as one, as an overview switched off is.
if g.attribute("show") == Some("0") {
tally.overviews_off += 1;
} else {
tally.grids += 1;
}
}
// Its grid, in the system it shows its extent in (its own, else the project's), a page
// millimetre being its extent's width over its own.
let map_crs = child(item, "crs").and_then(grid::readable_crs).or_else(|| tally.project_crs.clone());
let per_mm = (place.w > 0.0).then(|| (extent[2] - extent[0]) / place.w).filter(|v| v.is_finite() && *v > 0.0);
let turn = num(item, "mapRotation").filter(|r| r.is_finite()).unwrap_or(0.0);
let grid = grid::map_grid(item, map_crs.as_deref(), extent, per_mm, turn, tally);
if item.attribute("keepLayerSet") == Some("true") || item.attribute("followPreset") == Some("true") {
tally.locked_layers += 1;
}
Expand All @@ -814,7 +815,7 @@ fn map_item(item: Node, place: &Placed, id: Option<String>, target: &mut Project
center: rustgis_geom::Coord::new(0.0, 0.0),
scale: 1.0,
shape: Default::default(),
grid: None,
grid,
sections: Vec::new(),
extent_of: None,
extent_color: INDICATOR_COLOR,
Expand Down Expand Up @@ -1370,7 +1371,6 @@ impl Tally {
(self.tables, "attribute tables"),
(self.html, "HTML frames"),
(self.markers, "marker items"),
(self.grids, "map grids"),
(self.atlases, "atlases"),
(self.svg, "SVG pictures (RustGIS's pictures are PNG or JPEG)"),
(self.no_picture, "pictures that name no file"),
Expand Down Expand Up @@ -1534,6 +1534,7 @@ impl Tally {
}
// What their symbols and sizes noted, as theirs.
report.merge_as("print layouts", &self.notes);
self.grid.say(report);
if self.unread_texts > 0 {
report.skipped("", format!("its print layouts' labels' {} ({})", self.unread_text.join(", "), self.unread_texts));
}
Expand Down
Loading
Loading