r iterating a function across two dataframes to compute spatial distances between lon/lat points

34 Views Asked by At

I have two dataframes. 'Origins' contains X pairs of longitude/latitude coordinates, and 'Destinations' contains five pairs of coordinates for locations of interest. For each lon/lat pair in Origins, I want to compute the distances to each of the five lon/lat pairs in Destinations. Note that the five locations of interest in Destinations are the same at every iteration of Origins. This is trivial with a loop but I have X=500,000 Origins coordinates to process for each of the five Destinations coordinates and something faster is needed. For this example, we'll say Origins has two rows.

Origins <- 
  data.frame(Lon1 = c(-100, -50),
             Lat1 = c(20, 10))

Destinations <-
  data.frame(Lon2 = c(-91, -151, -139, -140, -100),
             Lat2 = c(9, 10, 11, 8, 5))

The result I want should have N=2 rows, one for each row of Origins, and N=5 columns containing the computed distances from the Origin in that row to each of the five Destinations.

geosphere::distRhumb() calculates the distance between two points (pairs of lon/lat coordinates), p1 and p2.

Using apply() by row of Origins seems like the way forward but I'm failing badly at specifying the syntax. Most of the time I just get errors. The following configuration at least executes but just returns the distance between the two coordinates listed in Origins:

apply(Origins, 1, function(Destinations){
      geosphere::distRhumb(p1 = Origins, p2 = Destinations)})

Any help is gratefully appreciated.

1

There are 1 best solutions below

1
Onyambu On BEST ANSWER

Use the function below:

distRhumb_vec <- function (p1, p2, r = 6378137) {
    n <-  nrow(p1)
    p1 <- as.matrix(p1)[gl(n, nrow(p2)), ]
    matrix(geosphere::distRhumb(p1,p2), n, byrow = TRUE)
}

distRhumb_vec(Origins, Destinations)

      [,1]     [,2]    [,3]    [,4]    [,5]
[1,] 1561085  5587883 4297025 4513817 1669792
[2,] 4502821 11072458 9742038 9897383 5544553