-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
366 lines (282 loc) · 9.51 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from PIL import Image
from PIL import ImageDraw
from flask import abort
from flask import Flask
from flask import request
from flask import send_file
from flask import render_template
from flaskwebgui import FlaskUI
Image.MAX_IMAGE_PIXELS = None
map_small = Image.open("map-small.jpg")
map_medium = Image.open("map-medium.jpg")
def get_hurdat_shape(initials):
initials = initials.upper()
if initials in ["TD", "TS", "HU", "TY"]: return "circle"
elif initials in ["SD", "SS"]: return "square"
elif initials in["EX", "LO", "DB", "WV"]: return "triangle"
def get_atcf_shape(initials):
initials = initials.upper()
if initials in ["TY", "TD", "TS", "ST", "TC", "HU", "XX"]: return "circle"
elif initials in ["SD", "SS"]: return "square"
elif initials in ["EX", "MD", "IN", "DS", "LO", "WV", "ET", "DB"]: return "triangle"
def get_ibtracs_shape(initials):
initials = initials.upper()
if initials in ["TS", "NR", "MX"]: return "circle"
elif initials == "SS": return "square"
elif initials in ["ET", "DS"]: return "triangle"
def get_rsmc_shape(num):
if num in ["2", "3", "4", "5", "7", "9"]: return "circle"
elif num == "6": return "triangle"
def stage_to_shape(stage):
if stage == "": return ""
return {
"extratropical cyclone": "triangle",
"subtropical cyclone": "square",
"tropical cyclone": "circle"
}[stage.lower()]
def ibtracs_sshs_to_cat(num):
if num in["-4", "-3", "-1"]: return -2
elif num in ["-2", "0"]: return -1
elif int(num) > 0 and int(num) < 6: return int(num)
else: return -999
def speed_to_cat(speed):
if speed == 0: return -999
elif speed <= 34: return -2
elif speed <= 64: return -1
elif speed <= 83: return 1
elif speed <= 96: return 2
elif speed <= 113: return 3
elif speed <= 137: return 4
else: return 5
def cat_to_colour(cat, accessible):
if accessible:
if cat == -999: return (192, 192, 192)
elif cat == -2: return (94, 186, 255)
elif cat == -1: return (0, 250, 244)
elif cat == 1: return (255, 247, 149)
elif cat == 2: return (255, 216, 33)
elif cat == 3: return (255, 143, 32)
elif cat == 4: return (255, 96, 96)
else: return (196, 100, 217)
else:
if cat == -999: return (192, 192, 192)
elif cat == -2: return (94, 186, 255)
elif cat == -1: return (0, 250, 244)
elif cat == 1: return (255, 255, 204)
elif cat == 2: return (255, 231, 117)
elif cat == 3: return (255, 193, 64)
elif cat == 4: return (255, 143, 32)
else: return (255, 96, 96)
def make_map(tracks, size, accessible):
if size == "medium": map = map_medium
else: map = map_small
FULL_WIDTH, FULL_HEIGHT = map.size
DOT_SIZE = 0.87890625 / 360 * FULL_WIDTH
LINE_SIZE = 0.25 / 360 * FULL_WIDTH
for i in tracks:
i["longitude"] = FULL_WIDTH/2 - float(i["longitude"][:-1]) % 180 * (-1 if i["longitude"][-1] == "E" else 1) / 360 * FULL_WIDTH
i["latitude"] = FULL_HEIGHT/2 - float(i["latitude"][:-1]) % 90 * (-1 if i["latitude"][-1] == "S" else 1) / 180 * FULL_HEIGHT
# cropping and resizing ==============================================
ZOOM = 3
min_longitude = min(i["longitude"] for i in tracks)
max_longitude = max(i["longitude"] for i in tracks)
min_latitude = min(i["latitude"] for i in tracks)
max_latitude = max(i["latitude"] for i in tracks)
top = min_latitude - FULL_HEIGHT * 5/180
left = min_longitude - FULL_WIDTH * 5/360
bottom = max_latitude + FULL_HEIGHT * 5/180
right = max_longitude + FULL_WIDTH * 5/360
if right - left < FULL_HEIGHT*45/180:
padding = (FULL_HEIGHT*45/180 - (right-left)) / 2
right += padding
left -= padding
if right - left < bottom - top:
padding = ((bottom-top) - (right-left)) / 2
right += padding
left -= padding
if bottom - top < (right-left) / 1.618033988749894:
padding = ((right-left) / 1.618033988749894 - (bottom-top)) / 2
bottom += padding
top -= padding
if left < 0: left = 0
if top < 0: top = 0
if right > FULL_WIDTH: right = FULL_WIDTH
if bottom > FULL_HEIGHT: bottom = FULL_HEIGHT
new_map = map.crop((left, top, right, bottom))
new_map = new_map.resize((round(new_map.size[0] * ZOOM), round(new_map.size[1] * ZOOM)))
# drawing =============================================================
draw = ImageDraw.Draw(new_map)
zoom_width, zoom_height = new_map.size
sorted_tracks = {}
for marker in tracks:
marker["longitude"] = round((marker["longitude"] - left) * ZOOM)
marker["latitude"] = round((marker["latitude"] - top) * ZOOM)
if marker["name"] in sorted_tracks: sorted_tracks[marker["name"]].append(marker)
else: sorted_tracks[marker["name"]] = [marker]
for tracks in sorted_tracks.values():
draw.line(
[(marker["longitude"], marker["latitude"]) for marker in tracks],
fill="white",
width=round(LINE_SIZE)
)
current = ""
for marker in tracks:
if marker["shape"] != "" and current != marker["shape"]: current = marker["shape"]
shape = marker["shape"]
if shape == "triangle":
side = DOT_SIZE * 3 ** 0.5;
bis = side * 3 ** 0.5 / 2;
coordinates = (
marker["longitude"],
marker["latitude"] - bis * 2 / 3,
marker["longitude"] - side / 2,
marker["latitude"] + bis / 3,
marker["longitude"] + side / 2,
marker["latitude"] + bis / 3
)
draw.polygon(coordinates, fill=cat_to_colour(marker["category"], accessible))
elif shape == "square":
size = DOT_SIZE / 2 ** 0.5
coordinates = (
marker["longitude"] - size,
marker["latitude"] - size,
marker["longitude"] + size,
marker["latitude"] + size
)
draw.rectangle(coordinates, fill=cat_to_colour(marker["category"], accessible))
elif shape == "circle":
coordinates = (
marker["longitude"] - DOT_SIZE,
marker["latitude"] - DOT_SIZE,
marker["longitude"] + DOT_SIZE,
marker["latitude"] + DOT_SIZE
)
draw.ellipse(coordinates, fill=cat_to_colour(marker["category"], accessible))
return new_map.resize((round(zoom_width//1.25), round(zoom_height//1.25)), resample=Image.ANTIALIAS) # anti aliasing
app = Flask(__name__)
ui = FlaskUI(app, width=900, height=800)
@app.route("/")
def main():
return render_template("index.html")
@app.route("/api/trackgen", methods=["POST"])
def gen_tracker():
tracks = request.json
if tracks == None: abort(400)
size = request.args.get("size", "small")
accessible = request.args.get("accessible", "false").lower()
if accessible not in["true", "false"]:
accessible = False
else:
accessible = [True, False][["true", "false"].index(accessible)]
for i in tracks:
i["category"] = speed_to_cat(float(i["speed"]))
i["shape"] = stage_to_shape(i["stage"])
del i["speed"]
del i["stage"]
make_map(tracks, size, accessible).save("tempFile.png")
return send_file("tempFile.png")
@app.route("/api/hurdat", methods=["POST"])
def hurdat():
data = request.json.split("\n")
accessible = request.args.get("accessible", "false").lower()
if accessible not in["true", "false"]:
accessible = False
else:
accessible = [True, False][["true", "false"].index(accessible)]
parsed = []
unique_id = ""
for line in data:
cols = line.split(", ")
if len(cols) == 3:
unique_id = cols[0]
else:
parsed.append(
{
"name": unique_id,
"latitude": cols[4],
"longitude": cols[5],
"category": speed_to_cat(float(cols[6])),
"shape": get_hurdat_shape(cols[3])
}
)
make_map(parsed, "medium", accessible).save("tempFile.png")
return send_file("tempFile.png")
@app.route("/api/atcf", methods=["POST"])
def atcf():
data = request.json.split("\n")
accessible = request.args.get("accessible", "false").lower()
if accessible not in["true", "false"]:
accessible = False
else:
accessible = [True, False][["true", "false"].index(accessible)]
parsed = []
for line in data:
cols = line.split(", ")
parsed.append(
{
"name": cols[1],
"latitude": cols[6][:-2]+"."+cols[6][-2:],
"longitude": cols[7][:-2]+"."+cols[7][-2:],
"category": speed_to_cat(float(cols[8])),
"shape": get_atcf_shape(cols[10])
}
)
make_map(parsed, "medium", accessible).save("tempFile.png")
return send_file("tempFile.png")
@app.route("/api/ibtracs", methods=["POST"])
def ibtracs():
data = request.json.split("\n")[2:]
accessible = request.args.get("accessible", "false").lower()
if accessible not in["true", "false"]:
accessible = False
else:
accessible = [True, False][["true", "false"].index(accessible)]
parsed = []
for line in data:
if line != "":
cols = line.split(",")
parsed.append(
{
"name": cols[0],
"latitude": cols[8]+"N",
"longitude": cols[9]+"E",
"category": ibtracs_sshs_to_cat(cols[25]),
"shape": get_ibtracs_shape(cols[7])
}
)
make_map(parsed, "medium", accessible).save("tempFile.png")
return send_file("tempFile.png")
@app.route("/api/rsmc", methods=["POST"])
def rsmc():
data = request.json.split("\n")
accessible = request.args.get("accessible", "false").lower()
if accessible not in["true", "false"]:
accessible = False
else:
accessible = [True, False][["true", "false"].index(accessible)]
unique_id = []
parsed = []
for line in data:
cols = line.split(" ")
cleaned_cols = []
for col in cols:
if col != "": cleaned_cols.append(col)
cols = cleaned_cols
if len(cols) == 9:
unique_id = cols[1]
else:
parsed.append(
{
"name": unique_id,
"latitude": cols[3][:-1]+"."+cols[3][-1:]+"N",
"longitude": cols[4][:-1]+"."+cols[4][-1:]+"E",
"category": speed_to_cat(float(cols[6])),
"shape": get_rsmc_shape(cols[2])
}
)
make_map(parsed, "medium", accessible).save("tempFile.png")
return send_file("tempFile.png")
@app.route("/favicon.ico")
def favicon():
return send_file("static/media/favicon.ico")
ui.run()