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
13 changes: 6 additions & 7 deletions crates/analysis/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,20 @@ pub fn shortest_route(
shortest_route_with(&NETWORK, ds, rows, from, to, cost_field, snap_m)
}

/// [`shortest_route`] over `held`, a layer held in another system than longitude/latitude: the
/// graph is built from `lonlat`, the layer in longitude/latitude, only when the one kept isn't
/// for `held`'s rows as they are, and is kept under them. Later routes over the same layer then
/// reuse it without converting the layer again; the converted copy isn't kept.
pub fn shortest_route_held<'a>(
/// [`shortest_route`] over `held`, a layer held in another system than longitude/latitude. The
/// graph is built from `held`'s lines, each converted to longitude/latitude as it is read (no
/// converted copy of the layer is made), only when the one kept isn't for `held`'s rows as they
/// are, and is kept under them. Later routes over the same layer then reuse it.
pub fn shortest_route_held(
held: &Dataset,
lonlat: impl FnOnce() -> Result<std::borrow::Cow<'a, Dataset>, String>,
rows: &[usize],
from: Coord,
to: Coord,
cost_field: Option<usize>,
snap_m: f64,
) -> Result<Route, String> {
check_route_ends(from, to, snap_m)?;
let graph = network_keyed(&NETWORK, held, rows, cost_field, || Network::build(&*lonlat()?, rows, cost_field))?;
let graph = network_keyed(&NETWORK, held, rows, cost_field, || Network::build(held, rows, cost_field))?;
route_on(&graph, held, from, to, snap_m)
}

Expand Down
14 changes: 14 additions & 0 deletions crates/data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,20 @@ impl Dataset {
Self::geometry_to_lonlat_by(&self.shown_conversion()?, g)
}

/// [`Dataset::geometry_to_lonlat`] of `g`, feature `id`'s geometry, for an edit of that one
/// feature: refused naming the layer and the feature when one of its places can't be given in
/// longitude/latitude, so it can't be edited there.
pub fn feature_to_lonlat<'a>(&self, id: u64, g: &'a Geometry) -> std::result::Result<Cow<'a, Geometry>, String> {

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.

feature_to_lonlat names self.name, the dataset's name, in its refusal. The file already notes that a layer's map name can differ from its data's name after a name clash (the named closure in check_topology, and the sync path using own.name), and the PR's own topology test is written around that. This error can therefore name a different layer than the one the user sees on the map.

Suggested fix: take the map layer name as a parameter from the callers (they already have l.name), or document in the doc comment that the name is the data's name so callers that know the map name can replace it.

Category: quality

let to = self.shown_conversion()?;
Self::converted_to_lonlat(&to, g).ok_or_else(|| {
format!(
"{}: feature {id} has a place in {} that can't be given in longitude/latitude, so it can't be edited there",
self.name,
to.source().label()
)
})
}

/// [`Dataset::geometry_to_lonlat`] by `to`, its [`Dataset::lonlat_conversion`] made once for
/// many geometries: refused, naming its system, when one of the geometry's places can't be
/// converted.
Expand Down
5 changes: 4 additions & 1 deletion crates/data/src/raster/planetary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn off_earth(semi_major: f64) -> bool {
/// `+ellps` or `+datum`, since PROJ's named ellipsoids are all the Earth's.
pub fn proj4_axes(proj4: &str) -> Option<(f64, f64)> {
let value = |key: &str| {
proj4.split_whitespace().take(64).find_map(|t| t.strip_prefix(key)).and_then(|v| v.parse::<f64>().ok()).filter(|v| v.is_finite())
proj4.split_whitespace().find_map(|t| t.strip_prefix(key)).and_then(|v| v.parse::<f64>().ok()).filter(|v| v.is_finite())
};
let a = value("+a=").or_else(|| value("+R=")).filter(|a| *a > 0.0)?;
let b = match (value("+b="), value("+rf=")) {
Expand Down Expand Up @@ -379,6 +379,9 @@ mod tests {
assert_eq!(proj4_axes("+proj=longlat +ellps=WGS84 +no_defs"), None);
assert_eq!(proj4_axes("+proj=longlat +a=x +no_defs"), None);
assert_eq!(proj4_axes("+proj=longlat +a=-5 +no_defs"), None);
// A radius written after many other parameters still counts: the whole definition is read.
let padding = (0..80).map(|i| format!("+x{i}=0")).collect::<Vec<_>>().join(" ");
assert_eq!(proj4_axes(&format!("+proj=longlat {padding} +a=3396190 +no_defs")), Some((3_396_190.0, 3_396_190.0)));
assert_eq!(proj4_axes(""), None);
}

Expand Down
71 changes: 50 additions & 21 deletions crates/engine/src/cmd/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,53 @@ fn coerce(v: &Attr, ty: FieldType) -> (Attr, Cut) {
}

fn paste(s: &mut Session, p: &Value) -> CmdResult {
let (src, said) = clip(s, p)?;
let (mut src, said) = clip(s, p)?;
let id = s.layer_id(p)?;
let l = s.map_mut().layer_mut(id).ok_or("no such layer")?;
if l.is_raster() {
return Err("features can't be pasted into a raster layer".into());
}
let want = l.geometry_type();
// The shapes the layer takes, written in its own coordinates from the system the clipboard
// holds them in: as they are in a layer of that system (pasted where they were copied from,
// nothing moves), else converted by the operation between the two, which doesn't depend on
// the map. All converted before anything changes; one with a place the layer's system can't
// hold is left out, counted and said, rather than the paste refused.
let (from, own) = (src.coord_crs(), l.dataset.coord_crs());
// Into Web Mercator by way of longitude/latitude, where the latitudes it can't hold are found.
let steps = if from == own {
vec![]
} else if own == Crs::WebMercator && from != Crs::LonLat {
vec![from.to(Crs::LonLat)?, Crs::LonLat.to(own)?]
} else {
vec![from.to(own)?]
};
let mut placed = Vec::with_capacity(src.features.len());
if !steps.is_empty() {
let none = rustgis_data::HeldPlaces::default();
for r in 0..src.features.len() {
let fits = src.features.row(r).and_then(|x| x.geometry()).is_some_and(|g| want == GeometryType::None || g.kind() == want);
let Some(Some(g)) = fits.then(|| src.features.set_geometry(r, None)).flatten() else {
placed.push(true);
continue;
};
match steps.iter().try_fold(g, |g, to| Dataset::edit_from_lonlat_by(to, g, &none)) {
Ok(g) => {
src.features.set_geometry(r, Some(g));
placed.push(true);
}
Err(_) => placed.push(false),
}
}
}
let unplaced = placed.iter().filter(|p| !**p).count();
if unplaced > 0 {
let mut keep = placed.into_iter();
src.features.retain_rows(|_| keep.next().unwrap_or(true));
if src.features.is_empty() {
return Err(format!("Paste: the copied feature(s) have a place {} ({}) can't hold", l.name, own.label()));

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.

The wording reads as if the layer's name were a place: "the copied feature(s) have a place plain (Robinson) can't hold". Make the layer the subject, as the notice below does: "Paste: the copied feature(s) have a place that layer {} ({}) can't hold".

Category: quality

}
}
// Ids for every feature the layer takes (one of another shape is left out below), above every id
// it has or keeps new rows above, refused before anything changes when they would run past the
// largest id.
Expand Down Expand Up @@ -184,30 +224,11 @@ fn paste(s: &mut Session, p: &Value) -> CmdResult {
// Per field, what converting the pasted values cut, and the values that aren't null but that
// the field can't hold.
let mut unfit = vec![(Cuts::default(), 0usize); l.dataset.fields.len()];
// The features the layer takes, their shapes written in its own coordinates from the system
// the clipboard holds them in: as they are in a layer of that system (pasted where they were
// copied from, nothing moves), else converted by the operation between the two, which
// doesn't depend on the map. All converted before any is added.
let (from, own) = (src.coord_crs(), l.dataset.coord_crs());
let mut taken: Vec<Feature> = src
let taken: Vec<Feature> = src
.features
.into_iter()
.filter(|ft| !ft.geometry.as_ref().is_some_and(|g| want != GeometryType::None && g.kind() != want))
.collect();
// Into Web Mercator by way of longitude/latitude, where the latitudes it can't hold are found.
let steps = if from == own {
vec![]
} else if own == Crs::WebMercator && from != Crs::LonLat {
vec![from.to(Crs::LonLat)?, Crs::LonLat.to(own)?]
} else {
vec![from.to(own)?]
};
let none = rustgis_data::HeldPlaces::default();
for ft in &mut taken {
for to in &steps {
ft.geometry = ft.geometry.take().map(|g| Dataset::edit_from_lonlat_by(to, g, &none)).transpose()?;
}
}
for ft in taken {
next = next.saturating_add(1);
let attrs = l
Expand All @@ -234,6 +255,14 @@ fn paste(s: &mut Session, p: &Value) -> CmdResult {
let name = l.name.clone();
let features = |n: usize| if n == 1 { "1 feature".to_string() } else { format!("{n} features") };
let mut notices = Vec::new();
if unplaced > 0 {
let (have, they) = if unplaced == 1 { ("has", "it wasn't") } else { ("have", "they weren't") };
notices.push(format!(
"Paste: {} {have} a place that layer {name} ({}) can't hold, so {they} pasted",
features(unplaced),
own.label()
));
}
for (f, (cuts, n)) in l.dataset.fields.iter().zip(unfit) {
notices.extend(cuts.notices("Paste", &f.name));
if n > 0 {
Expand Down
Loading
Loading