Skip to content

Commit

Permalink
fix warnings related to SVF
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep committed Nov 24, 2023
1 parent e95ad91 commit 84dd7df
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 14 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ Imports:
dplyr (>= 1.0.6),
magrittr (>= 2.0.1),
stars (>= 0.5),
rlang (>= 1.0)
rlang (>= 1.0),
Depends:
R (>= 2.10)
Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0)
withr (>= 2.5.1)
Config/testthat/edition: 3
VignetteBuilder: knitr
URL: https://github.com/icra/ediblecity,
https://icra.github.io/ediblecity/
BugReports: https://github.com/icra/ediblecity/issues
Config/potools/style: explicit
2 changes: 1 addition & 1 deletion R/edible_utils.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
check_sf <- function(x){
#check if x is a sf object
if (all(class(x) != "sf")) rlang::abort("x must be an `sf` object")
if (all(class(x) != "sf")) rlang::abort(tr_("x must be an `sf` object"))
}
2 changes: 1 addition & 1 deletion R/indicator_NO2.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ no2_seq <- function(x,
}

if(!(all(c("land_uses", "no2_seq1", "no2_seq2") %in% colnames(green_df))))
rlang::abort("green_df must contain the columns 'land_uses', 'no2_seq1' & 'no2_seq2'. See ?no2_seq for details")
rlang::abort(tr_("green_df must contain the columns 'land_uses', 'no2_seq1' & 'no2_seq2'. See ?no2_seq for details"))

x_f <- x %>% filter(land_use %in% green_df$land_uses)

Expand Down
2 changes: 1 addition & 1 deletion R/indicator_food.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ food_production <- function(x,
dplyr::select(land_uses, food1, food2)
} else {
if (!all(c("land_uses", "food1", "food2") %in% colnames(edible_df)))
rlang::abort("edible_df must have the columns 'land_uses', 'food1' and 'food2'. See `?food_production` for details.")
rlang::abort(tr_("edible_df must have the columns 'land_uses', 'food1' and 'food2'. See `?food_production` for details."))
}

#filter x based on edible
Expand Down
4 changes: 2 additions & 2 deletions R/indicator_green_capita.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ green_capita <- function(

check_sf(x)

if(all(is.null(inh_col), is.null(inhabitants))) rlang::abort("'inhabitants' or 'inh_col' must be provided.")
if(!is.null(inh_col) && is.null(name_col)) rlang::abort("'name_col' must be provided along with 'inh_col'")
if(all(is.null(inh_col), is.null(inhabitants))) rlang::abort(tr_("'inhabitants' or 'inh_col' must be provided."))
if(!is.null(inh_col) && is.null(name_col)) rlang::abort(tr_("'name_col' must be provided along with 'inh_col'"))

if (is.null(green_categories)){

Expand Down
4 changes: 2 additions & 2 deletions R/indicator_green_distance.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ green_distance <- function(x,
green_areas <- green_areas %>%
dplyr::filter(area >= min_area)

if(nrow(green_areas) == 0) rlang::warn("No public green areas larger than 'min_area' in 'x'. Returning 'NAs'")
if(nrow(green_areas) == 0) rlang::warn(tr_("No public green areas larger than 'min_area' in 'x'. Returning 'NAs'"))

houses <- x %>%
dplyr::filter(!!as.symbol(residence_col) %in% residences)

if(nrow(houses) == 0) rlang::abort("No residences found in 'x'")
if(nrow(houses) == 0) rlang::abort(tr_("No residences found in 'x'"))

nearest <- sf::st_nearest_feature(houses,green_areas)
distance <- sf::st_distance(houses, green_areas[nearest,], by_element = TRUE)
Expand Down
4 changes: 2 additions & 2 deletions R/indicator_heat_island.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ UHI <- function(
. <- NULL

check_sf(x)
if (!("stars" %in% class(SVF))) rlang::abort("SVF must be an object of class 'stars'")
if (!("stars" %in% class(SVF))) rlang::abort(tr_("SVF must be an object of class 'stars'"))

city_land_uses <- city_land_uses %>%
mutate(pGreen = ifelse(!is.na(pGreen),
Expand All @@ -71,7 +71,7 @@ UHI <- function(

# Reproject SVF if necessary
if(sf::st_crs(x_rast) != sf::st_crs(SVF)){
warning("Reprojecting SVF to ", sf::st_crs(x_rast)[[1]])
rlang::warn(tr_("Reprojecting SVF to ", sf::st_crs(x_rast)[[1]]))
SVF <- sf::st_transform(SVF, sf::st_crs(x_rast))
}

Expand Down
2 changes: 1 addition & 1 deletion R/set_scenario.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ set_scenario <- function(x,
check_sf(x)

#check if land_use col exists
if (!("land_use" %in% colnames(x))) rlang::abort("x needs a column called land_use, see ?set_scenario for more detail")
if (!("land_use" %in% colnames(x))) rlang::abort(tr_("x needs a column called land_use, see ?set_scenario for more detail"))

#if area_field is null, calculates de area of each feature
if (is.null(area_field)) {
Expand Down
6 changes: 3 additions & 3 deletions R/set_scenario_rationale.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ set_scenario_rationale <- function(x,
x$land_use[gardens_index] <- city_land_uses$land_uses[city_land_uses$location == "garden"]

if (nGardens*pGardens >= length(gardens_index)){
warning(paste("Only", length(gardens_index), "private gardens out of", nGardens*pGardens, "assumed satisfy the 'min_area_garden'\n"))
rlang::warn(tr_(paste("Only", length(gardens_index), "private gardens out of", nGardens*pGardens, "assumed satisfy the 'min_area_garden'\n")))
}

} else if (pGardens < 1){
Expand Down Expand Up @@ -72,7 +72,7 @@ set_scenario_rationale <- function(x,


if (length(vacant_index) < nVacant*pVacant){
warning(paste("Only", length(vacant_index), "vacant plots out of", nVacant*pVacant, "assumed satisfy the 'min_area_vacant'\n"))
rlang::warn(tr_(paste("Only", length(vacant_index), "vacant plots out of", nVacant*pVacant, "assumed satisfy the 'min_area_vacant'\n")))
nVacant <- length(vacant_index)

} else {
Expand Down Expand Up @@ -120,7 +120,7 @@ set_scenario_rationale <- function(x,
hydroponic_rooftop <- city_land_uses$land_uses[city_land_uses$jobs & city_land_uses$location == 'rooftop']

if (length(rooftop_index) < nRooftop*pRooftop){
warning(paste("Only", length(rooftop_index), "rooftops out of", nRooftop*pRooftop, "assumed satisfy the 'min_area_rooftop'\n"))
rlang::warn(tr_(paste("Only", length(rooftop_index), "rooftops out of", nRooftop*pRooftop, "assumed satisfy the 'min_area_rooftop'\n")))
nRooftop <- length(rooftop_index)

} else {
Expand Down
3 changes: 3 additions & 0 deletions R/utils-potools.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tr_ <- function(...){
enc2utf8(gettext(paste0(...), domain = "R-pkg"))
}
Binary file modified data/SVF.rda
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/testthat/test-indicator_heat_island.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
withr::local_envvar(new = c("GTIFF_SRS_SOURCE" = "ESPG"))

test_that("when return_raster an verbose are False, it returns a summary", {
UHI(city_example, SVF) %>%
expect_s3_class("summaryDefault")
Expand Down
2 changes: 2 additions & 0 deletions vignettes/ediblecity.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ Likewise, all the parameters used by the indicators are defined in `city_land_us
The urban heat island indicator can return a summary of values or a `stars` object. It needs a raster representing the Sky view factor. See `?UHI` for more details. We use the `SVF` object provided with the package.

```{r}
withr::local_envvar(new = c("GTIFF_SRS_SOURCE" = "ESPG")) # To avoid CRS warning
UHI(scenario, SVF)
```

```{r}
withr::local_envvar(new = c("GTIFF_SRS_SOURCE" = "ESPG")) # To avoid CRS warning
plot(UHI(scenario, SVF, return_raster = TRUE))
```
Expand Down

0 comments on commit 84dd7df

Please sign in to comment.