1

I have this Python function that computes the great-circle distance between two points, but I want to modify it so that a third parameter, altitude, can be incorporated into the Haversine formula.

from math import cos, sin, atan2, radians, sqrt

def findDistance(p, p2):
  R = 3959
  lat1 = radians(p[0])
  lon1 = radians(p[1])
  lat2 = radians(p2[0])
  lon2 = radians(p2[1])
  dlat = lat2 - lat1
  dlon = lon2 - lon1
  a = (sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2))**2
  c = 2 * atan2(sqrt(a), sqrt(1-a))
  return R * c

p and p2 are of the form (latitude, longitude, altitude)

Is there a way to modify the Haversine formula to take into account an altitude difference between two points?

aweeeezy
  • 111
  • How do you suppose a great circle would vary across two points with different altitudes? – J. M. ain't a mathematician Dec 28 '16 at 18:11
  • At first I was thinking that it might be possible to scrape the Haversine formula and just compute the Euclidean distance between the two points -- I guess that would work for most cases since the data points have randomly generated altitudes between 0 and 1200 miles...I was just hoping come up with something that would work for instances where the two points are closer to the surface of the earth. – aweeeezy Dec 28 '16 at 18:18
  • Think of two nearby mountains, with a valley between them. Is the distance you want from one peak to another ("as the crow flies"), or do you want the total distance going down from one peak, through the valley, and up to the other peak? – J. M. ain't a mathematician Dec 28 '16 at 18:27
  • "as the crow flies" -- like I said, I suppose for most cases the Euclidean distance would work just fine, but I just wanted to address the case where the two points are low enough in altitude such that they lie below the curvature of the earth (if that makes sense). – aweeeezy Dec 28 '16 at 18:31
  • 1
    I'd use Pythagoras: let d the Haversine distance and dh the difference in altitudes, D=sqrt(d**2+dh**2). – N74 Dec 28 '16 at 18:38
  • Thanks @N74 -- I think that'll do the job well. – aweeeezy Dec 28 '16 at 18:46
  • @N74: to reuse my mountain example: what if you have one mountain at the north pole, and one mountain at the south pole? – J. M. ain't a mathematician Dec 28 '16 at 18:51
  • @JM as you are noticing my answer does not follow your comment. Anyway for two mountains at the poles you'll have half great circle as distance (plus a delta given by the difference in height of the mountains). My reasoning does not give the minimum distance in your first example, as it gives the length of an arc that is longer than the line of sight distance. – N74 Dec 28 '16 at 19:30

0 Answers0