[英]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: 我的实现:
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.