简体   繁体   中英

Distm function for calculate distance between coordinates in R

Friends could help me calculate the distance using the distm function for the code below. I have two databases, one containing properties coordinates and the other industry coordinates. I would like to use the distm function to calculate the distance between the two bases. More precisely, from property 1 to industry 1, property 2, to industry 1, and so on. Any help is appreciated.

Thank you very much!

df<-structure(list(Properties = c(1,2,3,4), 
                     Latitude = c(-23.8, -23.8, -23.9, -23.9), 
                     Longitude = c(-49.6, -49.3, -49.4, -49.8), 
                     Waste = c(526, 350, 526, 469)), class = "data.frame", row.names = c(NA, -4L))

df1<-structure(list(Industry = c(1,2,3,4), Latitude = c(-23.4, -23.7, -23.4, -23.8), 
                     Longitude = c(-49.7, -49.4, -49.6, -49.7)), class = "data.frame", row.names = c(NA, -4L))

This should work:

library(geosphere)

distm(df[,c('Longitude','Latitude')],
      df1[,c('Longitude','Latitude')],
             fun=distVincentyEllipsoid)

         [,1]     [,2]     [,3]     [,4]
[1,] 45461.49 23203.37 44300.99 10190.84
[2,] 60243.58 15053.19 53852.61 40763.35
[3,] 63272.26 22151.07 59016.34 32505.87
[4,] 56308.59 46393.08 59016.34 15048.01

The first row indicates the distance between property 1 and industries 1, 2, 3 and 4.

See also here:

Function to calculate geospatial distance between two points (lat,long) using R

Geographic / geospatial distance between 2 lists of lat/lon points (coordinates)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM