rgisr-sfrgeo

Compute pairwise distances between two layers


In R, I have polygon layer A and point layer B. Both have the same number of features, there is one-to-one correspondence (pairing): each polygon of the layer A has a corresponding point in layer B and vice versa. How do I compute pairwise distances between these pairs of features?

I tried to use

gDistance(A, B, byid = TRUE)

but this will compute the whole N2 matrix, which is huge overhead (I would only use the diagonal of that matrix).


Solution

  • Unfortunatelly, seems rgeos and gDistance can't do it... So I found solution using the sf library:

    require(sf)
    g1 <- st_as_sf(A)
    g2 <- st_as_sf(B)
    dist <- st_distance(g1, g2, by_element=TRUE)
    

    This is going to generate pairwise distances.