I'm attempting to create a table of polygon neighbors based on a Voronoi tessellation (aka Dirichlet tessellations or Thiessen polygons) generated by the dirichlet()
function of the spatstat
library. For example, in the figure below, the upper right and bottom right tiles would each have 2 neighbors, the center right tile 4 neighbors, and the remaining two tiles 3 neighbors each. I want to capture the neighbor pairs in a table, and ideally capture the length of the boundary line that they share: e.g., 'Tile1', 'Tile2', 'shared_edge_length'.
Initially, I attempted to loop through and compare each pair of polygons in the tessellation using intersect.tess()
, intersect.own()
, as well as polyclip
functions, but I'm guessing these don't work because the tiles are by definition not overlapping in area, despite sharing edges. Is there a simple function to accomplish this (the alternative being to loop through the $bdry
points perhaps)? It seems like the regeos
package has gTouches
, but I couldn't find something similar for spatstat
.
This is my current non-working approach:
library(spatstat)
points <- ppp(x=c(-77.308703, -77.256582, -77.290600, -77.135668, -77.097144),
y=c(39.288603, 39.147019, 39.372818, 39.401898, 39.689203),
window=owin(xrange=c(-77.7,-77), yrange=c(39.1, 39.7)))
vt <- dirichlet(points) # Dirichlet tesselation
plot(vt)
tilesA <- tiles(vt)
n_tiles <- length(tilesA)
boundary_calcs <- data.frame('area1_id'=numeric(), 'area2_id'=numeric(), 'neighbor'=logical()) # Store boundary pairs
for (i in 1:n_tiles) {
for (j in 1:n_tiles) {
intersection <- intersect.owin(tilesA[[i]], tilesA[[j]], fatal=FALSE) # does not work
if (!is.empty(intersection)) {
boundary_calcs[nrow(boundary_calcs)+1, ] <- c(i, j, TRUE) # add to data table as new row
} } }