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
4 changes: 2 additions & 2 deletions crates/engine/command-surface.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ edit.flip
edit.groundToGrid on? scale? rotation?
edit.merge
edit.move dx dy units?
edit.moveSharedVertex layer? row part? ring? index lon lat
edit.moveSharedVertex layer? row part? ring? index lon? lat? x? y?
edit.paste layer? text?
edit.pasteSpecial name? text?
edit.previewShapeConversion layer? row featureId? kind request? image? frame?
Expand All @@ -124,7 +124,7 @@ edit.traverse layer? start units? legs
edit.undo
edit.updateBox layer? row featureId? box
edit.updateCircle layer? row featureId? circle
edit.vertex layer? row action part? ring? index lon? lat?
edit.vertex layer? row action part? ring? index lon? lat? x? y?
embeddings.add dataset url? year? bands? stretch? dimension? bbox? name? ran?
embeddings.addCube dataset bbox? start? end? bands? stretch? name? found? complete? unusable?
embeddings.change dataset from? to? bbox? cellSize? name? ran?
Expand Down
114 changes: 92 additions & 22 deletions crates/engine/src/cmd/design.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,11 +666,11 @@ pub fn commands() -> Vec<C> {
.when(need_editable)
.params(r#"{"layer"?, "row": number, "geometry": GeoJSON geometry}"#),
C::new("edit.vertex", "Edit Vertex", "Edit › Features › Modify", edit_vertex).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable).params(
r#"{"layer"?, "row": number, "action": "move"|"insert"|"delete", "part"?: number, "ring"?: number, "index": number (insert: the vertex the new one follows), "lon"?, "lat"?}"#,
r#"{"layer"?, "row": number, "action": "move"|"insert"|"delete", "part"?: number, "ring"?: number, "index": number (insert: the vertex the new one follows), "lon"?, "lat"?, "x"?, "y"? (in place of lon/lat: in the layer's own coordinates, written as they are)}"#,
),
C::new("edit.merge", "Merge", "Edit › Features › Modify", merge).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable_selection),
C::new("edit.moveSharedVertex", "Move Shared Vertex", "Edit › Features › Modify", shared_vertex::run).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable).params(
r#"{"layer"?, "row": number, "part"?: number, "ring"?: number, "index": number, "lon": degrees, "lat": degrees}"#,
r#"{"layer"?, "row": number, "part"?: number, "ring"?: number, "index": number, "lon"?: degrees, "lat"?: degrees, "x"?, "y"? (in place of lon/lat: in the layer's own coordinates)}"#,
),
C::new("edit.explode", "Explode", "Edit › Features › Modify", explode).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable_selection),
C::new("edit.split", "Split", "Edit › Features › Modify", split).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable_selection).params(r#"{"lon", "lat"}"#),
Expand All @@ -681,7 +681,7 @@ pub fn commands() -> Vec<C> {
.when(need_editable_selection)
.params(r#"{"line": GeoJSON LineString (lon/lat) drawn across the selected line or polygon's boundary}"#),
C::new("edit.trace", "Trace Edge Path", "Edit › Features", sketch::trace).roles(&[]).effects(crate::policy::Effects::NONE).view().params(
r#"{"from": [lon, lat], "to": [lon, lat], "layer"?, "row"?: number (else the nearest edge within the tolerance), "tolerancePx"?: number (default: the snapping tolerance)} (fromOffM and toOffM in Web Mercator metres, or metres of the grid of a layer held in a projected system)"#,
r#"{"from": [lon, lat], "to": [lon, lat], "layer"?, "row"?: number (else the nearest edge within the tolerance), "tolerancePx"?: number (default: the snapping tolerance); returns fromOffM and toOffM: Web Mercator metres, or metres of the grid of a layer held in a projected system}"#,
),
C::new("edit.traverse", "Traverse", "Edit › Features › Modify", traverse).roles(&[]).effects(crate::policy::Effects::NONE).when(need_editable).params(
r#"{"layer"?, "start": [lon, lat], "units"?: "Meters" (default)|"Feet"|"Kilometers"|"Miles", "legs": [{"distance": number, "bearing": degrees from north}]} (closed in a polygon layer, open in a line layer)"#,
Expand Down Expand Up @@ -1435,31 +1435,100 @@ fn edit_vertex(s: &mut Session, p: &Json) -> CmdResult {
}
};
let v = rustgis_data::VertexId::new(num("part")?.unwrap_or(0), num("ring")?.unwrap_or(0), num("index")?.ok_or("index is required")?);
let at = || -> Result<Coord, String> {
let (lon, lat) = (f(p, "lon").ok_or("lon is required")?, f(p, "lat").ok_or("lat is required")?);
if !(lon.is_finite() && lat.is_finite()) || lon.abs() > 360.0 || lat.abs() > 90.0 {
return Err("lon and lat are degrees".into());
}
Ok(Coord::new(lon, lat))
};
// In longitude/latitude, from the layer's own coordinates (`replace_geometry` writes it back).
let action = ps(p, "action").ok_or("action is required")?;
if !matches!(action, "move" | "insert" | "delete") {
return Err(format!("unknown action '{action}'"));
}
// A deleted vertex needs no place.
let own_at = own_place(p)?;
let l = s.map().layer(id).ok_or("no such row")?;
let feature = l.dataset.features.row(row).ok_or("no such row")?;
let own = feature.geometry().ok_or("the feature has no shape")?;
let g = l.dataset.feature_to_lonlat(feature.id(), own)?.into_owned();
let (g, v) = match ps(p, "action").ok_or("action is required")? {
"move" => (g.move_vertex(v, at()?).ok_or("no such vertex")?, v),
"insert" => {
let g = g.insert_vertex(v, at()?).ok_or("vertices can only be inserted in lines and polygons")?;
(g, rustgis_data::VertexId::new(v.part, v.ring, v.index + 1))
}
"delete" => (g.delete_vertex(v)?, v),
a => return Err(format!("unknown action '{a}'")),
// Deleting a vertex, or placing one in the layer's own coordinates, edits the feature as it
// is stored: no other vertex moves, and a feature with no place in longitude/latitude can be
// edited too (#2032 C2f-7).
if action == "delete" || own_at.is_some() {
let g = own.clone();
let (g, v) = vertex_edit(g, action, v, || own_at.ok_or_else(|| "x and y are both required".to_string()))?;
replace_geometry_own(s, id, row, g)?;
return Ok(json!({"part": v.part, "ring": v.ring, "index": v.index}));
}
// In longitude/latitude, from the layer's own coordinates (`replace_geometry` writes it back).
let Ok(g) = l.dataset.feature_to_lonlat(feature.id(), own) else {
// A feature with a place that has no longitude/latitude: edited as stored, the place given
// in longitude/latitude written in the layer's coordinates (a place snapped to as stored).
let g = own.clone();
let (g, v) = vertex_edit(g, action, v, || lonlat_in_own(s, id, lonlat_place(p)?))?;
replace_geometry_own(s, id, row, g)?;
return Ok(json!({"part": v.part, "ring": v.ring, "index": v.index}));
};
let (g, v) = vertex_edit(g.into_owned(), action, v, || lonlat_place(p))?;
replace_geometry(s, id, row, g)?;
Ok(json!({"part": v.part, "ring": v.ring, "index": v.index}))
}

/// A place in longitude/latitude in the coordinates of layer `id`, as [`replace_geometry`] writes
/// an edit's places: a stored place of a layer in its system that it reads as ([`held_places`]),
/// as stored; any other converted as the layer is drawn.
fn lonlat_in_own(s: &Session, id: u32, at: Coord) -> Result<Coord, String> {
let point = Geometry::Point(at);
let held = held_places(s, id, &[], &[&point]);
match s.map().layer(id).ok_or("no such layer")?.dataset.edit_from_lonlat(point, &held)? {
Geometry::Point(c) => Ok(c),
_ => Err("the place can't be given in the layer's coordinates".into()),
}
}

/// A vertex's place given in the layer's own coordinates (`x`, `y`: as the vertex editor gives it
/// on a map in the layer's system, #2032 C2f-7), or `None` when it is given in longitude/latitude
/// ([`lonlat_place`]) or not at all. Both kinds at once, or half of one, are refused.
pub(super) fn own_place(p: &Json) -> Result<Option<Coord>, String> {
let given = |k: &str| p.get(k).is_some_and(|v| !v.is_null());
if !given("x") && !given("y") {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// Half a pair is refused here too, even where no place is needed (a deleted vertex).
if given("lon") != given("lat") {
return Err("lon and lat are both required".into());
}
return Ok(None);
}
if given("lon") || given("lat") {
return Err("give lon and lat or x and y, not both".into());
}
let (x, y) = (f(p, "x").ok_or("x and y are both required")?, f(p, "y").ok_or("x and y are both required")?);
if !(x.is_finite() && y.is_finite()) {
return Err("x and y must be numbers".into());
}
Ok(Some(Coord::new(x, y)))
}

/// A vertex's place given in longitude/latitude (`lon`, `lat`).
pub(super) fn lonlat_place(p: &Json) -> Result<Coord, String> {
let (lon, lat) = (f(p, "lon").ok_or("lon is required")?, f(p, "lat").ok_or("lat is required")?);
if !(lon.is_finite() && lat.is_finite()) || lon.abs() > 360.0 || lat.abs() > 90.0 {
return Err("lon and lat are degrees".into());
}
Ok(Coord::new(lon, lat))
}

/// [`edit_vertex`]'s `action` on vertex `v` of `g`, placing a moved or inserted vertex `at`, with
/// the vertex edited after it (an inserted one follows `v`).
fn vertex_edit(
g: Geometry,
action: &str,
v: rustgis_data::VertexId,
at: impl FnOnce() -> Result<Coord, String>,
) -> Result<(Geometry, rustgis_data::VertexId), String> {
match action {
"move" => Ok((g.move_vertex(v, at()?).ok_or("no such vertex")?, v)),
"insert" => {
let g = g.insert_vertex(v, at()?).ok_or("vertices can only be inserted in lines and polygons")?;
Ok((g, rustgis_data::VertexId::new(v.part, v.ring, v.index + 1)))
}
"delete" => Ok((g.delete_vertex(v)?, v)),
a => Err(format!("unknown action '{a}'")),
}
}

/// The places an edit of layer `id`, given in longitude/latitude (`edited`), keeps as stored when
/// written back ([`rustgis_data::Dataset::edit_from_lonlat`]): those of the features it changes
/// (`originals`, in the layer's own coordinates), and a stored place of a feature in a layer of the
Expand Down Expand Up @@ -1865,8 +1934,9 @@ fn split(s: &mut Session, p: &Json) -> CmdResult {
}

/// [`split`] of the selected lines of layer `id`, held in `grid`'s projected system, in its own
/// coordinates (#2032 C2f-7): at the point nearest `at` (longitude/latitude) on its segments as they run straight there, measured flat in its
/// grid. Every place of the line stays as stored; only the point it is split at is new.
/// coordinates (#2032 C2f-7): at the point nearest `at` (longitude/latitude) on its segments as
/// they run straight there, measured flat in its grid. Every place of the line stays as stored;
/// only the point it is split at is new.
fn split_own(s: &mut Session, id: u32, grid: OwnGrid, at: Coord) -> CmdResult {
// Converted as the layer is drawn; the split point is projected onto a segment anyway, so
// stored places aren't looked up for it.
Expand Down
24 changes: 13 additions & 11 deletions crates/engine/src/cmd/design/shared_vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ pub(super) fn run(s: &mut Session, p: &Value) -> CmdResult {
let id = s.layer_id(p)?;
let row = index(p, "row", None)?;
let vertex = VertexId::new(index(p, "part", Some(0))?, index(p, "ring", Some(0))?, index(p, "index", None)?);
let lon = super::f(p, "lon").ok_or("lon is required")?;
let lat = super::f(p, "lat").ok_or("lat is required")?;
if !(lon.is_finite() && lat.is_finite()) || lon.abs() > 360.0 || lat.abs() > 90.0 {
return Err("lon and lat are degrees".into());
}
let own = super::own_place(p)?;
let layer = s.map().layer(id).ok_or("no such layer")?;
// The place in the layer's own coordinates, where its vertices are matched exactly: a stored
// place it was snapped to, as stored.
let place = Geometry::Point(Coord::new(lon, lat));
let held = super::held_places(s, id, &[], &[&place]);
let Geometry::Point(at) = layer.dataset.edit_from_lonlat(place, &held)? else {
return Err("the place can't be given in the layer's coordinates".into());
// The place in the layer's own coordinates, where its vertices are matched exactly: as given
// there (`x`, `y`), or from longitude/latitude, a stored place it was snapped to as stored.
let at = match own {
Some(at) => at,
None => {
let place = Geometry::Point(super::lonlat_place(p)?);
let held = super::held_places(s, id, &[], &[&place]);
let Geometry::Point(at) = layer.dataset.edit_from_lonlat(place, &held)? else {
return Err("the place can't be given in the layer's coordinates".into());
};
at
}
};
let source = layer.dataset.features.get(row).ok_or("no such row")?.geometry.as_ref().ok_or("the feature has no shape")?;
if !matches!(source, Geometry::Polygon(_)) {
Expand Down
76 changes: 76 additions & 0 deletions crates/engine/src/tests/own_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,79 @@ fn a_split_says_how_many_segments_it_couldnt_measure_and_a_clean_one_says_nothin
let said = split(Geometry::Line(vec![vec![Coord::new(X, Y), Coord::new(X + SIDE, Y + SIDE)]]));
assert!(said.iter().all(|t| !t.contains("could not be measured")), "{said:?}");
}

/// `edit.vertex` with `x`/`y` (the vertex editor on a map in the layer's own system) writes the
/// place as given, in the layer's coordinates; a deleted vertex leaves every other as stored. A
/// feature with a place that can't be given in longitude/latitude is edited so too (#2032 C2f-7).
#[test]
fn a_vertex_given_in_the_layers_own_coordinates_is_written_as_it_is() {
let far = Geometry::Polygon(vec![vec![vec![
Coord::new(500_000.1, 5_200_000.3),
Coord::new(501_000.7, 5_200_000.9),
Coord::new(1e9, 5_201_000.0),
Coord::new(500_000.1, 5_201_000.0),
Coord::new(500_000.1, 5_200_000.3),
]]]);
let mut s = session(held("Parcels", GeometryType::Polygon, vec![square(500_000.0, 5_000_000.0, 100.0), far.clone()]));
let id = s.map().layers[0].id;
let at = Coord::new(500_012.345_678_9, 5_000_003.210_987_6);
let before = stored(&s);
let r = s.run("edit.vertex", &json!({"layer": id, "row": 0, "action": "move", "index": 1, "x": at.x, "y": at.y})).unwrap();
assert_eq!(r, json!({"part": 0, "ring": 0, "index": 1}));
let after = stored(&s);
assert_eq!(after[1], at, "written as given");
assert_eq!(after.iter().zip(&before).filter(|(a, b)| a != b).count(), 1, "no other place moved");
let r =
s.run("edit.vertex", &json!({"layer": id, "row": 0, "action": "insert", "index": 1, "x": 500_050.0, "y": 5_000_001.0})).unwrap();
assert_eq!(r, json!({"part": 0, "ring": 0, "index": 2}));
assert_eq!(
s.map().layers[0].dataset.features.geometry(0).unwrap().vertex(rustgis_data::VertexId::new(0, 0, 2)),
Some(Coord::new(500_050.0, 5_000_001.0))
);
// The feature with no place in longitude/latitude: moved and a vertex deleted, as stored.
assert!(s.map().layers[0].dataset.feature_to_lonlat(1, &far).is_err());
s.run("edit.vertex", &json!({"layer": id, "row": 1, "action": "move", "index": 0, "x": 500_000.5, "y": 5_200_000.5})).unwrap();
s.run("edit.vertex", &json!({"layer": id, "row": 1, "action": "delete", "index": 1})).unwrap();
let g = s.map().layers[0].dataset.features.geometry(1).unwrap().clone();
let want = far.move_vertex(rustgis_data::VertexId::new(0, 0, 0), Coord::new(500_000.5, 5_200_000.5)).unwrap();
assert_eq!(g, want.delete_vertex(rustgis_data::VertexId::new(0, 0, 1)).unwrap());
// Undo goes back step by step.
s.run("edit.undo", &json!(null)).unwrap();
assert_eq!(s.map().layers[0].dataset.features.geometry(1), Some(&want));
// Both kinds of place, or half of one, are refused.
let both = json!({"layer": id, "row": 0, "action": "move", "index": 1, "x": 1.0, "y": 2.0, "lon": 9.0, "lat": 45.0});
assert!(s.run("edit.vertex", &both).unwrap_err().contains("not both"));
let half = json!({"layer": id, "row": 0, "action": "move", "index": 1, "x": 1.0});
assert!(s.run("edit.vertex", &half).unwrap_err().contains("x and y"));
let half = json!({"layer": id, "row": 0, "action": "delete", "index": 1, "lon": 9.0, "lat": null});
assert!(s.run("edit.vertex", &half).unwrap_err().contains("lon and lat"));
}

/// A vertex of a feature with a place that has no longitude/latitude, moved to a place given in
/// longitude/latitude (a vertex snapped to), is written in the layer's coordinates: the place
/// snapped to as stored, every other vertex as it was.
#[test]
fn a_lonlat_place_on_a_feature_with_no_lonlat_is_written_in_its_own_coordinates() {
let far = Geometry::Polygon(vec![vec![vec![
Coord::new(500_000.1, 5_200_000.3),
Coord::new(501_000.7, 5_200_000.9),
Coord::new(1e9, 5_201_000.0),
Coord::new(500_000.1, 5_201_000.0),
Coord::new(500_000.1, 5_200_000.3),
]]]);
let corner = Coord::new(500_100.123_456_7, 5_000_100.765_432_1);
let other = Geometry::Polygon(vec![vec![vec![
Coord::new(500_000.0, 5_000_000.0),
Coord::new(500_100.0, 5_000_000.0),
corner,
Coord::new(500_000.0, 5_000_100.0),
Coord::new(500_000.0, 5_000_000.0),
]]]);
let mut s = session(held("Parcels", GeometryType::Polygon, vec![other, far.clone()]));
let id = s.map().layers[0].id;
assert!(s.map().layers[0].dataset.feature_to_lonlat(1, &far).is_err());
let [lon, lat] = lonlat(corner);
s.run("edit.vertex", &json!({"layer": id, "row": 1, "action": "move", "index": 1, "lon": lon, "lat": lat})).unwrap();
let g = s.map().layers[0].dataset.features.geometry(1).unwrap().clone();
assert_eq!(g, far.move_vertex(rustgis_data::VertexId::new(0, 0, 1), corner).unwrap());
}
13 changes: 9 additions & 4 deletions crates/engine/src/tests/unplaced_edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,20 @@ fn split_and_cut_leave_out_what_they_cant_place() {
assert!(e.contains("can't be given in longitude/latitude, so they can't be cut"), "{e}");
}

/// An edit of the one feature that can't be placed (a vertex, a reshape, a merge with it) is
/// refused naming the layer and the feature.
/// An edit of the one feature that can't be placed (a reshape, a merge with it) is refused
/// naming the layer and the feature. A vertex moved to a place in longitude/latitude is written
/// in the layer's coordinates, every other vertex as stored (#2032 C2f-7).
#[test]
fn an_edit_of_a_feature_it_cant_place_names_it() {

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: the test's name and the first line of its doc still say an edit of this feature is refused, but the vertex part now asserts that a move succeeds and writes only that vertex (the reshape and merge parts still assert refusal). A name such as an_edit_of_a_feature_it_cant_place_names_it_and_a_vertex_move_writes_only_its_vertex would match what it checks. Left for the release quality pass if you prefer.

Category: quality

let mut s = session(true);
let id = s.map().layer_by_name("lines").unwrap().dataset.features.row(1).unwrap().id();
let named = format!("lines: feature {id} has a place in");
let e = s.run("edit.vertex", &json!({"layer": "lines", "row": 1, "action": "move", "index": 0, "lon": 10.0, "lat": 45.0})).unwrap_err();
assert!(e.contains(&named) && e.contains("so it can't be edited there"), "{e}");
let before = s.map().layer_by_name("lines").unwrap().dataset.features.geometry(1).unwrap().clone();
s.run("edit.vertex", &json!({"layer": "lines", "row": 1, "action": "move", "index": 0, "lon": 10.0, "lat": 45.0})).unwrap();
let after = s.map().layer_by_name("lines").unwrap().dataset.features.geometry(1).unwrap().clone();
let v = rustgis_data::VertexId::new(0, 0, 0);
assert_ne!(after.vertex(v), before.vertex(v));
assert_eq!(after.move_vertex(v, before.vertex(v).unwrap()), Some(before), "only the moved vertex changes");
select(&mut s, "lines", &[1]);
let across = json!({"type": "LineString", "coordinates": [[10.0, 45.0], [10.01, 45.01]]});
let e = s.run("edit.reshape", &json!({"line": across})).unwrap_err();
Expand Down
Loading
Loading