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
195 changes: 195 additions & 0 deletions crates/data/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,160 @@ impl Geometry {
}
}

/// One vertex of a geometry: its part (a line's part, a polygon's polygon; 0 for points), its
/// ring (a polygon's ring; 0 otherwise) and its place along the part or ring (a multipoint's
/// points are one part). A closed ring's repeated last point is not a vertex of its own: it
/// follows the first.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct VertexId {
pub part: usize,
pub ring: usize,
pub index: usize,
}

impl VertexId {
pub fn new(part: usize, ring: usize, index: usize) -> VertexId {
VertexId { part, ring, index }
}
}

/// How many vertices a path has: a closed ring's repeated last point doesn't count.
fn open_len(path: &[Coord], ring: bool) -> usize {
match (path.first(), path.last()) {
(Some(a), Some(b)) if ring && path.len() > 1 && a == b => path.len() - 1,
_ => path.len(),
}
}

impl Geometry {
/// The paths vertices lie on, with their part and ring and whether each is a polygon ring.
fn paths(&self) -> Vec<(usize, usize, &[Coord], bool)> {
match self {
Geometry::Point(c) => vec![(0, 0, std::slice::from_ref(c), false)],
Geometry::MultiPoint(v) => vec![(0, 0, v.as_slice(), false)],
Geometry::Line(parts) => parts.iter().enumerate().map(|(i, p)| (i, 0, p.as_slice(), false)).collect(),
Geometry::Polygon(polys) => polys
.iter()
.enumerate()
.flat_map(|(i, rings)| rings.iter().enumerate().map(move |(j, r)| (i, j, r.as_slice(), true)))
.collect(),
}
}

/// Calls `f` with every vertex, a closed ring's repeated last point left out.
pub fn for_each_vertex(&self, mut f: impl FnMut(VertexId, Coord)) {
for (part, ring, path, closed) in self.paths() {
for (index, c) in path.iter().take(open_len(path, closed)).enumerate() {
f(VertexId { part, ring, index }, *c);
}
}
}

/// Every vertex, a closed ring's repeated last point left out.
pub fn vertices(&self) -> Vec<(VertexId, Coord)> {
let mut out = Vec::new();
self.for_each_vertex(|v, c| out.push((v, c)));
out
}

/// Calls `f` with every edge of a line or polygon: the vertex it starts at and its two ends
/// (a ring's last edge comes back to its first vertex). A vertex inserted on an edge goes
/// after its start.
pub fn for_each_edge(&self, mut f: impl FnMut(VertexId, Coord, Coord)) {
if matches!(self, Geometry::Point(_) | Geometry::MultiPoint(_)) {
return;
}
for (part, ring, path, closed) in self.paths() {
let n = open_len(path, closed);
let count = if closed && n > 2 { n } else { n.saturating_sub(1) };
for index in 0..count {
if let (Some(a), Some(b)) = (path.get(index), path.get((index + 1) % n.max(1))) {
f(VertexId { part, ring, index }, *a, *b);
}
}
}
}

/// Every edge of a line or polygon (see [`Geometry::for_each_edge`]).
pub fn edges(&self) -> Vec<(VertexId, Coord, Coord)> {
let mut out = Vec::new();
self.for_each_edge(|v, a, b| out.push((v, a, b)));
out
}

/// The vertex's position, if it has one.
pub fn vertex(&self, v: VertexId) -> Option<Coord> {
let (_, _, path, closed) = self.paths().into_iter().find(|p| p.0 == v.part && p.1 == v.ring)?;
(v.index < open_len(path, closed)).then(|| path.get(v.index).copied()).flatten()
}

/// The path a vertex lies on, mutably, and whether it is a polygon ring.
fn path_mut(&mut self, part: usize, ring: usize) -> Option<(&mut Vec<Coord>, bool)> {
match self {
Geometry::Point(_) => None,
Geometry::MultiPoint(v) => (part == 0 && ring == 0).then_some((v, false)),
Geometry::Line(parts) => parts.get_mut(part).filter(|_| ring == 0).map(|p| (p, false)),
Geometry::Polygon(polys) => polys.get_mut(part).and_then(|rings| rings.get_mut(ring)).map(|r| (r, true)),
}
}

/// A copy with a vertex moved to `c` (a ring's closing point goes with its first vertex).
pub fn move_vertex(&self, v: VertexId, c: Coord) -> Option<Geometry> {
self.vertex(v)?;
let mut g = self.clone();
if let Geometry::Point(p) = &mut g {
*p = c;
return Some(g);
}
let (path, ring) = g.path_mut(v.part, v.ring)?;
let closed = ring && open_len(path, true) < path.len();
*path.get_mut(v.index)? = c;
if closed && v.index == 0 {
*path.last_mut()? = c;
}
Some(g)
}

/// A copy with a vertex at `c` inserted after `after`, on the edge it starts (lines and
/// polygons only).
pub fn insert_vertex(&self, after: VertexId, c: Coord) -> Option<Geometry> {
if matches!(self, Geometry::Point(_) | Geometry::MultiPoint(_)) {
return None;
}
self.vertex(after)?;
let mut g = self.clone();
let (path, _) = g.path_mut(after.part, after.ring)?;
path.insert(after.index + 1, c);
Some(g)
}

/// A copy without a vertex, refused when a line would keep fewer than two vertices, a
/// polygon ring fewer than three or a multipoint none.
pub fn delete_vertex(&self, v: VertexId) -> Result<Geometry, &'static str> {
self.vertex(v).ok_or("no such vertex")?;
let mut g = self.clone();
let kind = g.kind();
let multi = matches!(g, Geometry::MultiPoint(_));
let Some((path, ring)) = g.path_mut(v.part, v.ring) else {
return Err("a point has only one vertex");
};
let n = open_len(path, ring);
let closed = ring && n < path.len();
match kind {
GeometryType::Point if multi && n <= 1 => return Err("a multipoint keeps at least one point"),
GeometryType::Polyline if n <= 2 => return Err("a line keeps at least two vertices"),
GeometryType::Polygon if n <= 3 => return Err("a polygon ring keeps at least three vertices"),
_ => {}
}
path.remove(v.index);
if closed && v.index == 0 {
let first = *path.first().ok_or("no such vertex")?;
*path.last_mut().ok_or("no such vertex")? = first;
}
Ok(g)
}
}

fn dist2(a: Coord, b: Coord) -> f64 {
(a.x - b.x).powi(2) + (a.y - b.y).powi(2)
}
Expand Down Expand Up @@ -351,6 +505,47 @@ mod tests {
assert!(!segment_intersects_extent(Coord::new(-5.0, 12.0), Coord::new(15.0, 12.0), &Extent::new(0.0, 0.0, 10.0, 10.0)));
}

#[test]
fn vertex_editing() {
// A closed square ring: four vertices, the closing point following the first.
let g = Geometry::Polygon(vec![vec![square(0.0, 0.0, 10.0), square(4.0, 4.0, 2.0)]]);
assert_eq!(g.vertices().len(), 8);
assert_eq!(g.edges().len(), 8);
let first = VertexId::new(0, 0, 0);
let moved = g.move_vertex(first, Coord::new(-1.0, -1.0)).unwrap();
let Geometry::Polygon(p) = &moved else { panic!("not a polygon") };
assert_eq!(p[0][0][0], Coord::new(-1.0, -1.0));
assert_eq!(p[0][0][4], Coord::new(-1.0, -1.0), "the ring stays closed");
// Inserting on the ring's last edge (back to the first vertex) keeps it closed.
let ins = g.insert_vertex(VertexId::new(0, 1, 3), Coord::new(3.0, 5.0)).unwrap();
let Geometry::Polygon(p) = &ins else { panic!("not a polygon") };
assert_eq!(p[0][1].len(), 6);
assert_eq!(p[0][1][4], Coord::new(3.0, 5.0));
assert_eq!(p[0][1].first(), p[0][1].last());
// Deleting the first vertex re-closes the ring; a triangle can't lose one.
let del = g.delete_vertex(first).unwrap();
let Geometry::Polygon(p) = &del else { panic!("not a polygon") };
assert_eq!(p[0][0].len(), 4);
assert_eq!(p[0][0].first(), p[0][0].last());
assert!(del.delete_vertex(first).is_err());
// Lines keep two vertices; points can't lose theirs.
let line = Geometry::Line(vec![vec![Coord::new(0.0, 0.0), Coord::new(1.0, 0.0), Coord::new(2.0, 0.0)]]);
assert_eq!(line.edges().len(), 2);
let two = line.delete_vertex(VertexId::new(0, 0, 1)).unwrap();
assert!(two.delete_vertex(VertexId::new(0, 0, 0)).is_err());
assert!(Geometry::Point(Coord::new(0.0, 0.0)).delete_vertex(VertexId::new(0, 0, 0)).is_err());
assert!(Geometry::Point(Coord::new(0.0, 0.0)).insert_vertex(VertexId::new(0, 0, 0), Coord::new(1.0, 1.0)).is_none());
let mp = Geometry::MultiPoint(vec![Coord::new(0.0, 0.0), Coord::new(1.0, 1.0)]);
assert_eq!(mp.delete_vertex(VertexId::new(0, 0, 1)).unwrap(), Geometry::MultiPoint(vec![Coord::new(0.0, 0.0)]));
// Vertices that aren't there.
for v in [VertexId::new(0, 0, 9), VertexId::new(3, 0, 0), VertexId::new(0, 0, 4), VertexId::new(0, 5, 0)] {
assert!(g.move_vertex(v, Coord::new(0.0, 0.0)).is_none(), "{v:?}");
assert!(g.insert_vertex(v, Coord::new(0.0, 0.0)).is_none(), "{v:?}");
assert!(g.delete_vertex(v).is_err(), "{v:?}");
}
assert!(line.move_vertex(VertexId::new(0, 1, 0), Coord::new(0.0, 0.0)).is_none());
}

#[test]
fn degenerate_inputs_do_not_panic() {
assert!(!ring_contains(&[], Coord::new(0.0, 0.0)));
Expand Down
2 changes: 1 addition & 1 deletion crates/data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod wkb;
pub mod xml;
pub mod zip;

pub use geometry::{Geometry, GeometryType};
pub use geometry::{Geometry, GeometryType, VertexId};
pub use rustgis_geom::{Coord, Extent};
pub use value::{Field, FieldType, Value};

Expand Down
65 changes: 61 additions & 4 deletions crates/engine/src/cmd/design.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,12 @@ pub fn commands() -> Vec<C> {
})
})
.when(need_editable_selection),
C::new("edit.setGeometry", "Edit Vertices", "Edit › Features › Modify", set_geometry)
C::new("edit.setGeometry", "Set Geometry", "Edit › Features › Modify", set_geometry)
.when(need_editable)
.params(r#"{"layer"?, "row": number, "geometry": GeoJSON geometry}"#),
C::new("edit.vertex", "Edit Vertex", "Edit › Features › Modify", edit_vertex).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"?}"#,
),
C::new("edit.merge", "Merge", "Edit › Features › Modify", merge).when(need_editable_selection),
C::new("edit.explode", "Explode", "Edit › Features › Modify", explode).when(need_editable_selection),
C::new("edit.split", "Split", "Edit › Features › Modify", split).when(need_editable_selection).params(r#"{"lon", "lat"}"#),
Expand Down Expand Up @@ -433,6 +436,53 @@ fn set_geometry(s: &mut Session, p: &Json) -> CmdResult {
let id = s.layer_id(p)?;
let row = p.get("row").and_then(Json::as_u64).ok_or("row is required")? as usize;
let g = p.get("geometry").and_then(rustgis_data::io::geometry_from_geojson).ok_or("geometry must be a GeoJSON geometry")?;
replace_geometry(s, id, row, g)?;
Ok(Json::Null)
}

/// Moves, inserts or deletes one vertex of a feature: one edit, through the same checks and undo
/// as `edit.setGeometry`.
fn edit_vertex(s: &mut Session, p: &Json) -> CmdResult {
let id = s.layer_id(p)?;
let row = p.get("row").and_then(Json::as_u64).ok_or("row is required")? as usize;
// Absent is 0 (part and ring); anything else must be a whole number, never quietly 0.
let num = |k: &str| -> Result<Option<usize>, String> {
match p.get(k) {
None | Some(Json::Null) => Ok(None),
Some(v) => v.as_u64().map(|n| Some(n as usize)).ok_or_else(|| format!("{k} must be a whole number")),
}
};
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))
};
let g = s
.map()
.layer(id)
.and_then(|l| l.dataset.features.get(row))
.ok_or("no such row")?
.geometry
.clone()
.ok_or("the feature has no shape")?;
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}'")),
};
replace_geometry(s, id, row, g)?;
Ok(json!({"part": v.part, "ring": v.ring, "index": v.index}))
}

/// Replaces a feature's shape with one of the layer's kind, as one edit.
fn replace_geometry(s: &mut Session, id: u32, row: usize, g: Geometry) -> Result<(), String> {
super::edit::validate(&g)?;
let l = s.map_mut().layer_mut(id).ok_or("no such layer")?;
if l.geometry_type() != GeometryType::None && l.geometry_type() != g.kind() {
Expand All @@ -442,8 +492,7 @@ fn set_geometry(s: &mut Session, p: &Json) -> CmdResult {
let before = f.clone();
f.geometry = Some(g);
l.touch_geometry();
apply(s, id, vec![Change { before: Some(before), row: Some(row) }])?;
Ok(Json::Null)
apply(s, id, vec![Change { before: Some(before), row: Some(row) }])
}

/// The one layer holding the selection.
Expand Down Expand Up @@ -985,6 +1034,12 @@ fn snap(s: &mut Session, p: &Json) -> CmdResult {
/// Where a point at `c` (longitude/latitude) snaps to, and to what, if anywhere. Read-only, for
/// drawing the snap marker without running a command (projected shapes are cached on the way).
pub fn snap_at(s: &mut Session, c: Coord) -> Option<(Coord, &'static str)> {
snap_except(s, c, None)
}

/// [`snap_at`], leaving out one feature (layer id, row): a vertex being dragged doesn't snap to
/// the shape it is changing.
pub fn snap_except(s: &mut Session, c: Coord, skip: Option<(u32, usize)>) -> Option<(Coord, &'static str)> {
let sn = s.map().editing.snapping;
if !sn.enabled {
return None;
Expand All @@ -1005,7 +1060,9 @@ pub fn snap_at(s: &mut Session, c: Coord) -> Option<(Coord, &'static str)> {
if !l.in_scale_range(scale) {
continue;
}
for g in l.projected_in(&flat, &key).iter().flatten() {
let id = l.id;
for (row, g) in l.projected_in(&flat, &key).iter().enumerate() {
let Some(g) = g.as_ref().filter(|_| skip != Some((id, row))) else { continue };
if !g.extent().padded(0.0, tol).contains(m) {
continue;
}
Expand Down
5 changes: 4 additions & 1 deletion crates/engine/src/cmd/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn commands() -> Vec<C> {
.when(|s| if s.can_redo() { None } else { Some("nothing to redo") }),
C::new("tool.set", "Set Map Tool", "Home › Navigate", set_tool)
.view()
.params(r#"{"tool": "explore"|"select"|"zoomIn"|"measure"|"measureArea"|"createPoint"|"createLine"|"createPolygon"|"graphicPoint"|"graphicText"|"graphicLine"|"graphicCurve"|"graphicFreehand"|"graphicPolygon"|"graphicLasso"|"graphicRectangle"|"graphicCircle"|"graphicEllipse"|"graphicPicture"}"#),
.params(r#"{"tool": "explore"|"select"|"zoomIn"|"measure"|"measureArea"|"createPoint"|"createLine"|"createPolygon"|"editVertices"|"graphicPoint"|"graphicText"|"graphicLine"|"graphicCurve"|"graphicFreehand"|"graphicPolygon"|"graphicLasso"|"graphicRectangle"|"graphicCircle"|"graphicEllipse"|"graphicPicture"}"#),
C::new("info.project", "Project Info", "Project", info).view(),
C::new("info.commands", "List Commands", "Help", list).view(),
]
Expand Down Expand Up @@ -161,6 +161,9 @@ fn set_tool(s: &mut Session, p: &Value) -> CmdResult {
return Err(format!("{} needs a {} layer", t.label(), t.label().trim_start_matches("Create ").to_lowercase()));
}
}
if t == Tool::EditVertices && !s.map().editing.settings.enabled {
return Err("editing is turned off (Edit › Edit)".into());
}
s.tool = t;
Ok(Value::Null)
}
Expand Down
5 changes: 5 additions & 0 deletions crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ pub enum Tool {
CreatePoint,
CreateLine,
CreatePolygon,
/// Moves, inserts and deletes the vertices of the selected features.
EditVertices,
/// Draws a graphic: the shape is one of [`GRAPHIC_TOOLS`]' shapes.
Graphic(&'static str),
}
Expand Down Expand Up @@ -185,6 +187,7 @@ impl Tool {
Tool::CreatePoint => "createPoint",
Tool::CreateLine => "createLine",
Tool::CreatePolygon => "createPolygon",
Tool::EditVertices => "editVertices",
Tool::Graphic(shape) => GRAPHIC_TOOLS.iter().find(|g| g.0 == shape).map_or("graphicPoint", |g| g.1),
}
}
Expand All @@ -201,6 +204,7 @@ impl Tool {
Tool::CreatePoint,
Tool::CreateLine,
Tool::CreatePolygon,
Tool::EditVertices,
]
.into_iter()
.find(|t| t.code().eq_ignore_ascii_case(s.trim()))
Expand All @@ -215,6 +219,7 @@ impl Tool {
Tool::CreatePoint => "Create Point",
Tool::CreateLine => "Create Line",
Tool::CreatePolygon => "Create Polygon",
Tool::EditVertices => "Edit Vertices",
Tool::Graphic(shape) => GRAPHIC_TOOLS.iter().find(|g| g.0 == shape).map_or("Point", |g| g.2),
}
}
Expand Down
Loading
Loading