Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Visualize each year of your Strava activities individually
library(plotKML)
library(maps)
library(ggmap)
# Settings
activities_folder = "/Users/noqqe/Downloads/activities/"
output_folder = "/Users/noqqe/Downloads/"
center_map_on = "Nuremberg"
zoom_depth = 8 # 1 - 21
routes_color = "#FF000022"
years <- c(2011:2018)
line_thickness = 0.6
# Get list of files
setwd(activities_folder)
# Initialize empty progress
progress = c()
files = c()
googlemap = c()
# Loop over years
for (year in years) {
# Debug output
print(year)
progress <- c(progress, year)
# fetch all files
for (p in progress) {
files <- c(files,Sys.glob(paste(activities_folder, "SIMPLIFIED-", p, "*-Ride.gpx", sep="")))
}
# Initialize vectors
index <- c()
latitude <- c()
longitude <- c()
location <- c()
route <- c()
routes <- c()
# Parse all files
for (i in 1:length(files)) {
route <- readGPX(files[i])
location <- route$tracks[[1]][[1]]
# build up vectors of all coordinates
index <- c(index, rep(i, dim(location)[1]))
latitude <- c(latitude, location$lat)
longitude <- c(longitude, location$lon)
}
# Get a google map centered on target with appropriate zoom (1 - 21)
if (is.null(googlemap)) {
googlemap <- qmap(location = center_map_on, zoom = zoom_depth)
}
# Combine the three vectors into one data frame
routes <- data.frame(cbind(index, latitude, longitude))
# generate filename
pngpath = paste(output_folder, "/strava-progress-", year, ".png", sep = "")
# Define output file
print(paste("saving file to", pngpath))
png(filename = pngpath, width = 1000, height = 800)
# map routes data onto the google map
m <- googlemap + geom_path(aes(x = longitude, y = latitude, group = factor(index)),
colour = routes_color, data = routes, alpha=line_thickness) +
ggtitle(paste(year))
# theme for title
m <- m + theme(plot.title = element_text(color="red", size=52, face="bold.italic", hjust = 0.5))
# plot the picture and close
plot(m)
dev.off()
}
# Get Stop time
print(paste("Stop:", Sys.time()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment