I have a list of coordinates and can calculate a distance matrix among all points using the haversine distance metric.
Coordinates come a as numpy.array
Here's one way you can vectorize that calculation without creating a big matrix. coslat
is the array of cosines of the latitudes, and coslat[:-1]*coslat[1:]
is the vectorized version of the expression cos(ϕ1)cos(ϕ2) in the Haversine formula.
from __future__ import division, print_function
import numpy as np
def hav(theta):
return np.sin(theta/2)**2
coords = [[ 16.34576887, -107.90942116],
[ 12.49474931, -107.76030036],
[ 27.79461514, -107.98607881],
[ 12.90258404, -107.96786569],
[ -6.29109889, -107.88681145],
[ -2.68531605, -107.72796034]]
r = 6371
coordinates = np.deg2rad(coords)
lat = coordinates[:, 0]
lng = coordinates[:, 1]
coslat = np.cos(lat)
t = hav(np.diff(lat)) + coslat[:-1]*coslat[1:]*hav(np.diff(lng))
d = 2*r*np.arcsin(np.sqrt(t))
print(d)
Output:
[ 428.51472353 1701.42935412 1655.91938575 2134.25895299 401.33113696]