有没有一种方法可以计算 TERRAIN 上两个坐标之间的距离,而不是像乌鸦飞一样?

Is there a way to calculate distance between two coordinates ON TERRAIN and not as the crow flies?

我正在寻找一种方法来找到也具有高程数据的两个坐标之间的距离,但我希望解决方案考虑到两点之间的地形,即它们在地面上和不是 "as the crow flies"。

我已经找到并使用了距离 "in the air" 的 Haversine 公式,但显然它不会产生一个人在地面上行走的实际距离,因为在两点之间可能有一个斜坡上升和/或下降。点之间的距离越大,误差范围就越大。

我采样了一个真实的 .gpx 文件并将其数据转换为 JSON 格式。它看起来像这样:

let jsonSample = [{
    "lat": "57.107297", // 0
    "lon": "-5.334734",
    "ele": "957.00"
  }, // distance between 0 and 1 =>  169.1849929515954 m (as the crow flies) elevation difference: 50.210000000000036
  {
    "lat": "57.106590", // 1
    "lon": "-5.332253",
    "ele": "1007.21"
  }, // distance between 1 and 2 => 162.49601252670058  m (as the crow flies) elevation difference: 23.789999999999964
  {
    "lat": "57.105537", // 2 (summit)
    "lon": "-5.330387",
    "ele": "1031.00"
  }, // distance between 2 and 3 =>  32.45395539826568 m (as the crow flies) elevation difference: -13
  {
    "lat": "57.105284", // 3
    "lon": "-5.330119",
    "ele": "1018.00"
  }];

实际距离应该比用Haversine公式计算的距离长。但是我该如何计算呢?

Google "haversine formula elevation" 找到了我 this Math.SE question and answer

注意评论,只要您测量的距离相距不太远,使用毕达哥拉斯定理,您可以添加高程增量。

I'd use Pythagoras: let d the Haversine distance and dh the difference in altitudes, D=sqrt(d**2+dh**2).

类似

function haversineWithAltitude(lat1, lon1, alt1, lat2, lon2, alt2) {
    const groundDistanceInMeters = haversine(lat1, lon1, lat2, lon2);
    const elevationDifferenceMeters = Math.abs(alt1 - alt2);
    return Math.sqrt(groundDistanceInMeters * groundDistanceInMeters + elevationDifferenceInMeters * elevationDifferenceInMeters);
}

因此应该可以工作——但如前所述,如果点之间的长度很长,这将不可避免地变得更加不准确。 (那时,我不是数学家,也不知道更多,将长腿分段并分段计算每个距离。)