-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphing_LGA_flight_history.R
More file actions
538 lines (398 loc) · 15.7 KB
/
Graphing_LGA_flight_history.R
File metadata and controls
538 lines (398 loc) · 15.7 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
###########################################################################################-
###########################################################################################-
##
## Graphing LGA flight history ----
##
###########################################################################################-
###########################################################################################-
#=========================================================================================#
# Setting up ----
#=========================================================================================#
run_in_parallel <- FALSE
date_to_map <- lubridate::as_date("2020-04-06")
start_time <- hms::as_hms("00:00:01")
end_time <- hms::as_hms("23:59:59")
#-----------------------------------------------------------------------------------------#
# Loading libraries
#-----------------------------------------------------------------------------------------#
library(magrittr)
library(moveVis)
library(move)
library(tidyverse)
library(lubridate)
library(glue)
library(hms)
library(here)
library(DBI)
library(RSQLite)
library(dbplyr)
library(viridis)
library(fs)
library(ggdark)
library(doSNOW)
library(parallel)
library(doParallel)
library(tictoc)
#-----------------------------------------------------------------------------------------#
# Parsing dates for filtering data "server" side
#-----------------------------------------------------------------------------------------#
month_to_map <- month(date_to_map)
day_to_map <- day(date_to_map)
start_hour <- hour(start_time)
start_minute <- minute(start_time)
end_hour <- hour(end_time)
end_minute <- minute(end_time)
#-----------------------------------------------------------------------------------------#
# Creating frames folder
#-----------------------------------------------------------------------------------------#
frames_folder <- here(glue("plots/graph_frames/{date_to_map}/30_sec"))
dir_create(frames_folder)
#-----------------------------------------------------------------------------------------#
# Setting map view parameters
#-----------------------------------------------------------------------------------------#
# Bounding box around LGA (big enough to show aproaches from all directions)
lga_bbox <-
tibble(
longitude = c(-74.232575, -73.516318),
latitude = c(40.503766, 41.046881)
)
# Where the runways cross
lga_center <-
c(
longitude = -73.874861,
latitude = 40.780347
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# CRS
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
my_crs <- "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
#=========================================================================================#
# Loading and cleaning data ----
#=========================================================================================#
# Downloaded in "LGA flight history.R"
#-----------------------------------------------------------------------------------------#
# Pulling data
#-----------------------------------------------------------------------------------------#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Connecting to database
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
lga_tracks_db <- dbConnect(SQLite(), "data/lga_tracks_db.sqlite3")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Pulling
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
arrivals_tracks <-
lga_tracks_db %>%
tbl("arrivals_tracks") %>%
filter(
month == month_to_map,
day == day_to_map,
hour %>% between(start_hour, end_hour),
minute %>% between(start_minute, end_minute)
) %>%
collect() %>%
mutate(time = as_datetime(time, tz = "US/Eastern")) %>%
# Records with missing values are probably not "real" data points, so dropping them
drop_na(time, longitude, latitude, unique_flight) %>%
# Some of the records are doubled, which makes moveVis mad, so keeping only one
distinct(time, unique_flight, .keep_all = TRUE) %>%
# Restricting path data to coordinates inside the bounding box, to reduce unnecessary processing and
# memory overhead, and also so that the eventual summary statistics correspond to paths that
# are visible on the map
filter(
longitude %>% between(lga_bbox$longitude[1], lga_bbox$longitude[2]),
latitude %>% between(lga_bbox$latitude[1], lga_bbox$latitude[2])
)
dbDisconnect(lga_tracks_db)
# I want a frame for each half-minute of the day, but it's likely that not every half-minute interval will be
# present in the data, which means it will not be in the "move" object, which will mess up the frames.
# To avoid this, I'll add a whole day of fake data, corresponding to a persistent point that is placed
# behind the time label, by using the same formula I use to position the label.
date_min <-
as_datetime(str_c(date_to_map, start_time, sep = " "), tz = "US/Eastern") %>%
floor_date(unit = "30 seconds")
date_max <-
as_datetime(str_c(date_to_map, end_time, sep = " "), tz = "US/Eastern") %>%
ceiling_date(unit = "30 seconds")
arrivals_tracks <-
bind_rows(
tibble(
time = seq(date_min, date_max, "30 secs"),
longitude = mean(lga_bbox$longitude),
latitude = max(lga_bbox$latitude) - ((max(lga_bbox$latitude) - min(lga_bbox$latitude)) * 0.05),
unique_flight = "fake_flight"
),
arrivals_tracks
)
#-----------------------------------------------------------------------------------------#
# Converting for moveVis
#-----------------------------------------------------------------------------------------#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Turning the data.frame into a MoveStack
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
arrivals_move <-
df2move(
arrivals_tracks,
proj = my_crs,
x = "longitude",
y = "latitude",
time = "time",
track_id = "unique_flight"
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Aligning
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# To get accurate positions at each half-minute, this aligns the timestamps by interpolating the
# position of each flight at the given half-minute interval
arrivals_move_aligned <-
align_move(
arrivals_move,
res = 30,
digit = 0,
unit = "secs"
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Cleaning up
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Removing intermediate objects, because rapidly saving many ggplots (often in parallel) can
# be memory intensive
# rm(arrivals_move)
# rm(arrivals_tracks)
gc()
#-----------------------------------------------------------------------------------------#
# data.frame of distinct times and time label coords
#-----------------------------------------------------------------------------------------#
# This will be fed to "geom_label"
distinct_times <-
tibble(
time = seq(date_min, date_max, "30 secs")
) %>%
mutate(
time_chr = format(time, format = "%a %m/%e - %R"),
longitude = mean(lga_bbox$longitude),
latitude = max(lga_bbox$latitude) - ((max(lga_bbox$latitude) - min(lga_bbox$latitude)) * 0.05)
)
#-----------------------------------------------------------------------------------------#
# Extracting data from the aligned MoveStack
#-----------------------------------------------------------------------------------------#
arrivals_move_aligned_trimmed <-
arrivals_move_aligned@data %>%
as_tibble(rownames = "row_names") %>%
# Removing "fake_flight" from the count
filter(!row_names %>% str_detect("fake")) %>%
# Re-creating unique_flight
mutate(unique_flight = row_names %>% str_remove_all("\\..+")) %>%
# There shouldn't be any flight coords outside the bbox, but just to make sure...
filter(
x %>% between(lga_bbox$longitude[1], lga_bbox$longitude[2]),
y %>% between(lga_bbox$latitude[1], lga_bbox$latitude[2])
)
#-----------------------------------------------------------------------------------------#
# Summarizing
#-----------------------------------------------------------------------------------------#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# For each minute
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
arrivals_move_aligned_trimmed_summarized <-
arrivals_move_aligned_trimmed %>%
# The y-axis makes sense as "Flights / Minute", so keeping "60 secs" here. Joining with
# "min_max_seq_df" by "hour" and "minute" will replicate this count for each 30 sec frame
mutate(floor_time = floor_date(time, unit = "60 secs")) %>%
# Records will be doubled, as there are two 30-second segments within each 60 second segment,
# so here I'm removing the duplicates
distinct(unique_flight, floor_time, .keep_all = TRUE) %>%
count(floor_time, name = "flights_count") %>%
mutate(
day = day(floor_time),
hour = hour(floor_time),
minute = minute(floor_time)
) %>%
# Times are rounded to different resolutions, which means "time" will not match, so I'm removing it
select(-floor_time)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Complete summarized data
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# 30 second grid with fake coords
min_max_seq_df <-
tibble(
time = seq(date_min, date_max, "30 secs"),
longitude = 0,
latitude = 0,
unique_flight = "none"
) %>%
mutate(
day = day(time),
hour = hour(time),
minute = minute(time)
)
# Expanding the summarized data to have a record for each 30 second interval
arrivals_summarized <-
full_join(
min_max_seq_df,
arrivals_move_aligned_trimmed_summarized,
by = c("day", "hour", "minute")
) %>%
mutate(
flights_count = replace_na(flights_count, 0),
moving_flights_count = movingFun(flights_count, 120, "mean", na.rm = TRUE)
)
#=========================================================================================#
# Graph ----
#=========================================================================================#
#-----------------------------------------------------------------------------------------#
# Drawing graphs
#-----------------------------------------------------------------------------------------#
# To construct a manual legend, specifying `group = 1` and `colour = [text]` for the label
graph_frames <-
distinct_times$time %>%
map(
~ arrivals_summarized %>%
ggplot() +
# 0-point
geom_hline(yintercept = 0, color = "gray20", linetype = 2) +
# Daily average
geom_hline(
aes(
yintercept = mean(flights_count),
group = 1,
colour = "Daily average"
),
size = .75
) +
# Count in each minute
geom_line(
aes(
x = time,
y = flights_count,
group = 1,
colour = "Each minute"
),
size = .5
) +
# A loess smooth
geom_smooth(
aes(
x = time,
y = moving_flights_count,
group = 1,
colour = "loess (span = 0.5)"
),
method = "loess",
formula = y ~ x,
span = .5,
se = FALSE,
size = 1
) +
# A moving average over a window of 1 hour
geom_line(
aes(
x = time,
y = moving_flights_count,
group = 1,
colour = "Moving average (window = 1 hour)"
),
size = 1
) +
# Vertical line scanning with time
geom_vline(aes(xintercept = .x), color = "white") +
# Constructing manual legend and setting colors of lines (on the graph and in the legend)
scale_colour_manual(
name = NULL,
breaks = c("Each minute", "Moving average (window = 1 hour)", "loess (span = 0.5)", "Daily average"),
values = c("gray25", "#FDE725", "#424186", "#2AB07F"),
guide = guide_legend(direction = "vertical", override.aes = aes(size = 1.5))
) +
# Plot display specs
scale_y_continuous(name = "Flights / Minute", breaks = seq(0, 10, 2)) +
scale_x_datetime(
name = "Time",
date_labels = "%k:%M",
date_breaks = "2 hours"
) +
coord_cartesian(
xlim = c(date_min, date_max),
ylim = c(0, 11)
) +
dark_theme_gray() +
# Tweaking the plot display
theme(
legend.justification = c(0, 1), # Upper left
legend.position = c(0, 1), # Upper left
legend.background = element_rect(fill = NA, color = NA), # No background
legend.title = element_blank(), # Mo title
legend.margin = margin(t = 0, r = 10, b = 5, l = 10),
legend.key = element_rect(fill = NA), # No fill
text = element_text(size = 12) # Make it big
)
) %>%
# To give clusterApplyLB a way of accurately naming frames in order, if running in parallel
imap( ~ `attr<-`(.x, which = "frame", .y))
gc()
#-----------------------------------------------------------------------------------------#
# Saving graph frames ----
#-----------------------------------------------------------------------------------------#
if (run_in_parallel == TRUE) {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Setting up parallel saivng of graphs
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
cl <- makeSOCKcluster(3)
clusterExport(cl, c("frames_folder", "ggsave", "here", "glue"))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Running in parallel
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
format(now(), "%r"); tic()
clusterApplyLB(
cl = cl,
x = graph_frames,
fun = function(graph_frame) {
if (attr(graph_frame, which = 'frame') == 1) cat(length(graph_frames), " total\n", sep = "")
suppressMessages(
ggsave(
filename = glue("{frames_folder}/graph_frame_{attr(graph_frame, which = 'frame')}.png"),
plot = graph_frame,
width = 8,
height = 3.5,
units = "in",
dpi = 200
)
)
if (attr(graph_frame, which = 'frame') %% 10 == 0) cat(attr(graph_frame, which = 'frame'), ",", sep = "")
}
)
format(now(), "%r"); toc()
stopCluster(cl)
} else if (run_in_parallel == FALSE) {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Running in series
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
format(now(), "%r"); tic()
graph_frames %>%
iwalk(
~ {
# Printing progress
if (.y == 1) cat(length(graph_frames), " total\n", sep = "")
if (.y %% 10 == 0) cat(.y, ",", sep = "")
suppressMessages(
ggsave(
filename = glue("{frames_folder}/graph_frame_{.y}.png"),
plot = .x,
width = 8,
height = 3.5,
units = "in",
dpi = 200
)
)
# Collecting garbage every 100 frames
if (.y %% 100 == 0) gc()
}
)
format(now(), "%r"); toc()
}
rm(graph_frames)
gc()
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# # ---- THIS IS THE END! ----
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #