gpt4 book ai didi

python - 计算以 KM 表示的总距离(在 Python 中提供坐标列表)

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:58 28 4
gpt4 key购买 nike

我给出了带有经度和纬度的坐标列表。

[[-8.610876000000001, 41.14557], [-8.610858, 41.145579000000005], [-8.610903, 41.145768], [-8.610444, 41.146190999999995], [-8.609445000000001, 41.146758], [-8.608896, 41.147118], [-8.608968, 41.147127], [-8.608707, 41.147532000000005], [-8.608347, 41.148117000000006], [-8.608149, 41.148351000000005], [-8.608041, 41.148576000000006], [-8.607654, 41.14926], [-8.607348000000002, 41.149899000000005], [-8.607393, 41.149899000000005], [-8.607357, 41.149962], [-8.606817, 41.150979], [-8.606358, 41.151914999999995], [-8.605719, 41.152788], [-8.604981, 41.153318999999996], [-8.604783, 41.154345], [-8.604828, 41.154372], [-8.604801, 41.155353], [-8.604648000000001, 41.156774999999996], [-8.604522, 41.158197], [-8.604513, 41.159943000000005], [-8.604377999999999, 41.16055500000001], [-8.604377999999999, 41.1606], [-8.604369, 41.160644999999995], [-8.60436, 41.160807], [-8.604162, 41.161176], [-8.604126, 41.161248], [-8.60409, 41.16129300000001], [-8.60409, 41.161266000000005], [-8.604108, 41.161239], [-8.604126, 41.161194], [-8.604135, 41.161275], [-8.60391, 41.162048999999996], [-8.602929000000001, 41.162832], [-8.602551000000002, 41.163111], [-8.601894, 41.163597]]

我需要计算以公里为单位的总行驶距离。我想到使用 stackoverlflow 中给出的流行技术

dlon = lon2 - lon1
dlat = lat2 - lat1

a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))

distance = R * c

我试过了-

totaldistance = 0
for i in range(1, len(coordinates)):
totaldistance += distance(coordinates[0], coordinates[i])

但是我应该如何处理给定的坐标列表,该列表指定了所有对象漫游的确切位置。就像总旅行一样。由于我是 Python 新手,所以我陷入了困境

最佳答案

如果您想要解包所有计算的总和并求和:

tot = sum(distance(lat, lng)) for lat,lng in coords)

如果您希望传递每对子列表,即 l[0],l[1], l[2],l[3] 等..:

it = iter(coords)
tot = sum(distance(lat, lng)) for lat,lng in zip(it,it))

如果您想对每对l[0],l[1]l[1],l[2]等求和:

tot = sum(distance(l[i],l[i+1]) for i in range(len(l)-1))

关于python - 计算以 KM 表示的总距离(在 Python 中提供坐标列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31618403/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com