File size: 1,187 Bytes
9af7384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
A module to have different distance metrics for spatial interpolation
"""
import numpy as np
from scipy.spatial.distance import cdist


def haversine(X1, X2):
    """
    Arguments
    ---------
    One test point
    Multiple Train Points

    Long Lat Order
    """

    # Non-vectorized version
    # X1 = X1.reshape(1, 2)
    # difference = (X1 - X2) * np.pi / 180
    # test_point_lat = X1[:, 1] * np.pi / 180
    # training_locations_lat = X2[:, 1] * np.pi / 180

    # a = np.sin(difference[:, 0] / 2)**2 * np.cos(test_point_lat) * np.cos(training_locations_lat) +\
    #     np.sin(difference[:, 1] / 2)**2
    # radius = 6371
    # c = 2 * np.arcsin(np.sqrt(a))
    # return radius * c

    # Vectorized code
    lon1, lat1, lon2, lat2 = map(
        np.radians,
        [X1[:, 0, None], X1[:, 1, None], X2[:, 0, None], X2[:, 1, None]],
    )

    dlon = lon2.T - lon1
    dlat = lat2.T - lat1

    a = (
        np.sin(dlat / 2.0) ** 2
        + np.cos(lat1) @ np.cos(lat2.T) * np.sin(dlon / 2.0) ** 2
    )

    c = 2 * np.arcsin(np.sqrt(a))
    km = 6371 * c
    return km


def euclidean(X1, X2):
    # return np.linalg.norm(X1 - X2, 2, axis=1)
    return cdist(X1, X2)