Today I gave my presentation at useR! 2011. I am not satisfied with how it went – I ran over the time by 2 minutes (which is a lot considering it was supposed to be 17 minutes) and forgot to make a main point, which is showing the XML that goes in and out of the packages! So if you’re interested, be sure to run lines 72 to 75 of the code below.

Nevertheless, I still hope I could interest some people, and the one question I got was actually good: It was about uploading data! Maybe I have to implement that before people get excited? We’ll see.

Download the presentation notes and slides and the original abstract:

  • [download id=”28″ format=”3″]
  • [download id=”29″ format=”3″]

s4R – Accessing SensorWeb Data from R

Code:

# Author: Daniel Nuest (daniel.nuest@uni-muenster.de)
# Project: sos4R - visit the project web page, http://www.xn--nrdholmen-07a.net/sos4r
library("sos4R")
sessionInfo()# establish a connection to a SOS instance with default settings
weathersos "http://v-swe.uni-muenster.de:8080/WeatherSOS/sos")
weathersos
summary(weathersos)

################################################################################
# Request data for specific time intervall and offering
names(sosOfferings(weathersos))
sosOfferings(weathersos)[[1]]
off sosObservedProperties(off)
# Exploration of new services is NOT the strong side of sos4R

# offering is only mandatory parameter, but use time interval to limit data
obs # alternative: offering = "ATMOSPHERIC_TEMPERATURE"
eventTime = sosCreateTime(sos = weathersos,
time = "2009-08-10 12:00/2009-08-20 12:00"))
obs
#length(obs)
#str(obs)

# subsettable in the normal way
sosProcedures(obs[1])
sosProcedures(obs[[2]])

# accessor functions
sosBoundedBy(obs)
sosCoordinates(obs[[2]])

sosProcedures(obs)
# vs.
sosProcedures(weathersos)

################################################################################
# get the REAL data
result summary(result)
dim(result); dim(sosResult(obs[[2]]))

# shortcut to spatial data
obs.spdf summary(obs.spdf)
# Metadata are useful!

################################################################################
# Create plot
# Attention: plots ignore the fact that the times do NOT perfectly match!
x plot(x = obs[[1]]@result[[1]][1:x], y = obs[[1]]@result[[3]][1:x],
type = "l", lwd = "2",
col = "steelblue", main = "Temperature in Muenster and Kaernten, 2009",
xlab = "Time (00:00 o'clock)",
ylab = "Temperature (degree C)",
xaxt="n") # do not plot x-axis
r axis.POSIXct(side = 1, x = obs[[1]]@result[[1]][1:x], format = "%d. %h",
at = seq(r[1], r[2], by="days"))
lines(x = obs[[2]]@result[[1]][1:x], y = obs[[2]]@result[[3]][1:x],
col = "orange", lwd = "2")
legend("topleft", legend = c("Muenster", "Kaernten"),
col = c("steelblue", "orange"), lty = 1, bty="n")

################################################################################
# Time series analysis example
timeSeriesDemo()

# Is sos4R useful to you?
timeSeriesDemo(inspect = TRUE)

timeSeriesDemolibrary("xts")

# set up request parameters
station temperatureOffering temperature lastWeek begin = as.POSIXct(Sys.time() - 3600 * 24 * 7),
end = as.POSIXct(Sys.time()))

# make the request
obsLastWeek observedProperty = temperature,
procedure = station,
eventTime = sosCreateEventTimeList(lastWeek),
offering = temperatureOffering,
inspect = inspect)
dataLastWeek

# inspect data
#summary(dataLastWeek)
#names(dataLastWeek)

# create time series from data and plot
tempLastWeek order.by = dataLastWeek[["Time"]])

# calculate regression (polynomial fitting)
temp time x = loess(temp~time,
na.omit(dataLastWeek),enp.target=10)

# create plot
plot(tempLastWeek, main = "Temperature at WeatherSOS-Station in Muenster - LAST WEEK",
xlab = "Time",
ylab = paste("Temperature in", attributes(temp)[["unit of measurement"]]),
major.ticks = "days")
lines(dataLastWeek$Time, x$fitted, col = 'red', lwd=3)

cat("data values: ")
print(periodicity(tempLastWeek))
}

################################################################################
# Spatial analysis example, running while taking questions
demo("airquality")