str(knitr::opts_chunk$get())

knitr::opts_chunk$set(
  comment="",
  collapse = FALSE,
  echo = TRUE,
  cache = TRUE)

Overview

getting started - SOS4R

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).

SOS4R - project facts

In a nutshell , sos4R...

Sponsors

sos4R - convenience API

The key functions of the convenience API are:

The convenience API wraps and combines the default API reusing the available functions in sos4R.

Phenomena

Setting up the SOS

library("sos4R")

niwaHydro <- SOS(url = "https://climate-sos.niwa.co.nz/",
                 binding = "KVP", 
                 useDCPs = FALSE, 
                 version = "2.0.0")

Phenomena{.allowframebreaks}

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))

Sites

List sites {.allowframebreaks}

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)

Filter Sites {.allowframebreaks}

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")

Sites as List

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)

getData

Data download {.allowframebreaks}

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

Plotting and Analytics

Time Series Plotting {.allowframebreaks}

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)")

Analytics {.allowframebreaks}

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")

Multiple Time Series {.allowframebreaks}

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")


52North/sos4R documentation built on Jan. 30, 2021, 11:42 p.m.