0

我有两种方法可以计算 python 中地理坐标之间的距离:

from pyproj import Proj
import math

def calc_distance(lat1, lon1, lat2, lon2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    """
    # convert decimal degrees to radians
    lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])

    # haversine formula
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
    c = 2 * math.asin(math.sqrt(a))
    km = 6371 * c

    return km

def calc_distance_convert_utm(lat1, lon1, lat2, lon2):
    myProj = Proj("+proj=utm +zone=42, +north +ellps=WGS84 +datum=WGS84 +units=m +no_defs")

    # convert to utm 
    utm_x1, utm_y1 = myProj(lat1, lon1)
    utm_x2, utm_y2 = myProj(lat2, lon2)

    diff_x = abs(utm_x1 - utm_x2)
    diff_y = abs(utm_y1 - utm_y2)

    distance = math.sqrt(diff_x**2 + diff_y**2)

    return distance

我用以下值调用它:

lat1 = 34.866527
lon1 = 69.674606
lat2 = 34.864990
lon2 = 69.657655
print "approximation method: ", calc_distance(lat1, lon1, lat2, lon2)
print "converting to utm method: ", calc_distance_convert_utm(lat1, lon1, lat2, lon2)

但是,如果我比较结果,我会得到两个不同的值:

approximation method:  1.55593476881
converting to utm method:  1928.21537269

请注意,第一种方法以公里为单位返回距离,而第二种方法以米为单位返回。我已经将结果与您可以在网上找到的距离计算器进行了比较,似乎第一种方法(近似方法)是“更正确”的答案,因为这是大多数在线计算器返回的值。我想知道,为什么第二种方法(首先转换为 utm)没有返回更相似的结果(类似于 1555.9347...)。我有将近 0.5 公里的差异,这对我来说似乎很重要。

我做错什么了吗?任何帮助表示赞赏!谢谢

4

1 回答 1

0

我发现了错误……在 utm 转换方法中,我在转换过程中切换了纬度/经度值。它应该是:

utm_x1, utm_y1 = myProj(lon1, lat1)
utm_x2, utm_y2 = myProj(lon2, lat2)
于 2017-07-07T09:35:32.103 回答