Skip to content

Commit

Permalink
#69 patch geopoint handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Mayer authored and Florian Mayer committed May 18, 2020
1 parent 9c9eb45 commit 649288a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
9 changes: 5 additions & 4 deletions R/handle_ru_geopoints.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Split all WKT geopoints of a submission tibble into their components.
#' Split all geopoints of a submission tibble into their components.
#'
#' \lifecycle{stable}
#'
Expand All @@ -20,8 +20,9 @@
#' strings (if TRUE), default: FALSE.
#' See \code{\link{odata_submission_get}} parameter \code{wkt}.
#' @template param-verbose
#' @return The submissions tibble with all WKT geopoints split into their
#' components by \code{\link{split_geopoint}}.
#' @return The submissions tibble with all geopoints retained in their original
#' format, plus columns of their coordinate components as provided by
#' \code{\link{split_geopoint}}.
#' @export
#' @family utilities
#' @examples
Expand Down Expand Up @@ -51,7 +52,7 @@ handle_ru_geopoints <- function(data,
ru_msg_info(glue::glue("Found geopoints: {x}."))
}

for (colname in gp_cols) { # TODO use mutate_at
for (colname in gp_cols) {
if (colname %in% names(data)) {
if (verbose == TRUE) ru_msg_info(glue::glue("Parsing {colname}..."))
data <- data %>% split_geopoint(as.character(colname), wkt = wkt)
Expand Down
2 changes: 1 addition & 1 deletion R/split_geopoint.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' "POINT (114.00 -31.56 23.56)"
#' )
#' )
#' df_split <- df %>% split_geopoint("loc")
#' df_split <- df %>% split_geopoint("loc", wkt=TRUE)
#' testthat::expect_equal(
#' names(df_split),
#' c("stuff", "loc", "loc_longitude", "loc_latitude", "loc_altitude")
Expand Down
51 changes: 37 additions & 14 deletions tests/testthat/test-handle_ru_geopoints.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
test_that("handle_geopoint annotates GeoJSON points with lon lat alt acc", {
test_that(
"handle_ru_geopoints annotates GeoJSON points with lon lat alt acc",
{

fs <- form_schema(
pid = get_test_pid(),
Expand All @@ -21,7 +23,7 @@ test_that("handle_geopoint annotates GeoJSON points with lon lat alt acc", {
)

geo_fields <- fs %>%
dplyr::filter(type %in% c("geopoint", "geotrace", "geoshape")) %>%
dplyr::filter(type == "geopoint") %>%
magrittr::extract2("ruodk_name")

# This test requires a form with geo fields
Expand All @@ -39,16 +41,18 @@ test_that("handle_geopoint annotates GeoJSON points with lon lat alt acc", {

# GeoJSON should still exist after handle_ru_geopoint as nested lists
testthat::expect_true(
is.list(d[[geo_fields[1]]]),
label = glue::glue("GeoJSON field {geo_fields[i]} should be a nested list")
is.list(d[[geo_fields[i]]]),
label = glue::glue(
"GeoJSON field {geo_fields[i]} should be a nested list"
)
)

# handle_ru_geopoints should have appended new fields with postfixes
# for lon, lat, alt, acc
geofield_lon <- glue::glue("{geo_fields[1]}_longitude")
geofield_lat <- glue::glue("{geo_fields[1]}_latitude")
geofield_alt <- glue::glue("{geo_fields[1]}_altitude")
geofield_acc <- glue::glue("{geo_fields[1]}_accuracy")
geofield_lon <- glue::glue("{geo_fields[i]}_longitude")
geofield_lat <- glue::glue("{geo_fields[i]}_latitude")
geofield_alt <- glue::glue("{geo_fields[i]}_altitude")
geofield_acc <- glue::glue("{geo_fields[i]}_accuracy")
testthat::expect_true(
geofield_lon %in% names(d),
label = glue::glue("handle_ru_geopoint appends {geofield_lon}")
Expand All @@ -65,11 +69,30 @@ test_that("handle_geopoint annotates GeoJSON points with lon lat alt acc", {
geofield_acc %in% names(d),
label = glue::glue("handle_ru_geopoint appends {geofield_acc}")
)
# Fields must be numeric
testthat::expect_true(
is.numeric(d[,geofield_lon][[1]]),
label = glue::glue("handle_ru_geopoint {geofield_lon} is numeric")
)
testthat::expect_true(
is.numeric(d[,geofield_lat][[1]]),
label = glue::glue("handle_ru_geopoint {geofield_lat} is numeric")
)
testthat::expect_true(
is.numeric(d[,geofield_alt][[1]]),
label = glue::glue("handle_ru_geopoint {geofield_alt} is numeric")
)
testthat::expect_true(
is.numeric(d[,geofield_acc][[1]]),
label = glue::glue("handle_ru_geopoint {geofield_acc} is numeric")
)
}

})

test_that("handle_geopoint annotates WKT points with lon lat alt (no acc)", {
test_that(
"handle_ru_geopoints annotates WKT points with lon lat alt (no acc)",
{

fs <- form_schema(
pid = get_test_pid(),
Expand Down Expand Up @@ -110,16 +133,16 @@ test_that("handle_geopoint annotates WKT points with lon lat alt (no acc)", {

# GeoJSON should still exist after handle_ru_geopoint as nested lists
testthat::expect_true(
is.character(d[[geo_fields[1]]]),
is.character(d[[geo_fields[i]]]),
label = glue::glue("WKT field {geo_fields[i]} should be character")
)

# handle_ru_geopoints should have appended new fields with postfixes
# for lon, lat, alt, acc if given
geofield_lon <- glue::glue("{geo_fields[1]}_longitude")
geofield_lat <- glue::glue("{geo_fields[1]}_latitude")
geofield_alt <- glue::glue("{geo_fields[1]}_altitude")
geofield_acc <- glue::glue("{geo_fields[1]}_accuracy")
geofield_lon <- glue::glue("{geo_fields[i]}_longitude")
geofield_lat <- glue::glue("{geo_fields[i]}_latitude")
geofield_alt <- glue::glue("{geo_fields[i]}_altitude")
geofield_acc <- glue::glue("{geo_fields[i]}_accuracy")
testthat::expect_true(
geofield_lon %in% names(d),
label = glue::glue("handle_ru_geopoint appends {geofield_lon}")
Expand Down
7 changes: 1 addition & 6 deletions tests/testthat/test-split_geopoint.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,5 @@ test_that("split_geopoint works with numeric coordinates", {
testthat::expect_true(is.numeric(df_split$loc_altitude))
})






# Code
# usethis::edit_file("R/split_geopoint.R")
# usethis::use_r("split_geopoint")

0 comments on commit 649288a

Please sign in to comment.