1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
|
|
try: |
4
|
|
|
import logging |
5
|
|
|
import sys |
6
|
|
|
from scipy.spatial import distance |
7
|
|
|
except ImportError as err: |
8
|
|
|
logging.error('Error %s import module: %s', __name__, err) |
9
|
|
|
logging.exception('Exception occurred') |
10
|
|
|
|
11
|
|
|
sys.exit(128) |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def closest_point(point, points): |
|
|
|
|
15
|
|
|
# Find closest point from a list of points |
16
|
|
|
pt = points[distance.cdist([point], points).argmin()] |
|
|
|
|
17
|
|
|
return pt |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def closest_point_distance(point, points): |
|
|
|
|
21
|
|
|
# Find closest point from a list of points |
22
|
|
|
pt = points[distance.cdist([point], points).argmin()] |
|
|
|
|
23
|
|
|
pt_dist = '{:10.8f}'.format(distance.euclidean(point, pt)) |
24
|
|
|
return pt_dist |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def match_value(df, col1, x, col2): |
|
|
|
|
28
|
|
|
# Match value x from col1 row to value in col2 |
29
|
|
|
return df[df[col1] == x][col2].values[0] |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def finding_closest(data1, data2): |
|
|
|
|
33
|
|
|
# Add stop_id and stop_name to the closest point |
34
|
|
|
logging.info('Finding closest coordinates') |
35
|
|
|
data2['closest'] = [closest_point(x, list(data1['point'])) for x in data2['point']] |
36
|
|
|
logging.info('Calculating closest coordinates distances') |
37
|
|
|
data2['dist_closest'] = [closest_point_distance(x, list(data1['point'])) for x in data2['point']] |
|
|
|
|
38
|
|
|
logging.info('Selecting matching stop_id') |
39
|
|
|
data2['stop_id'] = [match_value(data1, 'point', x, 'stop_id') for x in data2['closest']] |
40
|
|
|
logging.info('Selecting matching name') |
41
|
|
|
data2['stop_name'] = [match_value(data1, 'point', x, 'stop_name') for x in data2['closest']] |
42
|
|
|
return data2 |
43
|
|
|
|