-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradar_parser.py
84 lines (59 loc) · 2.17 KB
/
radar_parser.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
76
77
78
79
80
81
82
83
import requests
from io import BytesIO
from PIL import Image
from collections import defaultdict
from constants import *
from util import *
from imagetools import *
from filtertools import *
import numpy as np
import scipy.ndimage as ndimage
import re
import json
def reverse_geocode(coordinates):
"""
Reverse-geocodes the input coordinates and returns the output as a python dictionary.
Uses LocationIQ's reverse-geocoding service. Their API was chosen due to an upper bound
of 10,000 free API calls a day.
Parameters:
coordinates (tuple) : A pair of the form (latitude, longitude)
Returns:
dict: A dictionary obtained from the JSON response.
"""
reverse_url = "https://us1.locationiq.com/v1/reverse.php"
data = {
'key': LOCATIONIQ_API_KEY,
'lat': str(coordinates[0]),
'lon': str(coordinates[1]),
'format': 'json'
}
response = requests.get(reverse_url, params=data)
return json.loads(response.text)
def calculate_rainfall_locations(image_url):
"""
Returns a list of locations under rainfall. Locations are described in the JSON format.
The image at the given URL is retrieved via a GET request. The image is then filtered and reverse-geocoded to obtain the locations under rainfall.
Parameters:
image_url (string) : The url of the radar image.
Returns:
List of locations experiencing heavy rainfall.
"""
image = get_radar_image_from_url(image_url)
coordinates_to_filter = load_filter(fp)
filtered_image = filter_reflectivity(image, 45, coordinates_to_filter)
coordinates = [convert_pixels_to_coordinates(x[0], x[1]) for x in filtered_image]
json_results = [reverse_geocode(x) for x in coordinates]
return json_results
def main():
json_results = calculate_rainfall_locations(sample_url)
final_results = []
for result in json_results:
try:
final_results.append(result['display_name'])
except KeyError:
pass
places = set(final_results)
for place in places:
print(place)
if __name__ == "__main__":
main()