如何在 Python(3) 中以一定速度在两个地理位置之间迭代

How to iterate between two geo locations with a certain speed in Python(3)

我想在真实世界地图(球形)上模拟运动并在 (google|openStreet) 地图上表示实际位置。

我有一个初始 lat/long 对,例如(51.506314, -0.088455) 并想搬到例如(51.509359, -0.087221) 以一定的速度定期获取插值坐标。

用于说明的伪代码:

loc_init = (51.509359, -0.087221)
loc_target = (51.509359, -0.087221)

move_path = Something.path(loc_init, loc_target, speed=50)

for loc in move_path.get_current_loc():
    map.move_to(loc)
    device.notify_new_loc(loc)
    ...
    time.sleep(1)

检索当前插值位置可以通过不同的方式发生,例如用固定的刷新时间(1 秒)或可能 运行 在一个线程中计算并持续计算新位置。

不幸的是,我以前从未使用过地理数据,因此在 Internet 上找不到有用的东西。也许已经有一个模块或一个实现这样做了?

解决了我的问题:

找到了一个 C++ 库 geographiclib,它被移植到 Python,完全符合我的要求。 计算反测地线并获取特定距离位置的示例代码:

from geographiclib.geodesic import Geodesic
import math

# define the WGS84 ellipsoid
geod = Geodesic.WGS84

loc_init = (51.501218, -0.093773)
loc_target = (51.511020, -0.086563)

g = geod.Inverse(loc_init[0], loc_init[1], loc_target[0], loc_target[1])
l = geod.InverseLine(loc_init[0], loc_init[1], loc_target[0], loc_target[1])

print ("The distance is {:.3f} m.".format(g['s12']))

# interval in m for interpolated line between locations
interval = 500
step = int(math.ceil(l.s13 / interval))

for i in range(step + 1):
    if i == 0:
        print ("distance latitude longitude azimuth")
    s = min(interval * i, l.s13)
    loc = l.Position(s, Geodesic.STANDARD | Geodesic.LONG_UNROLL)
    print ("{:.0f} {:.5f} {:.5f} {:.5f}".format(
        loc['s12'], loc['lat2'], loc['lon2'], loc['azi2']))

给出:

The distance is 1199.958 m.
distance latitude longitude azimuth
0 51.50122 -0.09377 24.65388
500 51.50530 -0.09077 24.65623
1000 51.50939 -0.08776 24.65858
1200 51.51102 -0.08656 24.65953