Skip to content

Instantly share code, notes, and snippets.

@Oreotrephes
Created March 17, 2014 22:59
Show Gist options
  • Select an option

  • Save Oreotrephes/9610131 to your computer and use it in GitHub Desktop.

Select an option

Save Oreotrephes/9610131 to your computer and use it in GitHub Desktop.
#ALL THIS IS IN TERMINAL
#First, install gpsbabel through some means. I have homebrew, so I did
$ brew install gpsbabel
#Navigated to wherever the gpx files are
$ cd ~/Downloads/activities
#and converted them
$ for i in *.gpx; do gpsbabel -t -i gpx -f $i -o unicsv -F $i.csv; done
#runs enormously fast!
#okay, so how long are all those put together?
$ cat *.gpx.csv | wc -l
#yours is about 750,000 lines, we'll just manually pre-allocate that many below
########################
#################
#ALL THIS IS IN R
#load files
setwd("~/Downloads/activities")
#this is now 4X faster for me
system.time(
routes <- (function(){
file_names <- dir(pattern = "Ride\\.gpx\\.csv$")
routes <- data.frame(
Track=numeric(750000),
Latitude=numeric(750000),
Longitude=numeric(750000),
Altitude=numeric(750000),
Date=numeric(750000),
Time=numeric(750000)
)
accumulator <- 1
for(file_name in file_names){
current_data <- read.csv(file_name, colClasses='character')
current_data$Track <- gsub(".gpx.csv$", "", file_name)
routes[accumulator:(nrow(current_data) + accumulator - 1), ] <- current_data[,c('Track', 'Latitude', 'Longitude', 'Altitude','Date','Time'),]
accumulator <- nrow(current_data) + accumulator
}
routes
})()
)
routes <- routes[complete.cases(routes), ]
as.numeric(routes$Longitude)->routes$Longitude
as.numeric(routes$Latitude)->routes$Latitude
as.factor(routes$Track)->routes$Track
require(ggmap)
baseMap <- get_googlemap(
center="Forest Park MO",
maptype="roadmap",
color="bw", zoom=12)
map <- ggmap(baseMap)+geom_path(aes(x=Longitude, y=Latitude, group=Track), data=routes, alpha=0.1, color="red")
#maps no trouble on my old machine in ~2 minutes
map
##this is no good##
#first stab at a heatmap doesn't really work great for this level of data; and doesn't seem to really go any faster
heatmap <- ggmap(baseMap) +
stat_bin2d(
aes(x=Longitude, y=Latitude),
bins=250,
alpha=0.8,
data=routes[c(1:10000),],
)
heatmap+scale_fill_gradient(low='white',high='red')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment