Skip to content

Commit

Permalink
Merge pull request #280 from rOpenGov/use_sf
Browse files Browse the repository at this point in the history
Require always sf for `get_eurostat_geospatial()`
  • Loading branch information
pitkant authored Oct 20, 2023
2 parents e2598fd + c2136a9 commit f68da84
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
47 changes: 24 additions & 23 deletions R/get_eurostat_geospatial.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#' @title Download Geospatial Data from GISCO
#'
#' @description Downloads either a simple features (sf) or a data_frame
#' of NUTS regions. This function is a wrapper of [giscoR::gisco_get_nuts()]
#' of NUTS regions. This function is a wrapper of [giscoR::gisco_get_nuts()].
#' This function requires to have installed the packages \CRANpkg{sf} and
#' \CRANpkg{giscoR}.
#'
#' @seealso [giscoR::gisco_get_nuts()]
#' @param output_class Class of object returned,
Expand Down Expand Up @@ -87,7 +89,7 @@
#' © EuroGeographics for the administrative boundaries
#'
#' Data downloaded using \pkg{giscoR}
#'
#'
#' @inheritSection eurostat-package Eurostat: Copyright notice and free re-use of data
#' @inheritSection eurostat-package Data source: GISCO - General Copyright
#' @inheritSection eurostat-package Data source: GISCO - Administrative Units / Statistical Units
Expand Down Expand Up @@ -120,6 +122,12 @@ get_eurostat_geospatial <- function(output_class = "sf",
cache = TRUE, update_cache = FALSE,
cache_dir = NULL, crs = "4326",
make_valid = "DEPRECATED", ...) {
# nocov start
if (!requireNamespace("sf")) {
message("'sf' package is required for geospatial functionalities")
return(invisible())
}
# nocov end
# Simplified and leaving most of the heavy-lifting to giscoR

# Deprecation messages
Expand Down Expand Up @@ -179,11 +187,6 @@ get_eurostat_geospatial <- function(output_class = "sf",
message("'giscoR' package is required for geospatial functionalities")
return(invisible())
}

if (!requireNamespace("sf")) {
message("'sf' package is required for geospatial functionalities")
return(invisible())
}
# nocov end

message(paste0(
Expand Down Expand Up @@ -230,15 +233,8 @@ get_eurostat_geospatial <- function(output_class = "sf",

# to df
if (output_class == "df") {
# nocov start
if (!requireNamespace("sf", quietly = TRUE)) {
# Remove geometry without sf package
shp$geometry <- NULL
# nocov end
} else {
# Remove geometry
shp <- sf::st_drop_geometry(shp)
}
# Remove geometry
shp <- sf::st_drop_geometry(shp)
}

return(shp)
Expand All @@ -258,15 +254,16 @@ geo_names <- function(x) {
x$id <- x$geo

# Arrange names in proper order
sfcol <- attr(x, "sf_column")
the_geom <- sf::st_geometry(x)
the_df <- sf::st_drop_geometry(x)
rest <- c(
"id", "LEVL_CODE", "NUTS_ID", "CNTR_CODE", "NAME_LATN", "NUTS_NAME",
"MOUNT_TYPE", "URBN_TYPE", "COAST_TYPE", "FID", "geo"
)

# Check what needed columns are not present in the source file
miss_cols <- setdiff(unique(c(rest, sfcol)), names(x))
extra_cols <- setdiff(names(x), unique(c(rest, sfcol)))
miss_cols <- setdiff(unique(rest), names(the_df))
extra_cols <- setdiff(names(the_df), rest)


# Add missing cols with NAs
Expand All @@ -275,10 +272,14 @@ geo_names <- function(x) {
names(template_df) <- x
template_df
})
x <- dplyr::bind_cols(c(list(x), list_df))
new_df <- dplyr::bind_cols(c(list(the_df), list_df))

# Final column order
order_cols <- unique(c(rest, extra_cols, sfcol))
xend <- x[, order_cols]
xend
order_cols <- unique(c(rest, extra_cols))
xend <- new_df[, order_cols]

# Back to sf
final_sf <- sf::st_sf(xend, geometry = the_geom)

final_sf
}
4 changes: 3 additions & 1 deletion man/get_eurostat_geospatial.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test_03_get_eurostat_geospatial.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test_that("get_eurostat_geospatial errors", {
skip_if_not_installed(pkg = "giscoR")
skip_if_not_installed(pkg = "sf")
skip_on_cran()
skip_if_offline()
skip_if(!giscoR::gisco_check_access(), "No access to GISCO")
Expand Down Expand Up @@ -35,6 +36,7 @@ test_that("get_eurostat_geospatial errors", {

test_that("get_eurostat_geospatial messages", {
skip_if_not_installed(pkg = "giscoR")
skip_if_not_installed(pkg = "sf")
skip_on_cran()
skip_if_offline()
skip_if(!giscoR::gisco_check_access(), "No access to GISCO")
Expand All @@ -59,6 +61,7 @@ test_that("get_eurostat_geospatial messages", {


test_that("get_eurostat_geospatial nuts levels", {
skip_if_not_installed(pkg = "sf")
# From internal data with default args
expect_message(all <- get_eurostat_geospatial(nuts_level = "all"), "eurostat")
expect_message(n0 <- get_eurostat_geospatial(nuts_level = "0"), "eurostat")
Expand Down Expand Up @@ -117,6 +120,7 @@ test_that("get_eurostat_geospatial nuts levels", {
})

test_that("get_eurostat_geospatial df", {
skip_if_not_installed(pkg = "sf")
# From internal data with default args
expect_message(
all <- get_eurostat_geospatial(
Expand Down Expand Up @@ -199,6 +203,7 @@ test_that("get_eurostat_geospatial df", {


test_that("get_eurostat_geospatial cache_dir", {
skip_if_not_installed(pkg = "sf")
skip_if_not_installed(pkg = "giscoR")
skip_on_cran()
skip_if_offline()
Expand Down Expand Up @@ -255,6 +260,7 @@ test_that("get_eurostat_geospatial cache_dir", {


test_that("giscoR returns NULL", {
skip_if_not_installed(pkg = "sf")
skip_if_not_installed(pkg = "giscoR")
skip_on_cran()
skip_if_offline()
Expand All @@ -275,6 +281,8 @@ test_that("giscoR returns NULL", {


test_that("Check column names", {
skip_if_not_installed(pkg = "sf")

# See https://github.com/rOpenGov/eurostat/issues/240
col_order <- c(
"id", "LEVL_CODE", "NUTS_ID", "CNTR_CODE", "NAME_LATN",
Expand All @@ -295,6 +303,7 @@ test_that("Check column names", {


test_that("Check column names POLYGONS from GISCO", {
skip_if_not_installed(pkg = "sf")
skip_if_not_installed(pkg = "giscoR")
skip_on_cran()
skip_if_offline()
Expand Down Expand Up @@ -398,6 +407,7 @@ test_that("Check column names POLYGONS from GISCO", {
})

test_that("Check column names LABELS from GISCO", {
skip_if_not_installed(pkg = "sf")
skip_if_not_installed(pkg = "giscoR")
skip_on_cran()
skip_if_offline()
Expand Down Expand Up @@ -523,6 +533,7 @@ test_that("Check column names LABELS from GISCO", {


test_that("Check column names BORDERS from GISCO", {
skip_if_not_installed(pkg = "sf")
skip_if_not_installed(pkg = "giscoR")
skip_on_cran()
skip_if_offline()
Expand Down

0 comments on commit f68da84

Please sign in to comment.