Open mdsumner opened 2 years ago
here's some sf helpers, segments and triangles (to avoid closing vertex dupe) as linestrings
sf_segment <- function(x) {
sc <- silicate::SC0(x)
topol <- silicate::sc_object(sc)$topology_
idx <- t(as.matrix(do.call(rbind, topol)[c(".vx0", ".vx1")]))
d <- silicate::sc_vertex(sc)[as.vector(idx), c("x_", "y_")]
d$feature_id <- rep(seq_len(dim(idx)[2L]), each = 2L)
out <- sfheaders::sf_linestring(d, x = "x_", y = "y_", linestring_id = "feature_id")
out$feature_id <- rep(seq_along(topol), unlist(lapply(topol, nrow)))
out
}
sf_triangle <- function(x, verts = c("x_", "y_")) {
if (inherits(x, "TRI0")) {
## might alread be sc DEL0 or TRI0
sc <- x
} else {
sc <- silicate::TRI0(x)
}
topol <- silicate::sc_object(sc)$topology_
idx <- t(as.matrix(do.call(rbind, topol)[c(".vx0", ".vx1", ".vx2")]))
d <- silicate::sc_vertex(sc)[as.vector(idx), verts]
d$feature_id <- rep(seq_len(dim(idx)[2L]), each = 3L)
out <- sfheaders::sf_linestring(d, x = verts[1], y = verts[2L], linestring_id = "feature_id")
out$feature_id <- rep(seq_along(topol), unlist(lapply(topol, nrow)))
out
}
example(read_sf)
plot(sf_segment(nc))
plot(sf_triangle(minimal_mesh))
plot(sf_triangle(anglr::DEL0(nc, max_area = .02)))
what are the key pathways? for example, turning sf polygons or lines into segments does not require vertex deduplication, so rather than go through SC0 every time we really only need the map of paths (number of coordinates in each), to use as a grouping
I think fleshing that list out, in light of traipse, rayshader, mapdeck, and various discussions around the place is worthwhile - like, when do you want segments? sometimes you want them all and to know what polygon they belong to, other times you need an actual mesh of segments for planar partition ... this is where sf fails us, we don't want to copy out every vertex instance ...
above use 'parcels_hobart' i.e.
also see for a more exhaustive exploration: https://github.com/dcooley/geometries/issues/4