Details for plot-overland.ipynb

Published by gedankenstuecke

Description

Use GPS data collected with Overland on an iOS device and plot it on a map

0

Tags & Data Sources

GPS location data location movement maps mapping Overland connection

Comments

Please log in to comment.

Notebook
Last updated 1 week, 1 day ago

Analyzing your GPS data from Overland

This notebook requires you to use the Overland connection to get your GPS data from your iPhone into Open Humans.

This notebook then uses the GPS data to plot your personal movement history using R.

For a start let's load our required packages:

In [9]:
library(httr)
library(jsonlite)
library(ggplot2)
library(devtools)

With that out of the way we can access our Overland data from our Open Humans account:

In [10]:
access_token <- Sys.getenv("OH_ACCESS_TOKEN")
url <- paste("https://www.openhumans.org/api/direct-sharing/project/exchange-member/?access_token=",access_token,sep="")
resp <- GET(url)
user <- content(resp, "parsed")

for (data_source in user$data){
    if (data_source$source == "direct-sharing-186" & grepl('overland-data', data_source$basename)){
         gps_data_url <- data_source$download_url
    }
}

temp <- tempfile()
download.file(gps_data_url,temp,method='wget')
json_data <- fromJSON(txt=temp)

Getting started with the data processing

Now that we have our fitbit data stored in json_data we can start to work with that data. Much of this notebook is adapted code from Shirin Glander's excellent blogpost, which is based on Google Location History data but the same principles apply for our Overland data.

Let's read the location data in to the loc variable along with the velocity and the date:

In [11]:
# extracting the locations dataframe
loc = json_data$geometry
properties = json_data$properties
loc$velocity <- properties$speed
loc$date <- properties$timestamp

Now we need to convert the coordinates as they are still in a weird format of having both latitude & longitude in one list. We'll fix this:

In [12]:
library(purrr)
loc$lon <- map(loc$coordinates,1)
loc$lat <- map(loc$coordinates,2)

loc$lat <- as.numeric(loc$lat)
loc$lon <- as.numeric(loc$lon)

Analyzing the data

Let's install the required ggmap package and load some more data visualization tools:

In [13]:
install.packages('ggmap')
library(lubridate)
library(zoo)
# set up plotting theme
library(ggplot2)
library(ggmap)

my_theme <- function(base_size = 12, base_family = "sans"){
  theme_grey(base_size = base_size, base_family = base_family) +
  theme(
    axis.text = element_text(size = 12),
    axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
    axis.title = element_text(size = 14),
    panel.grid.major = element_line(color = "grey"),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "aliceblue"),
    strip.background = element_rect(fill = "lightgrey", color = "grey", size = 1),
    strip.text = element_text(face = "bold", size = 12, color = "navy"),
    legend.position = "right",
    legend.background = element_blank(),
    panel.margin = unit(.5, "lines"),
    panel.border = element_rect(color = "grey", fill = NA, size = 0.5)
  )
}
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done

Let's make a map

The Bay Area

We will start by looking at a map of the Bay Area where I'm living. Below we define our boundaries that we want to plot by giving the latitude/longitude of the map we want to see. You can just adjust these points to the place on earth that you want to plot!

In [14]:
berkeley_boundary_west=-122.6
berkeley_boundary_east=-122.25
berkeley_boundary_south=37.74
berkeley_boundary_north=37.88
loc_2 <- loc[which(!is.na(loc$velocity)), ]

berkeley <- get_map(location=c(berkeley_boundary_west,
                               berkeley_boundary_south,
                               berkeley_boundary_east,
                               berkeley_boundary_north), source = "stamen",maptype='toner',zoom=12)
Map from URL : http://tile.stamen.com/toner/12/653/1581.png
Warning message in file.remove(index[[url]]):
“cannot remove file '460505855160e09d10be0ab48e3027fd.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/654/1581.png
Warning message in file.remove(index[[url]]):
“cannot remove file '9f0228160f70d9d39493f4e9467fc474.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/655/1581.png
Warning message in file.remove(index[[url]]):
“cannot remove file 'c1afe33305ada37c7225ac598f39201e.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/656/1581.png
Warning message in file.remove(index[[url]]):
“cannot remove file 'c42c350a9a3462e536ae32b2781328ad.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/657/1581.png
Warning message in file.remove(index[[url]]):
“cannot remove file '8b558a341d5a50dd91f1846edc759b2a.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/653/1582.png
Warning message in file.remove(index[[url]]):
“cannot remove file '678f4a92f486c9d8f2983adc3e5350da.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/654/1582.png
Warning message in file.remove(index[[url]]):
“cannot remove file '678ffdfaf1962278edf8e433df572a87.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/655/1582.png
Warning message in file.remove(index[[url]]):
“cannot remove file 'b9147f9a9a248284bda6812819b0e867.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/656/1582.png
Warning message in file.remove(index[[url]]):
“cannot remove file '553d6ebf9ff2327337daa0c61bc88781.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/657/1582.png
Warning message in file.remove(index[[url]]):
“cannot remove file '67ad09fe3a781d176036a0fb47ecae4b.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/653/1583.png
Warning message in file.remove(index[[url]]):
“cannot remove file '3b5c5d5933bd643dfe84861620a9c472.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/654/1583.png
Warning message in file.remove(index[[url]]):
“cannot remove file '19c17fc808652c1bf7cd2326a5098acf.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/655/1583.png
Warning message in file.remove(index[[url]]):
“cannot remove file '33ca101231e352286cbc487aee710dc6.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/656/1583.png
Warning message in file.remove(index[[url]]):
“cannot remove file 'df1c4d2efc1590d8bc945d4f422c5fdf.rds', reason 'No such file or directory'”Map from URL : http://tile.stamen.com/toner/12/657/1583.png
Warning message in file.remove(index[[url]]):
“cannot remove file 'de7a779920bc725c8ae9520ecb85f350.rds', reason 'No such file or directory'”
In [15]:
ggmap(berkeley) + 
    geom_point(data = subset(loc_2, as.Date(loc_2$date) > as.Date('2018-10-10')), 
                aes(x = lon, 
                    y = lat, 
                    color = velocity/1000*60*60,
                    alpha=(velocity/1000*60*60)/max(loc_2$velocity/1000*60*60)),size=0.7) + 
  theme(legend.position = "right") + 
  labs(x = "Longitude", y = "Latitude", 
       title = "Location history data points in Berkeley",
       subtitle = "Color scale shows velocity measured for location") +
  scale_colour_gradient(low = "blue", high = "red", guide = guide_legend(title = "Velocity")) + 
    scale_alpha_continuous('Velocity')

Looks like I haven't moved around much since starting to use Overland! But let's see how that will develop in the future!