-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path03_mapbox_helis.Rmd
144 lines (115 loc) · 3.8 KB
/
03_mapbox_helis.Rmd
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
---
title: "Animated helicopter paths using Mapdeck"
author: "Andrew Ba Tran"
date: "6/23/2020"
output: html_document
---
```{r setup, warning=F, message=F, fig.width=9, fig.height=4}
library(tidyverse)
library(sf)
library(mapdeck)
library(lubridate)
library(data.table)
mb_key <-'your_key_goes_here'
# my key is in this file that has not been uploaded to Github
source("scripts/mapbox_key.R")
# reading in the geojson file
heli_big2 <- read_sf("data/heli_points.geojson")
glimpse(heli_big2)
# Can choose which helicopters to focus on and what time
# This code below will filter only the two National Guard Lakotas
# And show where they traveled between 9 and 10 p.m. on June 1
# AE0BED is the Black Hawk, if you'd like to include that
heli_one9 <- heli_big2 %>%
#filter(code.x=="AE1F45" | code.x=="AE1FE3" | code.x=="AE0BED") %>%
filter(code.x=="AE1F45" | code.x=="AE1FE3") %>%
mutate(day=day(timestamp)) %>%
mutate(hour=hour(timestamp)) %>%
mutate(minute=minute(timestamp)) %>%
filter(day==1) %>%
filter(hour==21) %>%
filter(minute>00 & minute <=59)
heli_one10 <- heli_big2 %>%
#filter(code.x=="AE1F45" | code.x=="AE1FE3" | code.x=="AE0BED") %>%
filter(code.x=="AE1F45" | code.x=="AE1FE3") %>%
mutate(day=day(timestamp)) %>%
mutate(hour=hour(timestamp)) %>%
mutate(minute=minute(timestamp)) %>%
filter(day==1) %>%
filter(hour==22) %>%
filter(minute>00 & minute <=59)
heli_one <- rbind(heli_one9, heli_one10) %>%
arrange(code.x,timestamp)
## Quick chart for altitude
heli_one <- heli_one %>%
mutate(heli=case_when(
code.x=="AE1FE3" ~ "NG Lakota",
code.x=="AE1F45" ~ "NG Lakota Red Cross",
code.x=="AE0BED" ~ "Black Hawk"
))
ggplot(heli_one, aes(x=timestamp, y=altitude_adjusted, colour=heli)) +
geom_line() +
theme_minimal() +
labs(title="How low the two Lakotas traveled in DC on June 1")
## Mapping the path with Mapbox/Mapdeck
## We need to take the flight data data frame and
## transform the coordinate sinto a sfc_LINESTRING geometry
## so it will be compatible with Mapbox/Mapdeck code
heli_path <- heli_one
## grab the helicopter path coordinates only
dt <- sf::st_coordinates( heli_path )
## transform the matrix into a data table
dt <- as.data.table( dt )
# Extracting the altitude into an array
hel_alt <- heli_one %>%
mutate(day=day(timestamp)) %>%
pull(altitude)
# Extracting the timestamps into an array
hel_time <- heli_one %>%
mutate(day=day(timestamp)) %>%
pull(timestamp)
# Extracting a numeric value for the helicopter ID (1 and 2)
hel_id <- heli_one %>%
mutate(day=day(timestamp)) %>%
mutate(id=case_when(
code.x=="AE1F45" ~ 1,
code.x=="AE1FE3" ~ 2,
# code.x=="AE0BED" ~ 3, # in case there was interest in mapping the Black Hawk
TRUE ~ 0
)) %>%
pull(id)
## This is a lot of work just to structure our
## lat, lon, altitude, timestamp, and helicopter id
## juuust right
## Need Z and M attributes
dt[, Z := hel_alt ] ## setting Z as our altitude
dt[, M := hel_time] ## setting M as our timestamp
dt[, L1 := hel_id] ## setting L1 as our helicopter id number
## now we can convert back to sf LINESTRING
heli_trips <- sfheaders::sf_linestring(
obj = dt
, x = "X"
, y = "Y"
, z = "Z"
, m = "M"
, linestring_id = "L1"
)
```
## Mapbox visualization
```{r mapdeck, warning=F, message=F, fig.width=9, fig.height=6}
mapdeck(
token=mb_key # if you don't have an mb_key you won't see any street tiles
, location = c(-77.024058, 38.904742)
, zoom = 13
, pitch = 200
, style = mapdeck_style("dark")
) %>%
add_trips(
data = heli_trips
, stroke_colour = "L1"
, stroke_width = 30
, animation_speed = 50
, trail_length = 1000
)
```
**Click and drag the map around or zoom in. You can also hold control or command to tilt the camera and get a better glimpse at the elevation of the paths.**