当经度>时,Python纬度/经度中点计算会得出错误的结果90 [英] Python lat/long midpoint calculation gives wrong result when longitude > 90

查看:85
本文介绍了当经度>时,Python纬度/经度中点计算会得出错误的结果90的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在给定两端的点的经度和纬度时,我有一个短函数来计算线的中点的问题.简单地说,当经度大于-90度或小于90度时,它可以正常工作.对于地球的另一半,它提供了某种随机结果.

I have a problem with a short function to calculate the midpoint of a line when given the latitude and longitude of the points at each end. To put it simply, it works correctly when the longitude is greater than -90 degrees or less than 90 degrees. For the other half of the planet, it provides a somewhat random result.

该代码是 http://www中提供的javascript的python转换. .movable-type.co.uk/scripts/latlong.html ,并且似乎符合更正后的版本此处.与两个stackoverflow版本进行比较时,我承认我没有使用C#或Java进行编码,但是我无法发现错误所在.

The code is a python conversion of javascript provided at http://www.movable-type.co.uk/scripts/latlong.html, and appears to conform to the corrected versions here and here. When comparing with the two stackoverflow versions, I'll admit I don't code in C# or Java, but I can't spot where my error is.

代码如下:

#!/usr/bin/python

import math

def midpoint(p1, p2):
   lat1, lat2 = math.radians(p1[0]), math.radians(p2[0])
   lon1, lon2 = math.radians(p1[1]), math.radians(p2[1])
   dlon = lon2 - lon1
   dx = math.cos(lat2) * math.cos(dlon)
   dy = math.cos(lat2) * math.sin(dlon)
   lat3 = math.atan2(math.sin(lat1) + math.sin(lat2), math.sqrt((math.cos(lat1) + dx) * (math.cos(lat1) + dx) + dy * dy))
   lon3 = lon1 + math.atan2(dy, math.cos(lat1) + dx)
   return(math.degrees(lat3), math.degrees(lon3))

p1 = (6.4, 45)
p2 = (7.3, 43.5)
print "Correct:", midpoint(p1, p2)

p1 = (95.5,41.4)
p2 = (96.3,41.8)
print "Wrong:", midpoint(p1, p2)

有什么建议吗?

推荐答案

通过以下方式替换您的arg设置代码:

Replace your arg set up code by:

lat1, lon1 = p1
lat2, lon2 = p2
assert -90 <= lat1 <= 90
assert -90 <= lat2 <= 90
assert -180 <= lon1 <= 180
assert -180 <= lon2 <= 180
lat1, lon1, lat2, lon2 = map(math.radians, (lat1, lon1, lat2, lon2))

然后再次运行您的代码.

and run your code again.

更新有关涉及经度/纬度的计算的一些有用的一般建议:

Update A few hopefully-helpful general suggestions about calculations involving latitude/longitude:

  1. 以度或弧度为单位输入纬度/经度吗?
  2. 检查输入纬度/经度是否有效
  3. 检查输出纬度/经度"的有效范围.经度在国际日期轴上是不连续的.

可以有效地更改中点例程的最后一部分,以避免长距离使用时可能出现的问题:

The last part of the midpoint routine could be usefully changed to avoid a potential problem with long-distance use:

lon3 = lon1 + math.atan2(dy, math.cos(lat1) + dx)
# replacement code follows:
lon3d = math.degrees(lon3)
if lon3d < -180:
    print "oops1", lon3d
    lon3d += 360
elif lon3d > 180:
    print "oops2", lon3d
    lon3d -= 360
return(math.degrees(lat3), lon3d)

例如,找到新西兰奥克兰(-36.9,174.8)和大溪地帕皮提(-17.5,-149.5)之间的中点,便会在得到有效答案(-28.355951246746923, -165.72956909809082)

For example, finding a midpoint between Auckland, New Zealand (-36.9, 174.8) and Papeete, Tahiti (-17.5, -149.5) produces oops2 194.270430902 on the way to a valid answer (-28.355951246746923, -165.72956909809082)

这篇关于当经度&gt;时,Python纬度/经度中点计算会得出错误的结果90的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆