You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
opensensmapR/inst/doc/osem-intro.R

76 lines
2.5 KiB
R

2 years ago
## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)
2 years ago
## ----results = FALSE----------------------------------------------------------
2 years ago
library(magrittr)
library(opensensmapr)
2 years ago
# all_sensors = osem_boxes(cache = '.')
all_sensors = readRDS('boxes_precomputed.rds') # read precomputed file to save resources
2 years ago
## -----------------------------------------------------------------------------
summary(all_sensors)
2 years ago
## ---- message=FALSE, warning=FALSE--------------------------------------------
2 years ago
plot(all_sensors)
## -----------------------------------------------------------------------------
phenoms = osem_phenomena(all_sensors)
str(phenoms)
## -----------------------------------------------------------------------------
phenoms[phenoms > 20]
2 years ago
## ----results = FALSE, eval=FALSE----------------------------------------------
# pm25_sensors = osem_boxes(
# exposure = 'outdoor',
# date = Sys.time(), # ±4 hours
# phenomenon = 'PM2.5'
# )
2 years ago
## -----------------------------------------------------------------------------
2 years ago
pm25_sensors = readRDS('pm25_sensors.rds') # read precomputed file to save resources
2 years ago
summary(pm25_sensors)
plot(pm25_sensors)
2 years ago
## ---- results=FALSE, message=FALSE--------------------------------------------
2 years ago
library(sf)
library(units)
library(lubridate)
library(dplyr)
2 years ago
## ----bbox, results = FALSE, eval=FALSE----------------------------------------
# # construct a bounding box: 12 kilometers around Berlin
# berlin = st_point(c(13.4034, 52.5120)) %>%
# st_sfc(crs = 4326) %>%
# st_transform(3857) %>% # allow setting a buffer in meters
# st_buffer(set_units(12, km)) %>%
# st_transform(4326) %>% # the opensensemap expects WGS 84
# st_bbox()
# pm25 = osem_measurements(
# berlin,
# phenomenon = 'PM2.5',
# from = now() - days(3), # defaults to 2 days
# to = now()
# )
#
2 years ago
## -----------------------------------------------------------------------------
2 years ago
pm25 = readRDS('pm25_berlin.rds') # read precomputed file to save resources
plot(pm25)
## ---- warning=FALSE-----------------------------------------------------------
2 years ago
outliers = filter(pm25, value > 100)$sensorId
2 years ago
bad_sensors = outliers[, drop = TRUE] %>% levels()
2 years ago
pm25 = mutate(pm25, invalid = sensorId %in% bad_sensors)
## -----------------------------------------------------------------------------
2 years ago
st_as_sf(pm25) %>% st_geometry() %>% plot(col = factor(pm25$invalid), axes = TRUE)
2 years ago
## -----------------------------------------------------------------------------
pm25 %>% filter(invalid == FALSE) %>% plot()