-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
221 lines (201 loc) · 5.68 KB
/
script.R
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
# Install packages
install.packages("av")
install.packages("https://cran.r-project.org/src/contrib/Archive/elevatr/elevatr_0.2.0.tar.gz", repos = NULL, type = "source")
# install.packages("elevatr")
install.packages("plyr")
install.packages("raster")
install.packages("rayshader")
install.packages("sf")
install.packages("sp")
install.packages("XML")
# Import libraries
library("av")
library("elevatr")
library("plyr")
library("raster")
library("rayshader")
library("sf")
library("sp")
library("XML")
# ----------------
# Helper functions
# ----------------
# Convert character representations to timestamp
parse_timestamp <- function(string) {
timestamp <- sub("T", " ", string)
timestamp <- sub("Z", "", timestamp)
timestamp <- as.POSIXlt(timestamp)
return(timestamp)
}
# Extract lat, lon, ele extent from GPX track
create_extent <- function(track) {
extent <- list(
lat_min = min(track$lat),
lat_max = max(track$lat),
lon_min = min(track$lon),
lon_max = max(track$lon),
ele_min = min(track$ele),
ele_max = max(track$ele)
)
return(extent)
}
# Construct Simple Feature collection from GPX extent
create_features <- function(extent) {
features <- sf::st_as_sf(
x = data.frame(
lon = c(extent$lon_min, extent$lon_max),
lat = c(extent$lat_min, extent$lat_max)
),
coords = c("lon", "lat"),
crs = 4326
)
return(features)
}
# Load GPX file
load_gpx <- function(filename) {
gpx_raw <- XML::xmlTreeParse(filename, useInternalNodes = TRUE)
gpx_root <- XML::xmlRoot(gpx_raw)
metadata <- XML::xmlToList(gpx_root)$metadata
trackdata <- XML::xmlToList(gpx_root)$trk
tracksegments <- unlist(
trackdata[names(trackdata) == "trkseg"],
recursive = FALSE
)
track <- do.call(
plyr::rbind.fill,
lapply(
tracksegments,
function(x) as.data.frame(t(unlist(x)), stringsAsFactors = F)
)
)
names(track) <- c("ele", "time", "hr", "cad", "lat", "lon")
track$time <- parse_timestamp(track$time)
track$hr <- as.numeric(track$hr)
track$cad <- as.numeric(track$cad)
track$lon <- as.numeric(track$lon)
track$lat <- as.numeric(track$lat)
track$ele <- as.numeric(track$ele)
extent <- create_extent(track)
features <- create_features(extent)
gpx <- list(
name = trackdata$name,
time = parse_timestamp(metadata$time),
track = track,
extent = extent,
features = features
)
return(gpx)
}
# Construct elevation raster, matrics, and metrics from GPX
load_elevation <- function(gpx) {
raster <- elevatr::get_elev_raster(
gpx$features,
z = 14,
clip = "bbox"
)
matrix <- matrix(
raster::extract(raster, raster::extent(raster)),
nrow = ncol(raster), ncol = nrow(raster)
)
metrics <- list(
extent = raster::extent(raster),
dimemsion = dim(raster),
resolution = raster::res(raster)
)
elevation <- list(
raster = raster,
matrix = matrix,
metrics = metrics
)
return(elevation)
}
# Create vectors from elevation and GPX track
create_vectors <- function(gpx, elevation) {
vector <- list(
x_min = rep(elevation$metrics$extent@xmin, length(gpx$track$lon)),
y_min = rep(elevation$metrics$extent@ymin, length(gpx$track$lat))
)
return(vector)
}
# Create coordinates from elevation and GPX track
create_coordinates <- function(gpx, elevation, vectors) {
coordinates <- list(
x = (gpx$track$lon - vectors$x_min) / elevation$metrics$resolution[1],
y = (gpx$track$lat - vectors$y_min) / elevation$metrics$resolution[2],
z = raster::extract(elevation$raster, gpx$track[, c(6, 5)])
)
return(coordinates)
}
# Create shading layers from elevation matrix
create_layers <- function(elevation) {
layers <- list(
ray = rayshader::ray_shade(elevation$matrix),
ambient = rayshader::ambient_shade(elevation$matrix),
water = rayshader::detect_water(elevation$matrix)
)
return(layers)
}
# Render scene in XQuartz window
render_scene <- function(elevation, coordinates) {
# Clear RGL window
rgl::clear3d()
# Display shaded map with Rayshader
elevation$matrix %>%
rayshader::sphere_shade(texture = "bw") %>%
# Commented out for better performance during development
# rayshader::add_shadow(layers$ray, max_darken = 0.2) %>%
# rayshader::add_shadow(layers$ambient, max_darken = 0.2) %>%
# rayshader::add_water(layers$water, color = "bw") %>%
rayshader::plot_3d(
elevation$matrix,
theta = 45,
phi = 45,
solidcolor = "#eeeeee",
solidlinecolor = "#eeeeee",
soliddepth = gpx$extent$ele_min - 100,
shadowdepth = gpx$extent$ele_min - 100,
shadowcolor = "#bbbbbb",
windowsize = c(1200, 800),
)
# Display the GPX route in 3D
rgl::lines3d(
coordinates$x - elevation$metrics$dimemsion[2] / 2,
coordinates$z + 3,
-coordinates$y + elevation$metrics$dimemsion[1] / 2,
color = "#fc5200",
lwd = 2.0,
)
}
# Export static preview image
render_picture <- function(name) {
rayshader::render_snapshot(
filename = name,
title_text = name,
title_color = "#bbbbbb",
title_size = "16",
title_font = "mono",
title_position = "southeast",
)
}
# Export animated preview movie
render_animation <- function(name) {
rayshader::render_movie(
filename = name,
title_text = name,
title_color = "#bbbbbb",
title_size = "16",
title_font = "mono",
title_position = "southeast",
)
}
# -------------------
# Execution functions
# -------------------
gpx <- load_gpx("track.gpx")
elevation <- load_elevation(gpx)
vectors <- create_vectors(gpx, elevation)
coordinates <- create_coordinates(gpx, elevation, vectors)
layers <- create_layers(elevation)
render_scene(elevation, coordinates)
render_picture(toString(gpx$time))
# render_animation(toString(gpx$time))