From 1b0d508cd2bcb7ccf3f7bdf49d9830d0db832891 Mon Sep 17 00:00:00 2001 From: Kevin Cazelles Date: Sat, 29 Jun 2019 19:07:17 -0400 Subject: [PATCH] review how sf objects are built, close #70 :art: --- R/zzz.R | 34 +++++++++---------- ...est_search_nodes.R => test-search_nodes.R} | 0 2 files changed, 16 insertions(+), 18 deletions(-) rename tests/testthat/{test_search_nodes.R => test-search_nodes.R} (100%) diff --git a/R/zzz.R b/R/zzz.R index c438e1c..06722f7 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -75,36 +75,32 @@ resp_to_spatial <- function(x) { if (is.null(x)) { x } else { - # print(length(x)) - # null_to_na(x) - # tp <- lapply(null_to_na(x), switch_sf) - # print(length(tp)) - # do.call(rbind, tp) - suppressWarnings(do.call(rbind, lapply(null_to_na(x), switch_sf))) + dat <- do.call(rbind, lapply(null_to_na(x), + function(y) as.data.frame(y[names(y) != "geom"]))) + spd <- sf::st_sfc( + lapply(lapply(x, function(y) y[names(y) == "geom"]), switch_sf), + crs = 4326) + sf::st_sf(dat, spd) } } ## Build sf object based on geom.type switch_sf <- function(tmp) { - # print("ch") - df_nogeom <- as.data.frame(tmp[names(tmp) != "geom"], - stringsAsFactors = FALSE) - if (is.na(tmp$geom)) { - sf::st_sf(df_nogeom, geom = sf::st_sfc(sf::st_point( - matrix(NA_real_, ncol = 2)), crs = 4326)) + if (!length(tmp$geom)) { + # if NULL + sf::st_point(matrix(NA_real_, ncol = 2)) } else { co <- matrix(unlist(tmp$geom$coordinates), ncol = 2, byrow = TRUE) switch( tmp$geom$type, - Point = sf::st_sf(df_nogeom, geom = sf::st_sfc(sf::st_point(co), - crs = 4326)), - Polygon = sf::st_sf(df_nogeom, geom = sf::st_sfc(sf::st_polygon( - list(co)), crs = 4326)), + Point = sf::st_point(co), + Polygon = sf::st_polygon(list(co)), stop("Only `Point` and `Polygon` are supported.") ) } } + #' Get entries based on foreign key #' #' @param endpoint `character` API entry point @@ -134,6 +130,7 @@ get_from_fkey_net <- function(endpoint, verbose = TRUE, ...) { ) } + get_from_fkey_flt <- function(endpoint, verbose = TRUE, ...) { query <- list(...) resp_to_df_flt( @@ -180,7 +177,8 @@ get_gen <- function(endpoint, query = NULL, limit = 100, verbose = TRUE, ...) { # Loop over pages for (page in 0:pages) { - # if (verbose) cat("Now processing page", page+1, "/", pages+1, " \r") + if (verbose) + # cat("Data retrieval", signif(100*(page+1)/(pages+1), 3), "% \r") query$page <- page resp <- httr::GET(url, config = httr::add_headers(`Content-type` = "application/json"), ua, @@ -194,7 +192,7 @@ get_gen <- function(endpoint, query = NULL, limit = 100, verbose = TRUE, ...) { responses[[page + 1]] <- list(body = resp_raw(resp), response = resp) } } - # if (verbose) cat("\r\n") + # if (verbose) cat("\n") # if (!is.null(errors)) warning("Failed request(s) for page(s): ", paste0(errors, ", ")) diff --git a/tests/testthat/test_search_nodes.R b/tests/testthat/test-search_nodes.R similarity index 100% rename from tests/testthat/test_search_nodes.R rename to tests/testthat/test-search_nodes.R