【问题标题】:Determine distance from Latitude and Longitude [closed]确定与纬度和经度的距离[关闭]
【发布时间】:2021-01-01 11:31:54
【问题描述】:

我的数据集包含纬度和经度。现在我想确定这两个点之间的距离。你能帮我解决这个问题吗?在我的数据集中,我只有 2 个变量 经度 = -73.953918 纬度 = 40.778873

【问题讨论】:

标签: pandas dataframe


【解决方案1】:

如果您有 pandas 数据框,则将每一列转换为 np 数组,此函数可用于标量或 numpy 数组,只需与 lat 和 lng 保持一致即可:

def haversine_np(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    All args must be of equal length.
    """
    lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = np.sin(dlat / 2.0) ** 2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2.0) ** 2
    kms= (2 * 6367) * np.arcsin(np.sqrt(a))
    return kms

示例:

np.random.seed(42)
df = pd.DataFrame(dict(lat=np.random.uniform(32,33,(100,)),lng=np.random.uniform(33,34,(100,))))
point  = dict(lat=32.5,lng=33.5)
df['distance_km'] = haversine_np(df['lng'].values,df['lat'].values,point['lng'],point['lat'])
df
>>>
    lat         lng         distance_km
0   32.374540   33.031429   46.104413
1   32.950714   33.636410   51.683653
2   32.731994   33.314356   31.089653
3   32.598658   33.508571   10.992790
4   32.156019   33.907566   54.090586
... ... ... ...
95  32.493796   33.349210   14.149670
96  32.522733   33.725956   21.324490
97  32.427541   33.897110   38.093632
98  32.025419   33.887086   64.065001
99  32.107891   33.779876   50.888535

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2011-08-28
    • 2011-08-16
    • 2020-11-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多