You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The split function allows us to split a polygon by a line.
Currently, the first input needs to be a polygon, and the second needs to be a line.
It would be nice to include the option to split a line by another object (line or point).
For someone interested: There is a workaround using terra's ''aggregate'' function and lwgeom's ''st_split''.
library(terra)
line1 <- vect("LINESTRING (0 0, 2 2)")
line2 <- vect("LINESTRING (0 2, 2 0)")
line3 <- vect("LINESTRING (1 0, 1 2)")
# Combine lines into a single SpatVector
lines <- rbind(line1, line2, line3)
lines$id <- 1:nrow(lines)
plot(lines, y = "id", lwd = 2)
# We make this into a function
split_lines <- function(lines) {
# Aggregate the input lines
agg_lines <- aggregate(lines)
# Find the points that were added
points_before <- as.points(lines)
points_after <- as.points(agg_lines)
points_added <- erase(points_after, points_before)
# Split the lines at the added points
split_lines <- sf::st_collection_extract(lwgeom::st_split(sf::st_as_sf(lines), sf::st_as_sf(points_added)), "LINESTRING")
split_lines$new_id <- 1:nrow(split_lines)
return(vect(split_lines))
}
# We test the function
out = split_lines(lines)
plot(out, y = "new_id", lwd = 2)
The split function allows us to split a polygon by a line.
Currently, the first input needs to be a polygon, and the second needs to be a line.
It would be nice to include the option to split a line by another object (line or point).
Something along these lines:
The text was updated successfully, but these errors were encountered: