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: 25 additions & 5 deletions crates/engine/src/cmd/workflows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2013,24 +2013,44 @@ fn simulate(s: &mut Session, p: &Value) -> CmdResult {
}

/// Holds `ds`, a layer made in longitude/latitude from another, as that layer is
/// ([`Dataset::hold_as`]; `system` is its [`Dataset::coord_crs`] and [`Dataset::crs`]), and says
/// so when it can't be: it stays in longitude/latitude then, as it was made. A cancel stops the
/// command, which adds nothing.
/// ([`Dataset::hold_as`]; `system` is its [`Dataset::coord_crs`] and [`Dataset::crs`]), or in the
/// project's system ([`made_in`]), and says so when it can't be: it stays in longitude/latitude
/// then, as it was made. A cancel stops the command, which adds nothing.
pub(crate) fn hold_as(
s: &mut Session,
ds: &mut Dataset,
(held, crs): (rustgis_data::raster::Crs, Option<rustgis_data::raster::Crs>),
system: (rustgis_data::raster::Crs, Option<rustgis_data::raster::Crs>),
) -> Result<(), String> {
let (held, crs) = made_in(s, system);
match ds.hold_as(held, crs) {
Err(e) if matches!(rustgis_data::DataError::of_conversion(e.clone()), rustgis_data::DataError::Cancelled) => Err(e),
Err(e) => {
s.notify(format!("{}: kept in longitude/latitude, as it was made, not in {}: {e}", ds.name, held.label()), false);
// Its refusal starts with the layer's name, named once here.
let why = e.strip_prefix(&format!("{}: ", ds.name)).unwrap_or(&e);
let whose = if held == system.0 { String::new() } else { "the project's ".to_string() };
s.notify(format!("{}: kept in longitude/latitude, as it was made, not in {whose}{}: {why}", ds.name, held.label()), false);
Ok(())
}
Ok(()) => Ok(()),
}
}

/// The system a layer made in longitude/latitude from another, held in `system` (its
/// [`Dataset::coord_crs`] and [`Dataset::crs`]), is held in: the other's, unless that is one tools
/// don't work in ([`rustgis_analysis::works_in`]: longitude/latitude, Web Mercator, a geographic
/// one) and the project has a system of its own (#2032 C3a), which it then takes, as a tool's
/// output does. A geocentric project system holds no map coordinates, so it is passed over.
pub(crate) fn made_in(
s: &Session,
system: (rustgis_data::raster::Crs, Option<rustgis_data::raster::Crs>),
) -> (rustgis_data::raster::Crs, Option<rustgis_data::raster::Crs>) {
use rustgis_data::raster::Crs;
match s.project.coordinate_system.filter(|c| *c != Crs::LonLat && !c.is_geocentric()) {
Some(p) if !rustgis_analysis::works_in(system.0) => (p, Some(p)),
_ => system,
}
}

/// Numeric fields requested by name, or all numeric fields when omitted; invalid names fail.
pub(super) fn numeric_fields(ds: &Dataset, p: &Value) -> Result<Vec<usize>, String> {
match p.get("fields").and_then(Value::as_array) {
Expand Down
105 changes: 105 additions & 0 deletions crates/engine/src/tests/project_crs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,108 @@ fn a_cancel_while_an_output_is_converted_adds_nothing() {
assert!(t.map().layer_by_name("Out").is_none(), "look {n} of {looks}");
}
}

/// Roads (a line) and Parcels (two named squares) around Bern in longitude/latitude.
fn bern_roads_and_parcels() -> [rustgis_data::Dataset; 2] {
use rustgis_data::{Coord, Dataset, Field, FieldType, Geometry, GeometryType, Value as Cell};
let mut roads = Dataset::new("Roads", GeometryType::Polyline, vec![]);
let line = vec![Coord::new(7.43, 46.94), Coord::new(7.44, 46.945), Coord::new(7.45, 46.95)];
roads.push(Some(Geometry::Line(vec![line])), vec![]);
let mut parcels = Dataset::new("Parcels", GeometryType::Polygon, vec![Field::new("name", FieldType::Text)]);
for (i, x) in [7.43, 7.45].into_iter().enumerate() {
let ring = vec![
Coord::new(x, 46.94),
Coord::new(x + 0.01, 46.94),
Coord::new(x + 0.01, 46.95),
Coord::new(x, 46.95),
Coord::new(x, 46.94),
];
parcels.push(Some(Geometry::Polygon(vec![vec![ring]])), vec![Cell::Text(format!("p{i}"))]);
}
[roads, parcels]
}

/// Shortest Route, Simulation and Convert Labels to Annotation make their layer from another in
/// longitude/latitude: in a project with a system, it is held in that system, as a tool's output
/// is; from a layer held in a projected system, in that one, as before.
#[test]
fn layers_made_from_another_in_longitude_latitude_take_the_project_system() {
let made = |s: &Session, v: &Value| s.map().layer(v["layer"].as_u64().unwrap() as u32).unwrap().dataset.clone();
let run = |s: &mut Session| {
s.run("labels.enable", &json!({"layer": "Parcels", "on": true})).unwrap();
let route = s
.run("analysis.route", &json!({"layer": "Roads", "from": [7.4301, 46.9401], "to": [7.4499, 46.9499], "snapDistance": 100}))
.unwrap();
let simulated = s.run("analysis.simulate", &json!({"layer": "Parcels", "count": 3, "seed": 3})).unwrap();
let annotation = s.run("labels.convert", &json!({"layer": "Parcels"})).unwrap();
[made(s, &route), made(s, &simulated), made(s, &annotation)]
};
let session = |hold: Option<Crs>| {
let mut s = Session::new(Project::default());
for mut ds in bern_roads_and_parcels() {
if let Some(c) = hold {
ds.hold_as(c, Some(c)).unwrap();
}
s.map_mut().add_dataset(ds);
}
s
};
let plain = run(&mut session(None));
for ds in &plain {
assert_eq!((ds.coord_crs(), ds.crs), (Crs::LonLat, None), "{}: without a project system, as before", ds.name);
}
let mut s = session(None);
s.run("project.coordinateSystem", &json!({"crs": "EPSG:2056"})).unwrap();
let swiss = run(&mut s);
for (ds, before) in swiss.iter().zip(&plain) {
assert_eq!((ds.coord_crs(), ds.crs), (LV95, Some(LV95)), "{}", ds.name);
// The same places, converted.
let back = ds.in_lonlat().unwrap();
assert_eq!(back.features.len(), before.features.len(), "{}", ds.name);
for (a, b) in back.features.iter().zip(&before.features) {
let (mut pa, mut pb) = (Vec::new(), Vec::new());
a.geometry.as_ref().unwrap().for_each_coord(|c| pa.push(c));
b.geometry.as_ref().unwrap().for_each_coord(|c| pb.push(c));
assert_eq!(pa.len(), pb.len());
for (p, q) in pa.iter().zip(&pb) {
assert!((p.x - q.x).abs() < 1e-7 && (p.y - q.y).abs() < 1e-7, "{}: {p:?} {q:?}", ds.name);
}
}
}
// From layers held in UTM, the layers stay in UTM in an LV95 project.
let utm = Crs::Epsg(32632);
let mut s = session(Some(utm));
s.run("project.coordinateSystem", &json!({"crs": "EPSG:2056"})).unwrap();
for ds in run(&mut s) {
assert_eq!(ds.coord_crs(), utm, "{}", ds.name);
}
}

/// A layer made from another that the project's system can't hold stays in longitude/latitude,
/// said once with its name; in a geocentric project system it stays there unsaid.
#[test]
fn a_layer_made_from_another_the_project_system_cant_hold_stays_in_longitude_latitude() {
use rustgis_data::{Coord, Dataset, Field, FieldType, Geometry, GeometryType, Value as Cell};
let session = |crs: &str| {
let mut s = Session::new(Project::default());
let mut parcels = Dataset::new("Arctic", GeometryType::Polygon, vec![Field::new("name", FieldType::Text)]);
let ring =
vec![Coord::new(10.0, 87.0), Coord::new(11.0, 87.0), Coord::new(11.0, 88.0), Coord::new(10.0, 88.0), Coord::new(10.0, 87.0)];
parcels.push(Some(Geometry::Polygon(vec![vec![ring]])), vec![Cell::Text("north".into())]);
s.map_mut().add_dataset(parcels);
s.run("project.coordinateSystem", &json!({"crs": crs})).unwrap();
s
};
let mut s = session("EPSG:3857");
let out = said(&mut s, "analysis.simulate", &json!({"layer": "Arctic", "count": 2, "seed": 1, "name": "Pts"}));
let made = &s.map().layer_by_name("Pts").unwrap().dataset;
assert_eq!(made.coord_crs(), Crs::LonLat);
let note: Vec<&String> = out.iter().filter(|m| m.contains("kept in longitude/latitude")).collect();
assert_eq!(note.len(), 1, "{out:?}");
assert!(note[0].starts_with("Pts: kept in longitude/latitude, as it was made, not in the project's EPSG:3857: feature"), "{note:?}");
assert_eq!(note[0].matches("Pts").count(), 1, "{note:?}");
let mut s = session("EPSG:4978");
let out = said(&mut s, "analysis.simulate", &json!({"layer": "Arctic", "count": 2, "seed": 1, "name": "Pts"}));
assert_eq!(s.map().layer_by_name("Pts").unwrap().dataset.coord_crs(), Crs::LonLat);
assert!(!out.iter().any(|m| m.contains("kept in")), "{out:?}");
}
2 changes: 1 addition & 1 deletion docs/user-guide/analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,7 @@ click them on the map:
| Geostatistical Wizard | An IDW surface from feature values, using 12 nearest neighbours by default |
| Network Analysis | The shortest (or cheapest, by a cost field) route over a line layer |
| Neighborhood Explorer | What lies within a distance of a point, with sums of numeric fields |
| Simulation | Random points in polygons (a fixed number, or from a field × rate), spread in the layer's own coordinates when it is held in a projected system, else in longitude/latitude |
| Simulation | Random points in polygons (a fixed number, or from a field × rate), spread in the layer's own coordinates when it is held in a projected system, else in longitude/latitude (then held in the project's coordinate system when it has one, other than a geocentric one) |
| Data Engineering | Counts, nulls, distinct values, min, max and mean of every field |
| Exploratory Analysis | Histograms and correlations of numeric fields |
| Embeddings | The features most like the selected one, by their numeric attributes |
Expand Down
5 changes: 3 additions & 2 deletions docs/user-guide/cartography.md
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,9 @@ Turn labels on per layer (Labeling › Labels › **Label**), then:
- **Convert** (Labeling › Map Labeling, or Layers › right-click › **Convert To Annotation**) turns a
layer's labels into an annotation layer: one point per label, carrying its
text in a `TEXT` field, which you can then edit, move or delete like any other features.
The annotation layer is held in the labelled layer's system. In a projected system a label's
point is found in its own coordinates, where a map in that system draws it; a moved label that
The annotation layer is held in the labelled layer's system; for a layer in
longitude/latitude, Web Mercator or another system in degrees, in the project's coordinate
system when it has one (not a geocentric one). In a projected system a label's point is found in its own coordinates, where a map in that system draws it; a moved label that

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this paragraph line runs far past the wrap width the rest of the guide uses. Rewrap the paragraph so each line stays near 100 columns.

Category: quality

can't be placed there stays at its feature's label point, and the conversion says how many.
A label along a line becomes an upright point at its line's middle vertex (the end of a line
of two), which can be some way from where the label was drawn: its angle, curve, repeats and
Expand Down
6 changes: 4 additions & 2 deletions docs/user-guide/maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,10 @@ Tools follow it too. A tool that works in longitude/latitude (on a layer in degr
always does) gives its new layers in the project's system, converted, and with it as their own, so
they export in it; one with a place the system can't hold (beyond 85.05° in Web Mercator, say)
stays in longitude/latitude, and the tool says so. A tool that works in its first layer's own
projected system keeps its output there. IDW, Kriging, Kernel Density and the Geostatistical Wizard
make a raster from a layer in degrees, or with no system, in the project's when it is projected,
projected system keeps its output there. Shortest Route, Simulation and Convert Labels to
Annotation do the same with the layer they make from another. IDW, Kriging, Kernel Density and
the Geostatistical Wizard make a raster from a layer in degrees, or with no system, in the
project's when it is projected,
and say so (`crs: "EPSG:4326"` still makes it in degrees). An export of a layer with no system of
its own (one drawn in longitude/latitude, say) is written in the project's system where the format
can hold one (GeoPackage, Shapefile, FlatGeobuf, GeoParquet), and says so. A geocentric project system holds no map
Expand Down
Loading