Reorganise R files and connect with python test code
This commit is contained in:
@@ -1,34 +1,10 @@
|
|||||||
library(XML)
|
# Calculate distances (in metres) using the function pointDistance from the 'raster' package.
|
||||||
library(raster)
|
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.
|
# Shift vectors for lat and lon so that each row also contains the next position.
|
||||||
geodf$lat.p1 <- shift.vec(geodf$lat, -1)
|
geodf$lat.p1 <- shift.vec(geodf$lat, -1)
|
||||||
geodf$lon.p1 <- shift.vec(geodf$lon, -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!
|
# Parameter 'lonlat' has to be TRUE!
|
||||||
geodf$dist.to.prev <- apply(geodf, 1, FUN = function (row) {
|
geodf$dist.to.prev <- apply(geodf, 1, FUN = function (row) {
|
||||||
pointDistance(c(as.numeric(row["lat.p1"]),
|
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"])),
|
c(as.numeric(row["lat"]), as.numeric(row["lon"])),
|
||||||
lonlat = T)
|
lonlat = T)
|
||||||
})
|
})
|
||||||
|
|
||||||
# Transform the column 'time' so that R knows how to interpret it.
|
# Transform the column 'time' so that R knows how to interpret it.
|
||||||
geodf$time <- strptime(geodf$time, format = "%Y-%m-%dT%H:%M:%OS")
|
geodf$time <- strptime(geodf$time, format = "%Y-%m-%dT%H:%M:%OS")
|
||||||
|
|
||||||
# Shift the time vector, too.
|
# Shift the time vector, too.
|
||||||
geodf$time.p1 <- shift.vec(geodf$time, -1)
|
geodf$time.p1 <- shift.vec(geodf$time, -1)
|
||||||
|
|
||||||
# Calculate the number of seconds between two positions.
|
# Calculate the number of seconds between two positions.
|
||||||
geodf$time.diff.to.prev <- as.numeric(difftime(geodf$time.p1, geodf$time))
|
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.
|
# 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.m.per.sec <- geodf$dist.to.prev / geodf$time.diff.to.prev
|
||||||
geodf$speed.km.per.h <- geodf$speed.m.per.sec * 3.6
|
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.speed <- lowess(geodf$speed.km.per.h, f = 0.01)$y
|
||||||
geodf$lowess.ele <- lowess(geodf$ele, f = 0.02)$y
|
geodf$lowess.ele <- lowess(geodf$ele, f = 0.02)$y
|
||||||
|
|
||||||
|
|
||||||
# Plot elevations and smoother
|
# Plot elevations and smoother
|
||||||
plot(geodf$ele, type = "l", bty = "n", xaxt = "n", ylab = "Elevatio", xlab = "", col = "grey40")
|
plot(geodf$ele, type = "l", bty = "n", xaxt = "n", ylab = "Elevatio", xlab = "", col = "grey40")
|
||||||
lines(geodf$lowess.ele, col = "red", lwd = 3)
|
lines(geodf$lowess.ele, col = "red", lwd = 3)
|
||||||
4
flow.py
4
flow.py
@@ -114,8 +114,8 @@ class Flow:
|
|||||||
for item in contents:
|
for item in contents:
|
||||||
self.downloadTraining(item['listItemId'])
|
self.downloadTraining(item['listItemId'])
|
||||||
|
|
||||||
def processTraining(self):
|
def processTraining(self, session):
|
||||||
proc = subprocess.Popen(['RScript','test.R', 'Rik_Veenboer_2015-04-25_10-14-43'], stdout = subprocess.PIPE, universal_newlines = True)
|
proc = subprocess.Popen(['RScript','test.R', session], stdout = subprocess.PIPE, universal_newlines = True)
|
||||||
while True:
|
while True:
|
||||||
line = proc.stdout.readline()
|
line = proc.stdout.readline()
|
||||||
if line != '':
|
if line != '':
|
||||||
|
|||||||
36
test.R
36
test.R
@@ -1,2 +1,34 @@
|
|||||||
args <- commandArgs(trailingOnly = TRUE)
|
library(XML)
|
||||||
print(args)
|
|
||||||
|
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))
|
||||||
2
test.py
2
test.py
@@ -21,7 +21,7 @@ with Flow() as flow:
|
|||||||
# print(start <= test <= end)
|
# print(start <= test <= end)
|
||||||
# flow.getEventsInRange(start, end)
|
# flow.getEventsInRange(start, end)
|
||||||
# flow.parseCalenderEvents()
|
# flow.parseCalenderEvents()
|
||||||
flow.processTraining()
|
flow.processTraining('Rik_Veenboer_2015-11-10_17-21-59')
|
||||||
|
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
|||||||
10
xtest.R
10
xtest.R
@@ -2,12 +2,15 @@
|
|||||||
#install.packages("plotKML")
|
#install.packages("plotKML")
|
||||||
#install.packages("maptools")
|
#install.packages("maptools")
|
||||||
#install.packages("gpsbabel")
|
#install.packages("gpsbabel")
|
||||||
cat("\014")
|
|
||||||
library(XML)
|
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)
|
data = xmlParse(file)
|
||||||
|
|
||||||
@@ -28,3 +31,4 @@ colnames(xx) = c("lat", "lon")
|
|||||||
|
|
||||||
|
|
||||||
xxx=merge(result, xx)
|
xxx=merge(result, xx)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user