From 2b8762d52c381787ec9ed8e1582f087b6a704534 Mon Sep 17 00:00:00 2001 From: noerw Date: Thu, 24 Aug 2017 21:45:19 +0200 Subject: [PATCH] rename osem_as_sf to st_as_sf.sensebox for consistency and better integration with sf --- NAMESPACE | 3 ++- R/00utils.R | 13 ------------- R/box_utils.R | 19 ++++++++++++++++++- R/measurement_utils.R | 12 ++++++++++++ man/osem_as_sf.Rd | 21 --------------------- man/st_as_sf.osem_measurements.Rd | 19 +++++++++++++++++++ man/st_as_sf.sensebox.Rd | 19 +++++++++++++++++++ tools/monitor/get-boxes.R | 2 +- vignettes/osem-intro.Rmd | 3 +-- 9 files changed, 72 insertions(+), 39 deletions(-) delete mode 100644 man/osem_as_sf.Rd create mode 100644 man/st_as_sf.osem_measurements.Rd create mode 100644 man/st_as_sf.sensebox.Rd diff --git a/NAMESPACE b/NAMESPACE index e9e1430..833dc19 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -20,11 +20,12 @@ export(mutate_.osem_measurements) export(mutate_.sensebox) export(osem_as_measurements) export(osem_as_sensebox) -export(osem_as_sf) export(osem_box) export(osem_boxes) export(osem_counts) export(osem_measurements) export(osem_phenomena) +export(st_as_sf.osem_measurements) +export(st_as_sf.sensebox) importFrom(graphics,plot) importFrom(magrittr,"%>%") diff --git a/R/00utils.R b/R/00utils.R index 2c69a67..fa6b766 100644 --- a/R/00utils.R +++ b/R/00utils.R @@ -1,16 +1,3 @@ -# ============================================================================== -# -#' Convert a \code{sensebox} or \code{osem_measurements} dataframe to an -#' \code{\link[sf]{st_sf}} object. -#' -#' @param x The object to convert -#' @param ... maybe more objects to convert -#' @return The object with an st_geometry column attached. -#' @export -osem_as_sf = function (x, ...) { - sf::st_as_sf(x, ..., coords = c('lon', 'lat'), crs = 4326) -} - # parses from/to params for get_measurements_ and get_boxes_ parse_dateparams = function (from, to) { from = utc_date(from) diff --git a/R/box_utils.R b/R/box_utils.R index 0edff4e..cd76b2a 100644 --- a/R/box_utils.R +++ b/R/box_utils.R @@ -10,7 +10,7 @@ plot.sensebox = function (x, ...) { } geom = x %>% - osem_as_sf() %>% + sf::st_as_sf() %>% sf::st_geometry() bbox = sf::st_bbox(geom) @@ -74,6 +74,8 @@ summary.sensebox = function(object, ...) { invisible(object) } +# ============================================================================== +# #' Converts a foreign object to an sensebox data.frame. #' @param x A data.frame to attach the class to #' @export @@ -92,6 +94,8 @@ mutate_.sensebox = dplyr_class_wrapper(osem_as_sensebox) #' @export mutate.sensebox = dplyr_class_wrapper(osem_as_sensebox) +# ============================================================================== +# #' maintains class / attributes after subsetting #' @noRd #' @export @@ -100,3 +104,16 @@ mutate.sensebox = dplyr_class_wrapper(osem_as_sensebox) mostattributes(s) = attributes(x) s } + +# ============================================================================== +# +#' Convert a \code{sensebox} dataframe to an \code{\link[sf]{st_sf}} object. +#' +#' @param x The object to convert +#' @param ... maybe more objects to convert +#' @return The object with an st_geometry column attached. +#' @export +st_as_sf.sensebox = function (x, ...) { + sf:::st_as_sf.data.frame(x, ..., coords = c('lon', 'lat'), crs = 4326) +} + diff --git a/R/measurement_utils.R b/R/measurement_utils.R index bb0b0de..3f4bd18 100644 --- a/R/measurement_utils.R +++ b/R/measurement_utils.R @@ -34,3 +34,15 @@ mutate.osem_measurements = dplyr_class_wrapper(osem_as_measurements) s } +# ============================================================================== +# +#' Convert a \code{osem_measurements} dataframe to an \code{\link[sf]{st_sf}} object. +#' +#' @param x The object to convert +#' @param ... maybe more objects to convert +#' @return The object with an st_geometry column attached. +#' @export +st_as_sf.osem_measurements = function (x, ...) { + sf:::st_as_sf.data.frame(x, ..., coords = c('lon', 'lat'), crs = 4326) +} + diff --git a/man/osem_as_sf.Rd b/man/osem_as_sf.Rd deleted file mode 100644 index 7495a4d..0000000 --- a/man/osem_as_sf.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/00utils.R -\name{osem_as_sf} -\alias{osem_as_sf} -\title{Convert a \code{sensebox} or \code{osem_measurements} dataframe to an -\code{\link[sf]{st_sf}} object.} -\usage{ -osem_as_sf(x, ...) -} -\arguments{ -\item{x}{The object to convert} - -\item{...}{maybe more objects to convert} -} -\value{ -The object with an st_geometry column attached. -} -\description{ -Convert a \code{sensebox} or \code{osem_measurements} dataframe to an -\code{\link[sf]{st_sf}} object. -} diff --git a/man/st_as_sf.osem_measurements.Rd b/man/st_as_sf.osem_measurements.Rd new file mode 100644 index 0000000..f541a36 --- /dev/null +++ b/man/st_as_sf.osem_measurements.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/measurement_utils.R +\name{st_as_sf.osem_measurements} +\alias{st_as_sf.osem_measurements} +\title{Convert a \code{osem_measurements} dataframe to an \code{\link[sf]{st_sf}} object.} +\usage{ +st_as_sf.osem_measurements(x, ...) +} +\arguments{ +\item{x}{The object to convert} + +\item{...}{maybe more objects to convert} +} +\value{ +The object with an st_geometry column attached. +} +\description{ +Convert a \code{osem_measurements} dataframe to an \code{\link[sf]{st_sf}} object. +} diff --git a/man/st_as_sf.sensebox.Rd b/man/st_as_sf.sensebox.Rd new file mode 100644 index 0000000..f28a4f7 --- /dev/null +++ b/man/st_as_sf.sensebox.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/box_utils.R +\name{st_as_sf.sensebox} +\alias{st_as_sf.sensebox} +\title{Convert a \code{sensebox} dataframe to an \code{\link[sf]{st_sf}} object.} +\usage{ +st_as_sf.sensebox(x, ...) +} +\arguments{ +\item{x}{The object to convert} + +\item{...}{maybe more objects to convert} +} +\value{ +The object with an st_geometry column attached. +} +\description{ +Convert a \code{sensebox} dataframe to an \code{\link[sf]{st_sf}} object. +} diff --git a/tools/monitor/get-boxes.R b/tools/monitor/get-boxes.R index 2277f02..d3aac3a 100644 --- a/tools/monitor/get-boxes.R +++ b/tools/monitor/get-boxes.R @@ -15,7 +15,7 @@ neverActive = b[is.na(b$lastMeasurement), ] %>% nrow() b_agg = data.frame(time = Sys.time(), boxcount = nrow(b)) b_agg$model = b$model %>% table() %>% as.list() %>% list() b_agg$exposure = b$exposure %>% table() %>% as.list() %>% list() -b_agg$geometry = b %>% osem_as_sf() %>% st_geometry() %>% list() +b_agg$geometry = b %>% st_as_sf() %>% st_geometry() %>% list() b_agg$phenomena = b %>% osem_phenomena() %>% list() b_agg$active = list( diff --git a/vignettes/osem-intro.Rmd b/vignettes/osem-intro.Rmd index 5f92f2f..9906138 100644 --- a/vignettes/osem-intro.Rmd +++ b/vignettes/osem-intro.Rmd @@ -141,8 +141,7 @@ pm25 = mutate(pm25, invalid = sensorId %in% bad_sensors) Then plot the measuring locations, flagging the outliers: ```{r} -pm25_sf = osem_as_sf(pm25) -st_geometry(pm25_sf) %>% plot(col = factor(pm25$invalid), axes = T) +st_as_sf(pm25) %>% st_geometry() %>% plot(col = factor(pm25$invalid), axes = T) ``` Removing these sensors yields a nicer time series plot: