简体   繁体   English

如何计算两个地理坐标的距离?

[英]How to compute distance of two geographical coordinates?

I read this question and implemented the accepted answer in Python (see below). 我阅读了这个问题,并在Python中实现了可接受的答案(见下文)。 It works in principle, but the results are consistently about 30% higher than expected (Czech Republic) - is that the expected accuracy of this algorithm? 它原则上可以工作,但是结果始终比预期高出约30%(捷克共和国)-这是该算法的预期准确性吗?

To verify the algorithm, I used BoundingBox to get a bounding box with a known diagonal distance (building, two cities) and used the output coordinates as input for "my" algorithm. 为了验证该算法,我使用BoundingBox来获得一个具有已知对角线距离(建筑物,两个城市)的边界框,并将输出坐标用作“ my”算法的输入。

Where is the problem? 问题出在哪儿?

  • my implementation? 我的实施?
  • the algorithm itself? 算法本身?
  • Python? 蟒蛇?
  • testing? 测试?

My implementation: 我的实现:

R= 6371 #km
dLat = math.radians(lat2-lat1)
dLon = math.radians(lon2-lon1)
lat1 = math.radians(lat1)
lat2 = math.radians(lat2)

a= math.sin(dLat/2)*math.sin(dLat/2) + math.sin(dLon/2) * math.sin(dLon/2) * math.cos(lat1) * math.cos(lat2)
c= 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = R * c;
return d

No, the algorithm is not supposed to have an error of that magnitude. 不,该算法不应具有该数量级的误差。 The link specifies that you can expect around a 0.3% error. 该链接指定您可以预期出现0.3%左右的错误。

I can not reproduce your results with your code, so I believe the error is with your testing. 我无法使用您的代码重现您的结果,因此我认为错误在于您的测试。

Here's some testing data from a site with the distance between and coordinates of Prague and Brno in decimal degrees format: 以下是一些站点的测试数据,其中布拉格和布尔诺之间的距离和坐标为十进制格式:

lat_prague, long_prague = 50.0833, 14.4667
lat_brno, long_brno = 49.2000, 16.6333
expected_km = 184.21

Here are the testing results: 测试结果如下:

>>> def calc(lat1,lon1, lat2,lon2):
# ... your code ...

>>> calc(lat_prague,long_prague,lat_brno,long_brno)
184.34019283649852
>>> calc(lat_prague,long_prague,lat_brno,long_brno) / expected_km
1.0007067631317437

A wild guess: for locations in the Czech Republic, the error you're getting seems in the right order of magnitude for with mixing up latitude and longitude: 一个大胆的猜测:对于捷克共和国的位置,您所得到的错误似乎是将纬度和经度混合在一起的正确数量级:

>>> calc(long_prague,lat_prague,long_brno,lat_brno)
258.8286271447481
>>> calc(long_prague,lat_prague,long_brno,lat_brno) / expected_km
1.405073704710646

This is apparently a known confusion . 这显然是已知的混乱 A coordinate specified as only a pair of numbers is ambiguous (for instance: both BoundingBox and the reference for the distance above use (long, lat), and the algorithm uses the ordering lat, long). 仅指定为一对数字的坐标是模棱两可的(例如:BoundingBox和上面距离的参考都使用(长,纬度),并且算法使用lat,long排序)。 When you come across the ambiguous format with an unfamiliar data source without a formal specification, you'll just have to sanity-check. 如果您在使用不熟悉的数据源而没有正式规范的情况下遇到含糊不清的格式,则只需要进行完整性检查即可。 Sites like Wikipedia will tell you unambiguously that Prague lies at "50°05′N 14°25′E" -- that is, very roughly, around 50 degrees latitude (north-south) and 14 degrees longitude (east-west). Wikipedia这样的网站都会明确地告诉您,布拉格位于“ 50°05′N 14°25′E”处-也就是说,大致来说,北纬50度(东西)和北纬14度(东西)。

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

相关问题 如何使用python获取两个地理坐标之间的行驶距离? - How to get the driving distance between two geographical coordinates using python? 计算来自不同行的两个坐标之间的距离 - Compute the distance between two coordinates from different rows 如何让我的 for 循环计算列表中坐标之间的距离 - how to get my for loop to compute the distance between coordinates in list 如何计算 Python 中两个 Z8​​C578DE37278ADA488D763EA86C5CF20Z 坐标(纬度和经度)之间的 RMSE - How to compute RMSE between two GPS coordinates (latitude and longitude) in Python 如何使用OSM和python查找两个坐标之间的路径和距离? - How to find path and distance between two coordinates using OSM and python? 如何根据两个坐标之间的距离附加列表? - How to append a list based on the distance between two coordinates? 散点图地理坐标 - Scatter plotting geographical coordinates 如何获取邮政编码,提供地理坐标,在 python 中使用地理编码器 - How to get the postal code, providing the geographical coordinates, using geocoder in python 计算Python中GPS坐标序列的距离(米)。 - Compute distance in meters of sequence of GPS coordinates in Python. 计算两个熊猫数据框的字符串之间的距离 - Compute distance between strings of two pandas DataFrames
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM