【发布时间】:2020-08-19 06:29:39
【问题描述】:
我正在尝试遍历多行纬度/经度坐标,并为每个坐标创建一个新的“距离”列。
这是它的样子: 我使用了这个公式:
def haversine(lat1, lon1, lat2, lon2, to_radians=True, earth_radius=6371):
"""
slightly modified version: of http://stackoverflow.com/a/29546836/2901002
Calculate the great circle distance between two points
on the earth (specified in decimal degrees or in radians)
All (lat, lon) coordinates must have numeric dtypes and be of equal length.
"""
if to_radians:
lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])
a = np.sin((lat2-lat1)/2.0)**2 + \
np.cos(lat1) * np.cos(lat2) * np.sin((lon2-lon1)/2.0)**2
return earth_radius * 2 * np.arcsin(np.sqrt(a))
df['dist'] = \
haversine(df.lat.shift(), df.lon.shift(),
df.loc[1:, 'lat'], df.loc[1:, 'lon'])
但我收到此错误:
AttributeError Traceback (most recent call last)
~\anaconda3\envs\geopandas\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5273 return self[name]
-> 5274 return object.__getattribute__(self, name)
5275
AttributeError: 'Series' object has no attribute 'radians'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-10-e19d56f45506> in <module>
20 df['dist'] = \
21 haversine(df.lat.shift(), df.lon.shift(),
---> 22 df.loc[1:, 'lat'], df.loc[1:, 'lon'])
<ipython-input-10-e19d56f45506> in haversine(lat1, lon1, lat2, lon2, to_radians, earth_radius)
10 """
11 if to_radians:
---> 12 lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])
13
14 a = np.sin((lat2-lat1)/2.0)**2 + \
TypeError: loop of ufunc does not support argument 0 of type Series which has no callable radians
method
抱歉,格式不正确 - 谁能帮我理解发生了什么?
谢谢!
【问题讨论】:
-
抱歉指定它不仅仅是两个静态点,我希望它循环遍历行,并将其与循环中的前一个点进行比较以计算 500 多行 lon/lat 的距离跨度>
标签: python loops coordinates distance haversine