我已经给出了经度和纬度的坐标列表。
[[-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的新手,我遇到了困难
答案 0 :(得分:1)
如果你想要解压缩所有计算的总和并加总:
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))
答案 1 :(得分:0)
它并不完全清楚你在问什么,但我认为你想要总结一下所行的总距离。这很容易实现,而且你非常接近(假设您的距离函数正常工作)。你只需要对你的循环做一点改动:
totaldistance = 0
for i in range(len(coordinates) - 1):
totaldistance += distance(coordinates[i], coordinates[i + 1])
答案 2 :(得分:0)
现在你要添加从0到1,0到2等的距离。你想要获得总行进距离,如0到1,1对2,2到3,......?如果是这样,请尝试编辑totaldistance循环:
totaldistance = 0
for i in range(0, len(coordinates)-1):
totaldistance += distance(coordinates[i], coordinates[i+1])