You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python计算两个经纬度点之间每间隔10公里的所有坐标点

沿两点大圆路径等距采样经纬度点Python实现

示例图

方案说明

直接使用专业大地测量库geographiclib实现,基于WGS84坐标系(GPS通用坐标系)计算,精度远高于手动实现的简化公式,无需自行处理方位角、椭球校正等复杂逻辑。

步骤实现

1. 安装依赖

pip install geographiclib

2. 完整代码

from geographiclib.geodesic import Geodesic

def get_equidistant_gps_points(start: tuple[float, float], end: tuple[float, float], interval_km: int = 10) -> list[tuple[float, float]]:
    """
    沿起点到终点的大圆路径,按指定间隔采样经纬度点
    :param start: 起点坐标(纬度, 经度)
    :param end: 终点坐标(纬度, 经度)
    :param interval_km: 采样间隔,单位公里
    :return: 采样点列表,包含起点和终点
    """
    # 加载WGS84椭球模型
    geod = Geodesic.WGS84
    # 反算两点总距离(米)、起点到终点的方位角
    inv_res = geod.Inverse(lat1=start[0], lon1=start[1], lat2=end[0], lon2=end[1])
    total_m = inv_res['s12']
    azimuth = inv_res['azi1']

    points = []
    current_m = 0
    interval_m = interval_km * 1000
    # 按间隔采样
    while current_m <= total_m:
        dir_res = geod.Direct(lat1=start[0], lon1=start[1], azi1=azimuth, s12=current_m)
        points.append((round(dir_res['lat2'], 6), round(dir_res['lon2'], 6)))
        current_m += interval_m
    # 兼容浮点误差,确保终点存在
    end_rounded = (round(end[0], 6), round(end[1], 6))
    if points[-1] != end_rounded:
        points.append(end_rounded)
    return points

# 测试:马德里到巴黎10公里间隔采样
if __name__ == '__main__':
    madrid = (40.437, -3.7325)
    paris = (48.853, 2.351074)
    res = get_equidistant_gps_points(madrid, paris, 10)
    print(f"采样总点数:{len(res)}")
    print(f"起点:{res[0]}")
    print(f"第二个点(间隔10公里):{res[1]}")
    print(f"终点:{res[-1]}")

运行结果参考

马德里到巴黎的大圆距离约为1052公里,10公里间隔采样总点数为106个,输出示例:

采样总点数:106
起点:(40.437, -3.7325)
第二个点(间隔10公里):(40.513785, -3.67987)
终点:(48.853, 2.351074)

内容的提问来源于stack exchange,提问作者ProcolHarum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 19:09:01