Build distance matrix in a vectorized way (without loop) from Latitude Longitude coordinates

对着背影说爱祢 提交于 2019-12-23 04:01:36

问题


I would like to come up with a faster way to create a distance matrix between all lat lon pairs. This QA addresses doing a vectorized way with standard Linear Algebra, but without Lat Lon coordinates.

In my case these lat longs are farms. Here is my Python code, which for the full data set (4000 (lat, lon)'s) takes at least five minutes. Any ideas?

> def slowdistancematrix(df, distance_calc=True, sparse=False, dlim=100):
    """
    inputs: df

    returns:
    1.) distance between all farms in miles
    2.) distance^2

    """

    from scipy.spatial import distance_matrix
    from geopy.distance import geodesic

    unique_farms = pd.unique(df.pixel)
    df_unique = df.set_index('pixel')
    df_unique = df_unique[~df_unique.index.duplicated(keep='first')] # only keep unique index values
    distance = np.zeros((unique_farms.size,unique_farms.size))

    for i in range(unique_farms.size):
        lat_lon_i = df_unique.Latitude.iloc[i],df_unique.Longitude.iloc[i]
        for j in range(i):
            lat_lon_j = df_unique.Latitude.iloc[j],df_unique.Longitude.iloc[j]
            if distance_calc == True:
                distance[i,j] = geodesic(lat_lon_i, lat_lon_j).miles
                distance[j,i] = distance[i,j] # make use of symmetry

    return distance, np.power(distance, 2)

回答1:


My solution is a vectorized version of this implementation:

import numpy as np

def dist(v):
    v = np.radians(v)

    dlat = v[:, 0, np.newaxis] - v[:, 0]
    dlon = v[:, 1, np.newaxis] - v[:, 1]

    a = np.sin(dlat / 2.0) ** 2 + np.cos(v[:, 0]) * np.cos(v[:, 0]) * np.sin(dlon / 2.0) ** 2

    c = 2 * np.arcsin(np.sqrt(a))
    result = 3956 * c

    return result

However you will need to convert your dataframe to a numpy array, using the attribute values. For example:

df = pd.read_csv('some_csv_file.csv')
distances = dist(df[['lat', 'lng']].values)


来源:https://stackoverflow.com/questions/58345103/build-distance-matrix-in-a-vectorized-way-without-loop-from-latitude-longitude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!