Skip to content

Commit

Permalink
code format with air
Browse files Browse the repository at this point in the history
  • Loading branch information
PMassicotte committed Feb 27, 2025
1 parent 92e7826 commit f035e21
Show file tree
Hide file tree
Showing 23 changed files with 199 additions and 157 deletions.
10 changes: 5 additions & 5 deletions R/check_data_exist.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
#' @return TRUE or FALSE
#'
#' @export
check_data_exist <- function(scale = 110,
type,
category = c("cultural", "physical", "raster")) {
check_data_exist <- function(
scale = 110L,
type,
category = c("cultural", "physical", "raster")
) {
# check permitted category
category <- match.arg(category)

Expand All @@ -42,7 +44,6 @@ check_data_exist <- function(scale = 110,
# check on permitted scales, convert names to numeric
scale <- check_scale(scale)


df_data <- read.csv(
system.file(
"extdata",
Expand All @@ -51,7 +52,6 @@ check_data_exist <- function(scale = 110,
)
)


# first check if type is within the list
if (!type %in% df_data$type) {
warning(
Expand Down
16 changes: 7 additions & 9 deletions R/find_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
#' @examples \dontrun{
#' ne_find_vector_data(scale = 10, category = "physical")
#' }
ne_find_vector_data <- function(scale = 110,
category = c("cultural", "physical"),
getmeta = FALSE) {
ne_find_vector_data <- function(
scale = 110L,
category = c("cultural", "physical"),
getmeta = FALSE
) {
## check permitted category (no way to check against available rasters)
category <- match.arg(category)

Expand All @@ -31,7 +33,6 @@ ne_find_vector_data <- function(scale = 110,
# scale has already been checked in check_scale, and category in match.arg
path <- paste0(scale, "m_", category)


## call to ne_git_contents returns a list with contents of
## github directory (based on specified path), github api
## response, and http path.
Expand All @@ -58,7 +59,6 @@ ne_find_vector_data <- function(scale = 110,
)
}


return(layers)
}

Expand Down Expand Up @@ -94,7 +94,7 @@ ne_git_contents <- function(path) {
df <- httr::content(x = resp, as = "text", encoding = "UTF-8")
df <- jsonlite::fromJSON(df)

if (httr::status_code(resp) != 200) {
if (httr::status_code(resp) != 200L) {
stop(
sprintf(
"GitHub API request failed [%s]\n%s\n<%s>",
Expand Down Expand Up @@ -135,7 +135,7 @@ ne_git_layer_names <- function(x, scale, getmeta) {
## creates a list of available layer names and
## list of metadata links

if (httr::status_code(x$response) != 200) {
if (httr::status_code(x$response) != 200L) {
stop(
sprintf(
"GitHub API request failed [%s]\n%s\n<%s>",
Expand All @@ -150,7 +150,6 @@ ne_git_layer_names <- function(x, scale, getmeta) {
## Create the pattern that matches the prefix that should be removed
prefix <- paste0("ne_", scale, "m_")


## clean and return layer names
l <- x$content
# gets just readme because 1 per layer
Expand Down Expand Up @@ -182,7 +181,6 @@ ne_git_layer_names <- function(x, scale, getmeta) {
link <- gsub("\" />", "", link)
}


## iterate findlinks function through each html page to
## pull out the metadata link. Adding an optional status bar
## since downloads could stall.
Expand Down
28 changes: 14 additions & 14 deletions R/get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,42 @@
#'
#' @return A \code{sf} object.
get_data <- function(
scale = 110,
type = c(
"countries",
"map_units",
"sovereignty",
"tiny_countries"
)) {
scale = 110L,
type = c(
"countries",
"map_units",
"sovereignty",
"tiny_countries"
)
) {
# check on permitted scale arg, convert names to numeric
scale <- check_scale(scale)
# check permitted type arg
type <- match.arg(type)

# tiny_countries not available at scale 10
if (type == "tiny_countries" && scale == 10) {
if (type == "tiny_countries" && scale == 10L) {
stop("tiny_countries are not available at scale 10, use scale 50 or 110")
}

# check for the data packages and try to install if not there
# avoid this check for one example dataset in this package (i.e. countries110)

if (scale == 10) {
if (scale == 10L) {
check_rnaturalearthhires()
} else if (!(scale == 110 && type == "countries")) {
} else if (!(scale == 110L && type == "countries")) {
check_rnaturalearthdata()
}

# choose which map based on type and scale (stored in different packages)

if (scale == 110 && type == "countries") {
if (scale == 110L && type == "countries") {
sf_object <- getExportedValue("rnaturalearth", paste0(type, scale))
} else if (scale == 110 || scale == 50) {
} else if (scale == 110L || scale == 50L) {
sf_object <- getExportedValue("rnaturalearthdata", paste0(type, scale))
} else if (scale == 10) {
} else if (scale == 10L) {
sf_object <- getExportedValue("rnaturalearthhires", paste0(type, scale))
}


return(sf_object)
}
16 changes: 10 additions & 6 deletions R/install-rnaturalearthdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ check_rnaturalearthdata <- function() {
message("The rnaturalearthdata package needs to be installed.")
install_rnaturalearthdata()
} else if (
utils::packageVersion("rnaturalearthdata") < rnaturalearthdata_version) {
utils::packageVersion("rnaturalearthdata") < rnaturalearthdata_version
) {
message("The rnaturalearthdata package needs to be updated.")
install_rnaturalearthdata()
}
Expand All @@ -34,21 +35,24 @@ install_rnaturalearthdata <- function() {

# 23/2/17 changed to try install if not interactive to avoid winbuilder
# warning
input <- 1
input <- 1L
if (interactive()) {
input <- utils::menu(c("Yes", "No"),
input <- utils::menu(
c("Yes", "No"),
title = "Install the rnaturalearthdata package?"
)
}

if (input == 1) {
if (input == 1L) {
message("Installing the rnaturalearthdata package.")
tryCatch(
utils::install.packages("rnaturalearthdata",
utils::install.packages(
"rnaturalearthdata",
repos = c("http://packages.ropensci.org", "http://cran.rstudio.com"),
type = "source"
),
error = error_func, warning = error_func
error = error_func,
warning = error_func
)
} else {
stop(paste(
Expand Down
13 changes: 8 additions & 5 deletions R/install-rnaturalearthhires.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ check_rnaturalearthhires <- function() {
message("The rnaturalearthhires package needs to be installed.")
install_rnaturalearthhires()
} else if (
utils::packageVersion("rnaturalearthhires") < rnaturalearthhires_version) {
utils::packageVersion("rnaturalearthhires") < rnaturalearthhires_version
) {
message("The rnaturalearthhires package needs to be updated.")
install_rnaturalearthhires()
}
Expand All @@ -33,18 +34,20 @@ install_rnaturalearthhires <- function() {

# 23/2/17 changed to try install if not interactive to avoid winbuilder
# warning
input <- 1
input <- 1L
if (interactive()) {
input <- utils::menu(c("Yes", "No"),
input <- utils::menu(
c("Yes", "No"),
title = "Install the rnaturalearthhires package?"
)
}

if (input == 1) {
if (input == 1L) {
message("Installing the rnaturalearthhires package.")
tryCatch(
devtools::install_github("ropensci/rnaturalearthhires"),
error = error_func, warning = error_func
error = error_func,
warning = error_func
)
} else {
stop(paste(
Expand Down
11 changes: 5 additions & 6 deletions R/ne_coastline.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
#' }
#'
#' @export
ne_coastline <- function(scale = 110,
returnclass = c("sf", "sv")) {
ne_coastline <- function(scale = 110L, returnclass = c("sf", "sv")) {
returnclass <- match.arg(returnclass)

if (returnclass == "sp") {
deprecate_sp("ne_download(returnclass = 'sp')")
}

# check for the data packages and try to install if not there
if (scale == 10) {
if (scale == 10L) {
check_rnaturalearthhires()
} else {
check_rnaturalearthdata()
Expand All @@ -32,11 +31,11 @@ ne_coastline <- function(scale = 110,
# choose which map based on scale
sldf <- NULL

if (scale == 110) {
if (scale == 110L) {
sldf <- rnaturalearthdata::coastline110
} else if (scale == 50) {
} else if (scale == 50L) {
sldf <- rnaturalearthdata::coastline50
} else if (scale == 10) {
} else if (scale == 10L) {
sldf <- rnaturalearthhires::coastline10
}

Expand Down
26 changes: 14 additions & 12 deletions R/ne_countries.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@
#'
#' @export
ne_countries <- function(
scale = 110,
type = "countries",
continent = NULL,
country = NULL,
geounit = NULL,
sovereignty = NULL,
returnclass = c("sf", "sv")) {
scale = 110L,
type = "countries",
continent = NULL,
country = NULL,
geounit = NULL,
sovereignty = NULL,
returnclass = c("sf", "sv")
) {
returnclass <- match.arg(returnclass)
if (returnclass == "sp") {
deprecate_sp("ne_download(returnclass = 'sp')")
Expand All @@ -60,7 +61,7 @@ ne_countries <- function(
if (!is.null(continent)) {
filter <- tolower(spat_object$continent) %in% tolower(continent)

if (sum(filter) == 0) {
if (sum(filter) == 0L) {
stop("No such continent (", continent, ") in the data")
}
}
Expand All @@ -71,7 +72,7 @@ ne_countries <- function(
filter_country <- tolower(spat_object$admin) %in% tolower(country)
filter <- filter & filter_country

if (sum(filter_country) == 0) {
if (sum(filter_country) == 0L) {
stop("No such country (", country, ") in the data")
}
}
Expand All @@ -81,17 +82,18 @@ ne_countries <- function(
filter_geounit <- tolower(spat_object$geounit) %in% tolower(geounit)
filter <- filter & filter_geounit

if (sum(filter_geounit) == 0) {
if (sum(filter_geounit) == 0L) {
stop("No such geounit (", geounit, ") in the data")
}
}

# filter by sovereignty (BEWARE its called sovereignt in ne)
if (!is.null(sovereignty)) {
filter_sovereignty <- tolower(spat_object$sovereignt) %in% tolower(sovereignty)
filter_sovereignty <- tolower(spat_object$sovereignt) %in%
tolower(sovereignty)
filter <- filter & filter_sovereignty

if (sum(filter_sovereignty) == 0) {
if (sum(filter_sovereignty) == 0L) {
stop("No such sovereignty (", sovereignty, ") in the data")
}
}
Expand Down
26 changes: 17 additions & 9 deletions R/ne_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@
#'
#' @export
ne_download <- function(
scale = 110,
type = "countries",
category = c("cultural", "physical", "raster"),
destdir = tempdir(),
load = TRUE,
returnclass = c("sf", "sv")) {
scale = 110L,
type = "countries",
category = c("cultural", "physical", "raster"),
destdir = tempdir(),
load = TRUE,
returnclass = c("sf", "sv")
) {
category <- match.arg(category)
returnclass <- match.arg(returnclass)

Expand All @@ -85,12 +86,18 @@ ne_download <- function(

# without extension, e.g. .shp
file_name <- ne_file_name(
scale = scale, type = type, category = category, full_url = FALSE
scale = scale,
type = type,
category = category,
full_url = FALSE
)

# full url including .zip
address <- ne_file_name(
scale = scale, type = type, category = category, full_url = TRUE
scale = scale,
type = type,
category = category,
full_url = TRUE
)

# download zip to temporary location, unzipped files are saved later
Expand Down Expand Up @@ -129,7 +136,8 @@ ne_download <- function(
)
return(spatial_object)
} else {
file_name <- switch(category,
file_name <- switch(
category,
"raster" = file.path(destdir, file_name, paste0(file_name, ".tif")),
file.path(destdir, paste0(file_name, ".shp"))
)
Expand Down
Loading

0 comments on commit f035e21

Please sign in to comment.