str(knitr::opts_chunk$get()) knitr::opts_chunk$set( comment="", collapse = FALSE, echo = TRUE, cache = TRUE)
R is a statistical tool and programming language tailored for data analysis including spatial data.
It allows to query data from standard conform SOS instances using simple R function calls and does no require any knowledge about the Sensor Web. It is easily extendible for new data models and opens the huge amount of analysis and visualization features of the R environment for the Sensor Web. https://52north.github.io/sos4R/
sos4R
includes a collection of convenience functions which wrap the complex SOS interface with its specific terms (e.g. FOI, procedure).
The wrapper function use more generic terms easily accessible for all users, especially without a strong knowledge of the OGC standards of the Sensor Web Enablement (see "OGC SWE and SOS" vignette for details).
In a nutshell , sos4R...
took an estimated 13 years of effort (COCOMO model) starting with its first commit in May, 2013 ending with its most recent commit about three weeks ago
CRAN: https://cran.r-project.org/package=sos4R
Sponsors
The key functions of the convenience API are:
The convenience API wraps and combines the default API reusing the available functions in sos4R.
library("sos4R") niwaHydro <- SOS(url = "https://climate-sos.niwa.co.nz/", binding = "KVP", useDCPs = FALSE, version = "2.0.0")
The function phenomena(..)
provides information about observed phenomena and time periods of data.
phenomena <- phenomena(sos = niwaHydro) str(phenomena)
head(phenomena)
\framebreak
The retrieved data can be extended by time intervals and site identifier for which data is available.
head(phenomena(sos = niwaHydro, includeTemporalBBox = TRUE))
\framebreak
head(phenomena(sos = niwaHydro, includeSiteId = TRUE))
\framebreak
One can also add both temporal extent and sites.
head(phenomena(sos = niwaHydro, includeTemporalBBox = TRUE, includeSiteId = TRUE))
The function sites(..)
provides information about sites where observations are made, including metadata about the sites (e.g. location).
The returned object is a SpatialPointsDataFrame
.
sites <- sites(sos = niwaHydro) head(sites)
\framebreak
One can retrieve additional metadata about the phenomena and the time period for which data is available.
Including temporal extent implies inclusion of phenomena.
In the next chunks the object is coerced to a date.frame
to get a tabular view.
sitesPhen <- sites(sos = niwaHydro, includePhenomena = TRUE) head(colnames(sitesPhen@data)) colnames(sitesPhen@data)[c(1,3,80)] <- c("ID", "FOG", "RAIN")
\framebreak
sitesPhen[,c(1,3,80)]
\framebreak
sitesTempBB <- sites(sos = niwaHydro, includeTemporalBBox = TRUE, includePhenomena = TRUE) str(sitesTempBB[1:3,c(1,3,80)]@data)
One can filter sites using phenomena and temporal extent.
head(sites(sos = niwaHydro, phenomena = phenomena[3,]))
\framebreak
head(sites(sos = niwaHydro, begin = as.POSIXct("1904-01-01"), end = as.POSIXct("1905-12-31")))
\framebreak
The SpatialPointsDataFrame
allows access to coordinates with coordinate reference system (CRS).
library(sp) coordinates(sites)[1:3,]
sites@proj4string
bbox(sites)
\framebreak
One way of plotting and exploring the sites is:
library("mapview") mapview(sites[-16,], legend=FALSE, col.regions="#65c6e4")
The function siteList(..)
is an analogue, but the result is a list rather than a spatial object.
siteList <- siteList(sos = niwaHydro) str(siteList) head(siteList)
The function getData(..)
retrieves data and returns them in a long form 'data.frame' that is ready-to-use for the xts
or spacetime
package.
The returned data can be limited by thematic, spatial, and temporal filters. Thematic filtering (phenomena) support the values of the previous functions as inputs. Spatial filters are either sites, or a bounding box. Temporal filter is a time period during which observations are made.
Without a temporal extent, the used SOS only returns the last measurement. \footnotesize
obsData <- getData(sos = niwaHydro, phenomena = phenomena[18,1], sites = siteList[1:2,1] ) str(obsData,1)
\normalsize \framebreak
The result data.frame
includes additional metadata.
attributes(obsData[[3]])
Request more data with a temporal extent for all sites.
obsData <- getData(sos = niwaHydro, phenomena = phenomena[18,1], sites = siteList[1,1], begin = parsedate::parse_iso_8601("1970-01-01T12:00:00+12:00"), end = parsedate::parse_iso_8601(Sys.time()))
\footnotesize
str(obsData, 2)
\normalsize
Plot received data as time series:
library(xts) ts1056 <- xts(obsData[obsData$siteID == '1056',3], obsData[obsData$siteID == '1056', "timestamp"]) plot(x = ts1056, main = "Monthly: Extreme max. Temp. (°C)")
Conversion of the time series:
extTs <- merge(ts1056, as.xts(ts(start = c(1981,10), end = c(2018, 8), frequency = 12))) smplTs <- ts(as.numeric(extTs[,1]), c(1981, 10), frequency = 12) smplTsFill <- imputeTS::na.interpolation(smplTs) decTs <- decompose(smplTsFill)
\framebreak
plot(decTs) lm(y~x, data.frame(y=as.numeric(decTs$trend), x=1:length(decTs$trend)))
plot(decTs$trend,xlim=c(1981, 2020), main="Simple linear trend of monthly maxima") abline(22.96-(1981+9/12)*0.0022*12, 0.0022*12, col="red")
You can also retrieve data for phenomena from multiple sites.
multipleSites <- siteList$siteID[35:38] obsData <- getData(sos = niwaHydro, phenomena = phenomena[18,1], sites = sites[1:2,]$siteID, # siteList[1:2,1] begin = parsedate::parse_iso_8601(Sys.time()-5.5*365*24*60*60), end = parsedate::parse_iso_8601(Sys.time()) ) ts1056 <- xts(obsData[obsData$siteID == '1056',3], obsData[obsData$siteID == '1056',"timestamp"]) names(ts1056) <- "Station__1056" ts11234 <- xts(obsData[obsData$siteID == '11234',3], obsData[obsData$siteID == '11234',"timestamp"]) names(ts11234) <- "Station_11234" p <- plot(x = na.fill(merge(ts1056, ts11234), list(NA, "extend", NA)), main = "Monthly: Extreme max. Temp. (°C)", ylim=c(14,41)) addLegend("topleft", ncol=2, col = c("black", "red"), lty=1) plot(p)
\framebreak
library(sp) mapview(sites[1:2,], map.types="OpenTopoMap")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.