简体   繁体   English

通过转换为utm计算纬度/经度距离与使用近似方法得出的结果不同

[英]Lat/lon distance calculation by converting to utm gives different results than using approximate method

I've got two methods for calculating the distance between georeferenced coordinates in python: 我有两种方法可以计算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

Which I call with the following values: 我称其为以下值:

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)

However, If I compare the results, I get two different values: 但是,如果比较结果,则会得到两个不同的值:

approximation method:  1.55593476881
converting to utm method:  1928.21537269

Note, that the first method returns the distance in kilometers while the second returns it in meters. 请注意,第一种方法以公里为单位返回距离,第二种方法以米为单位返回距离。 I've compared the result with distance calculators which you can find online, and it seems that the first method (approximation method) is the "more correct" answer as this is the value most online calculators return. 我将结果与可以在网上找到的距离计算器进行了比较,似乎第一种方法(近似方法)是“更正确”的答案,因为这是大多数在线计算器返回的值。 I wonder, why the second method (converting to utm first) does not return a more similar result (something like 1555.9347...). 我想知道为什么第二种方法(首先转换为utm)不会返回更相似的结果(例如1555.9347 ...)。 I have a difference of almost 0.5 km which seems pretty much to me. 我相差约0.5公里,对我来说似乎差不多。

Did I do anything wrong? 我做错了吗? Any help is appreciated! 任何帮助表示赞赏! Thanks 谢谢

I've found the error ... In the utm converting method I've switched the lat/lon values in the conversion process. 我发现了错误...在utm转换方法中,我在转换过程中切换了经/纬度值。 It should be: 它应该是:

utm_x1, utm_y1 = myProj(lon1, lat1)
utm_x2, utm_y2 = myProj(lon2, lat2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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