11

Is there a package which allows to compute the spatial distance between two points taking into account the elevation. So for each point, we would have latitude, longitude and elevation. So far, I had to write the following function:

library(geosphere)  
distance3D <- function (point1, point2) {
      planiDist <- distm(point1[1:2], point2[1:2])
      altiDist <- point2[3] - point1[3]
      dist3D <- sqrt(planiDist^2+altiDist^2)
      return(dist3D)
    }

I was just wondering if one function existed in one of the R packages.

Liky
  • 1,105
  • 2
  • 11
  • 32
  • 1
    There's a link to FORTRAN code to do that here: https://stackoverflow.com/questions/11710972/great-circle-distance-plus-altitude-change. Apparently, you can run fortran functions in R: https://www.r-bloggers.com/fortran-and-r-speed-things-up/, but I've never done it. You may already know this, but your current function isn't accurate. You can't use the euclidian distance formula for points on a sphere. If you draw the "triangle" (not really, since the lines are curved) of the equal-height path, and the point to point path, you'll see that the line connecting them isn't orthogonal. – IceCreamToucan Jun 28 '18 at 23:17
  • Definitively, correct! My calculation is not accurate. I am doing an approximation here considering I have a triangle. – Liky Jun 28 '18 at 23:48
  • I found [this post](https://stackoverflow.com/questions/1108965/taking-altitude-into-account-when-calculating-geodesic-distance) on SO about this subject. Might be of use. – phiver Jun 29 '18 at 08:51
  • This [post](https://gis.stackexchange.com/questions/140272/calculating-distance-between-set-of-points-lat-and-long-in-r) can be usefull! – Adelmo Filho Aug 20 '18 at 01:13
  • @Koot6133, the link you refer too works when having XYZ coordinates. In my case, I have latitude, longitude and elevation. – Liky Sep 18 '18 at 09:37
  • This [post](https://gis.stackexchange.com/questions/252374/measuring-distance-between-two-points-considering-elevation-raster) goes over how to accomplish this with the RQGIS package. –  Sep 19 '18 at 19:30
  • You can use the haversine https://rdrr.io/github/michaelmalick/r-malick/src/R/haversine.R but that doesn't take into account altitude which you could sort out by using euclidean distance – Vivek Katial Sep 27 '18 at 08:48

1 Answers1

0

The haversine great circle distance might be what you are looking for.

library(geosphere)
distHaversine(p1, p2, r=6378137)


#p1, p2-longitude/latitude of point(s). Can be a vector of two numbers, 
     a matrix of 2    columns (first one is longitude, second is latitude) 
     or a   SpatialPoints* object
#r-radius of the earth; default = 6378137 m