计算大量纬度/经度距离

问题描述 投票:0回答:1

我已尝试运行推荐的更新,但似乎仍然出现错误:

df.apply(lambda row:hs.haversine_vector(df["Start"], df["Stop"], unit=Unit.MILES))

下面的错误消息。这是因为数据集的坐标无效吗?

File ~\AppData\Local\anaconda3\Lib\site-packages\haversine\haversine.py:102, in _ensure_lat_lon_vector(lat, lon)
     98 def _ensure_lat_lon_vector(lat: "numpy.ndarray", lon: "numpy.ndarray"):
     99     """
    100     Ensure that the given latitude and longitude have proper values. An exception is raised if they are not.
    101     """
--> 102     if numpy.abs(lat).max() > 90:
    103         raise ValueError("Latitude(s) out of range [-90, 90]")
    104     if numpy.abs(lon).max() > 180:

TypeError: bad operand type for abs(): 'str'
python distance latitude-longitude haversine
1个回答
0
投票

我假设 Start 和 Stop 是字符串,每个数字由逗号分隔两个数字。因此,这些需要转换为两个浮点数的元组,然后传递给Haversine 函数。这可以通过将该函数应用于 DF 的每一行并将结果保存在新列(此处名为

dist
)中来完成。

您可以使用以下代码(以简化的数据集显示):

import haversine as hs
import pandas as pd

distance = pd.DataFrame({'Trip #': [1, 2],
                         'Start': ['43.241831,-79.75182', '35.6631,-77.369092'],
                         'Stop': ['43.23553,-79.837909', '35.799686,-77.97908']
                        })

def func(row):
    x = row['Start'].split(',')
    loc1 = (float(x[0]), float(x[1]))
    y = row['Stop'].split(',')
    loc2 = (float(y[0]), float(y[1]))
    d = hs.haversine(loc1, loc2, unit='mi')
    return d

distance['dist'] = distance.apply(func, axis = 1)

print(distance)

给予:

   Trip #                Start                 Stop       dist
0       1  43.241831,-79.75182  43.23553,-79.837909   4.355104
1       2   35.6631,-77.369092  35.799686,-77.97908  35.490359
© www.soinside.com 2019 - 2024. All rights reserved.