-
Notifications
You must be signed in to change notification settings - Fork 7
/
weather_station_location.py
75 lines (62 loc) · 1.81 KB
/
weather_station_location.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Weather Station location
"""
from haversine import haversine
def calc_distance_two_points(lat_from, long_from, lat_to, long_to):
"""Calculate distance between two points
https://pypi.org/project/haversine/#description
Arguments
----------
long_from : float
Longitute coordinate from point
lat_from : float
Latitute coordinate from point
long_to : float
Longitute coordinate to point
lat_to : float
Latitue coordinate to point
Return
------
distance : float
Distance
"""
distance_in_km = haversine(
(lat_from, long_from),
(lat_to, long_to),
unit='km')
return distance_in_km
def get_closest_station(
latitude_reg,
longitude_reg,
weather_stations
):
"""Search ID of closest weater station
Arguments
----------
longitude_reg : float
Longitute coordinate of Region Object
latitude_reg : float
Latitute coordinate of Region Object
weather_stations : dict
Weater station data
Return
------
closest_id : int
ID of closest weather station
"""
closest_dist = 99999999999
for station_id in weather_stations:
try:
lat_to = weather_stations[station_id].latitude
long_to = weather_stations[station_id].longitude
except AttributeError:
lat_to = weather_stations[station_id]["latitude"]
long_to = weather_stations[station_id]["longitude"]
dist_to_station = calc_distance_two_points(
lat_from=latitude_reg,
long_from=longitude_reg,
lat_to=lat_to,
long_to=long_to)
if dist_to_station < closest_dist:
closest_dist = dist_to_station
closest_id = station_id
return closest_id