data(utrecht_district); data(graph_utrecht_district)
coord <- sf::st_coordinates(sf::st_centroid(utrecht_district))
adj_municip <- as(as(igraph::as_adjacency_matrix(graph_utrecht_district, type = "both"),
"symmetricMatrix"),
"TsparseMatrix")
edge_list <- data.frame(adj_municip@i + 1, adj_municip@j + 1)
segment_df <- cbind(coord[edge_list[, 1], ], coord[edge_list[, 2], ])
ptmat <- as.matrix(segment_df[, 1:4])[2:nrow(segment_df), ]
linesegs <- lapply(split(ptmat, 1:nrow(ptmat)), function(x) {
x <- matrix(x, nrow = 2, byrow = TRUE)
x <- sf::st_linestring(x)})
final_sf <- sf::st_sf(sf::st_sfc(linesegs), 'ID' = 1:length(sf::st_sfc(linesegs)))
op <- par(mar = rep(0, 4))
plot(sf::st_geometry(utrecht_district), lwd = 0.6, border = "grey")
plot(sf::st_geometry(final_sf), lwd = 0.5, add = TRUE)
plot(sf::st_centroid(utrecht_district), add = TRUE, col = "black", pch = 20,
cex = 0.5)
par(op)
Run the code above in your browser using DataLab