【发布时间】:2021-07-04 17:28:10
【问题描述】:
我想计算两个坐标之间的距离。我知道我可以计算两点之间的半正弦距离。但是,我想知道是否有更简单的方法来代替使用迭代整个列的公式创建循环(也会在循环中出错)。
以下是示例的一些数据
# Random values for the duration from one point to another
random_values = random.sample(range(2,20), 8)
random_values
# Creating arrays for the coordinates
lat_coor = [11.923855, 11.923862, 11.923851, 11.923847, 11.923865, 11.923841, 11.923860, 11.923846]
lon_coor = [57.723843, 57.723831, 57.723839, 57.723831, 57.723827, 57.723831, 57.723835, 57.723827]
df = pd.DataFrame(
{'duration': random_values,
'latitude': lat_coor,
'longitude': lon_coor
})
df
duration latitude longitude
0 5 11.923855 57.723843
1 2 11.923862 57.723831
2 10 11.923851 57.723839
3 19 11.923847 57.723831
4 16 11.923865 57.723827
5 4 11.923841 57.723831
6 13 11.923860 57.723835
7 3 11.923846 57.723827
为了计算距离,这是我尝试过的:
# Looping over each row to compute the Haversine distance between two points
# Earth's radius (in m)
R = 6373.0 * 1000
lat = df["latitude"]
lon = df["longitude"]
for i in lat:
lat1 = lat[i]
lat2 = lat[i+1]
for j in lon:
lon1 = lon[i]
lon2 = lon[i+1]
dlon = lon2 - lon1
dlat = lat2 - lat1
# Haversine formula
a = math.sin(dlat / 2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2)**2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
distance = R * c
print(distance) # in m
计算距离的两个点应该取自同一列。
第一个距离值:
11.923855 57.723843 (point1/observation1)
11.923862 57.723831 (point2/observation2)
秒距离值:
11.923862 57.723831 (point1/observation2)
11.923851 57.723839(point2/observation3)
第三个距离值:
11.923851 57.723839(point1/observation3)
11.923847 57.723831 (point1/observation4)
...(等等)
【问题讨论】:
-
您需要两个点 (lat1, lon1) 和 (lat2, lon2) 来计算距离。数据框中的每一行只有一个点。你能指定应该用哪个其他点计算距离吗?
-
我已编辑问题以使这部分更清晰。它们应该来自同一列。
标签: python loops math haversine