Skip to content

Instantly share code, notes, and snippets.

@dill
Last active January 3, 2016 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dill/bf2a18c8a2a64576780d to your computer and use it in GitHub Desktop.
Save dill/bf2a18c8a2a64576780d to your computer and use it in GitHub Desktop.
Calculate distance travelled from EXIF data
### blah
library(exif)
# get the files
files <- dir(".", full.names=TRUE)
files <- files[grepl(".JPG", files)]
# get the data
exifs <- read_exif(files)
# just want places and times
exifs <- exifs[ ,c("latitude", "longitude", "timestamp")]
# get the entries with lat/long
exifs <- subset(exifs, latitude!=0 & longitude!=0)
# convert to some poorly thought-out projection
library(rgdal)
library(maptools)
segsp <- SpatialPoints(exifs[,c("longitude", "latitude")])
# give the sp object a projection
proj4string(segsp) <-CRS("+proj=longlat +datum=WGS84")
# re-project
segsp.t <- spTransform(segsp, CRSobj=CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"))
# make a data.frame and sort it by time
coords_time <- cbind.data.frame(segsp.t@coords, exifs$timestamp)
library(lubridate)
coords_time[,3] <- ymd_hms(coords_time[,3])
coords_time <- coords_time[order(coords_time[,3]),]
# now calculate some distances
dists <- sqrt(diff(coords_time[,1])^2 + diff(coords_time[,2])^2)
overall_dist <- sum(dists)
# save sp object
save(segsp, file="sp.RData")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment