Reorganise R files and connect with python test code

This commit is contained in:
2015-12-18 18:06:28 +00:00
parent 2369ca508d
commit 3b92594fb7
6 changed files with 52 additions and 35 deletions

View File

@@ -1,34 +1,10 @@
library(XML)
# Calculate distances (in metres) using the function pointDistance from the 'raster' package.
library(raster)
shift.vec <- function (vec, shift) {
if(length(vec) <= abs(shift)) {
rep(NA ,length(vec))
}else{
if (shift >= 0) {
c(rep(NA, shift), vec[1:(length(vec)-shift)]) }
else {
c(vec[(abs(shift)+1):length(vec)], rep(NA, abs(shift))) } } }
pfile <- htmlTreeParse("Rik_Veenboer_2015-11-10_17-21-59.gpx", useInternalNodes = T)
# Get all elevations, times and coordinates via the respective xpath
elevations <- as.numeric(xpathSApply(pfile, path = "//trkpt/ele", xmlValue))
times = xpathSApply(pfile, path = "//trkpt/time", xmlValue)
coords <- xpathSApply(pfile, path = "//trkpt", xmlAttrs)
# Extract latitude and longitude from the coordinates
lats <- as.numeric(coords["lat",])
lons <- as.numeric(coords["lon",])
# Put everything in a dataframe and get rid of old variables
geodf <- data.frame(lat = lats, lon = lons, ele = elevations, time = times)
rm(list=c("elevations", "lats", "lons", "pfile", "times", "coords"))
head(geodf)
# Shift vectors for lat and lon so that each row also contains the next position.
geodf$lat.p1 <- shift.vec(geodf$lat, -1)
geodf$lon.p1 <- shift.vec(geodf$lon, -1)
# Calculate distances (in metres) using the function pointDistance from the 'raster' package.
# Parameter 'lonlat' has to be TRUE!
geodf$dist.to.prev <- apply(geodf, 1, FUN = function (row) {
pointDistance(c(as.numeric(row["lat.p1"]),
@@ -36,12 +12,16 @@ geodf$dist.to.prev <- apply(geodf, 1, FUN = function (row) {
c(as.numeric(row["lat"]), as.numeric(row["lon"])),
lonlat = T)
})
# Transform the column 'time' so that R knows how to interpret it.
geodf$time <- strptime(geodf$time, format = "%Y-%m-%dT%H:%M:%OS")
# Shift the time vector, too.
geodf$time.p1 <- shift.vec(geodf$time, -1)
# Calculate the number of seconds between two positions.
geodf$time.diff.to.prev <- as.numeric(difftime(geodf$time.p1, geodf$time))
# Calculate metres per seconds, kilometres per hour and two LOWESS smoothers to get rid of some noise.
geodf$speed.m.per.sec <- geodf$dist.to.prev / geodf$time.diff.to.prev
geodf$speed.km.per.h <- geodf$speed.m.per.sec * 3.6
@@ -50,7 +30,6 @@ geodf$speed.km.per.h <- ifelse(geodf$speed.km.per.h > 40, 0, geodf$speed.km.per.
geodf$lowess.speed <- lowess(geodf$speed.km.per.h, f = 0.01)$y
geodf$lowess.ele <- lowess(geodf$ele, f = 0.02)$y
# Plot elevations and smoother
plot(geodf$ele, type = "l", bty = "n", xaxt = "n", ylab = "Elevatio", xlab = "", col = "grey40")
lines(geodf$lowess.ele, col = "red", lwd = 3)

View File

@@ -114,8 +114,8 @@ class Flow:
for item in contents:
self.downloadTraining(item['listItemId'])
def processTraining(self):
proc = subprocess.Popen(['RScript','test.R', 'Rik_Veenboer_2015-04-25_10-14-43'], stdout = subprocess.PIPE, universal_newlines = True)
def processTraining(self, session):
proc = subprocess.Popen(['RScript','test.R', session], stdout = subprocess.PIPE, universal_newlines = True)
while True:
line = proc.stdout.readline()
if line != '':

2
stash.R Normal file
View File

@@ -0,0 +1,2 @@
cat("\014")
setwd(dirname(parent.frame(2)$ofile))

36
test.R
View File

@@ -1,2 +1,34 @@
args <- commandArgs(trailingOnly = TRUE)
print(args)
library(XML)
args = commandArgs(trailingOnly = TRUE)
datadir = 'data'
session = args[1]
filename = paste(datadir, '/', session, '.gpx', sep = '')
print(filename)
shift.vec <- function (vec, shift) {
if(length(vec) <= abs(shift)) {
rep(NA ,length(vec))
}else{
if (shift >= 0) {
c(rep(NA, shift), vec[1:(length(vec)-shift)]) }
else {
c(vec[(abs(shift)+1):length(vec)], rep(NA, abs(shift))) } } }
pfile <- htmlTreeParse("data/Rik_Veenboer_2015-11-10_17-21-59.gpx", useInternalNodes = T)
# Get all elevations, times and coordinates via the respective xpath
elevations <- as.numeric(xpathSApply(pfile, path = "//trkpt/ele", xmlValue))
times = xpathSApply(pfile, path = "//trkpt/time", xmlValue)
coords <- xpathSApply(pfile, path = "//trkpt", xmlAttrs)
# Extract latitude and longitude from the coordinates
lats <- as.numeric(coords["lat",])
lons <- as.numeric(coords["lon",])
# Put everything in a dataframe and get rid of old variables
geodf <- data.frame(lat = lats, lon = lons, ele = elevations, time = times)
rm(list=c("elevations", "lats", "lons", "pfile", "times", "coords"))
# Output head of frame
print(head(geodf))

View File

@@ -21,7 +21,7 @@ with Flow() as flow:
# print(start <= test <= end)
# flow.getEventsInRange(start, end)
# flow.parseCalenderEvents()
flow.processTraining()
flow.processTraining('Rik_Veenboer_2015-11-10_17-21-59')
sys.exit()

10
xtest.R
View File

@@ -2,12 +2,15 @@
#install.packages("plotKML")
#install.packages("maptools")
#install.packages("gpsbabel")
cat("\014")
library(XML)
cat("\014")
setwd(dirname(parent.frame(2)$ofile))
file = "Rik_Veenboer_2015-11-10_17-21-59.gpx"
file = "test.gpx"
file = "data/Rik_Veenboer_2015-11-10_17-21-59.gpx"
#file = "test.gpx"
data = xmlParse(file)
@@ -28,3 +31,4 @@ colnames(xx) = c("lat", "lon")
xxx=merge(result, xx)