Get angle/orientation/direction of GPS track using R
Hey,
Today a bit of trSIGonometry with this function to compute the angle/orientation/direction between 2 points of a GPX file.
############################ #Function ############################ angle <- function(pt1, pt2) { conv = 180 / pi # conversion radians / degrees diff <- pt2 - pt1 diff.abs <- abs(diff) if (diff[1]>=0 & diff[2]>=0){angle <- pi/2 - atan(diff.abs[2] / diff.abs[1])} if (diff[1]>=0 & diff[2]<=0){angle <- pi/2 + atan(diff.abs[2] / diff.abs[1])} if (diff[1]<=0 & diff[2]>=0){angle <- 3*pi/2 + atan(diff.abs[2] / diff.abs[1])} if (diff[1]<=0 & diff[2]<=0){angle <- pi + (pi/2- atan(diff.abs[2] / diff.abs[1]))} return(angle * conv) } ############################ #Script ############################ library(rgdal) library(raster) library(data.table) #Set folder setwd("/home/GuruBlard/gpx/") # Load file names filenames <- list.files(pattern=".gpx$") # Iteration through all gpx files for (filename in filenames){ # Load the GPX #ogrListLayers("export.gpx") gpx.trackpoints <- readOGR(dsn = filename, layer= "track_points", stringsAsFactors = F) # Extract the coordinates as a matrix: gpx.tp <- coordinates(gpx.trackpoints) # #Loop through and add angle - the difference between the of the current point and the following one: for (tp in 1:(nrow(gpx.tp)-1)) { gpx.trackpoints$angle[tp] <- angle(gpx.tp[tp,], gpx.tp[tp+1,]) } gpx.trackpoints@data$Date <- substr(as.character(gpx.trackpoints@data$time), 1,10) gpx.trackpoints@data$Hour <- substr(as.character(gpx.trackpoints@data$time),12,19) gpx.trackpoints@data$filename <- filename writeOGR(gpx.trackpoints, paste0(tools::file_path_sans_ext(filename), ".shp"), layer=tools::file_path_sans_ext(filename), driver="ESRI Shapefile") } |