I use R
(and sf
, mainly) and I was wondering if it's possible to create Voronoi polygons starting from LINESTRING
objects instead of POINT
. For example, given a sfc
such as
# package
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
# example linestring
my_linestring_sfc <- st_sfc(
st_linestring(matrix(c(-2, -2, -1, -1, 0, 0), ncol = 2, byrow = TRUE)),
st_linestring(matrix(c(2, -2, 1, -1, 0, 0), ncol = 2, byrow = TRUE)),
st_linestring(matrix(c(0, 0, 0, 1, 0, 2), ncol = 2, byrow = TRUE))
)
par(mar = rep(0, 4))
plot(my_linestring_sfc, reset = FALSE)
plot(st_cast(my_linestring_sfc, "POINT"), add = TRUE, pch = 16)
I can create the Voronoi polygons associated with its points using the following code:
my_voronoi <- st_voronoi(do.call("c", st_cast(my_linestring_sfc, "POINT"))) %>%
st_collection_extract()
plot(my_voronoi, reset = FALSE)
plot(my_linestring_sfc, add = TRUE)
plot(st_cast(my_linestring_sfc, "POINT"), add = TRUE, pch = 16)
Created on 2020-04-11 by the reprex package (v0.3.0)
and I can merge the polygons according to their intersection with the original linestrings. The problem is that this tessellation does not correspond to my "intended" result since the middle polygon is shared between the three lines. The ideal result should be as follows (which I created manually):
Created on 2020-04-11 by the reprex package (v0.3.0)
I read a few SO posts on this topic. This is a solution with Mathematica software while this is another solution with ArcGis, and they both use the Euclidean Allocation. In this other question they suggest "Segment Voronoi Diagrams" and CGAL. This looks like the most promising solution using PostGIS, but I have no idea how to translate that code into R. Is it possible to adapt those ideas to my problem?
Note: Check here since maybe it's possible to adapt etiennebr approach to the previous PostGIS answer.