如何以给定速度在2个gps坐标之间迭代gps点

2024-09-27 22:19:45 发布

您现在位置:Python中文网/ 问答频道 /正文

给定两个启动和停止gps坐标(即点#1:39.5210981,-76.6194347,点#2:39.620669,-76.554627),我使用OpenStreetMap获得了这两个启动和停止gps坐标之间最短路线的所有gps坐标点

问题是,我如何以给定路线每1秒的速度(即30英里/小时或45公里/小时)迭代一组新的gps坐标

我尝试使用另一个stackoverflow用户给出的代码计算两点之间的距离:

def midpoint(lat1, long1, lat2, long2, per):
     return lat1 + (lat2 - lat1) * per, long1 + (long2 - long1) * per

但是对于某些点来说,它看起来很不恰当,而且,如果某些点非常接近,那么我也不恰当


Tags: 代码用户距离stackoverflow路线速度gpsopenstreetmap
1条回答
网友
1楼 · 发布于 2024-09-27 22:19:45

我建议使用一个外部库来实现这一点,如果你想在长距离内准确地完成这一点,那么要正确地计算是相当困难的。我以前使用过pyproj,它公开了一个^{}方法,该方法说:

Returns forward and back azimuths, plus distances between initial points (specified by lons1, lats1) and terminus points (specified by lons2, lats2).

“方位角”是你头部到达该点的角度,距离以米为单位

它还公开了^{}用于返回坐标:

Returns longitudes, latitudes and back azimuths of terminus points given longitudes (lons) and latitudes (lats) of initial points, plus forward azimuths (az) and distances (dist).

我想你会想把这些放在一起是这样的:

import pyproj

# WGS-84 is the coordinate system used by GPS
wgs84_geod = pyproj.CRS('WGS 84').get_geod()

# I think I'm pulling these apart in the right order, but you'd want to check
src_lat, src_lon = 39.5210981,-76.6194347
dst_lat, dst_lon = 39.6206699,-76.554627

# how many meters between each point
delta = 45 * 1000 / 3600

# keep track of where we currently are
lat, lon = src_lat, src_lon
result = []

while True:
    # figure out which direction to go and how far away we are
    az, _, dist = wgs84_geod.inv(lon, lat, dst_lon, dst_lat)

    # are we done yet?
    if dist < delta:
        break

    result.append((lon, lat))

    # move at our speed towards our target
    lon, lat, _ = wgs84_geod.fwd(lon, lat, az, delta)

这在短距离内给出了一条基本上是直线的曲线,但在较长距离内曲线会很明显

相关问题 更多 >

    热门问题