Skip to content

Commit

Permalink
Fix return of 0-length tibbles for TEOW (#199)
Browse files Browse the repository at this point in the history
* added 0-row check in .bind_assets

* return NA for non-intersection result with TEOW

* update NEWS
  • Loading branch information
goergen95 authored Nov 10, 2023
1 parent 59a4c93 commit 87b99ba
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@

- Quickstart vignette now uses the ESA Landcover resource
as an example for how to use the package (#201).

## Bug Fixes

- `biome` and `ecoregions` now properly handle 0-length tibbles (#196)

## New features

- GFW resources and indicators now include latest GFC-2022-v1.10 version (#204).


## Internal

- `calc_indicators()` now includes a check for 0-length tibbles (#199)


# mapme.biodiversity 0.4.0

## New features
Expand Down
15 changes: 11 additions & 4 deletions R/calc_biome.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,31 @@ NULL
biomes <- NULL
new_area <- NULL
area <- NULL

if (nrow(teow[[1]]) == 0) {
return(NA)
}

merged <- .comp_teow(
x = x,
teow = teow,
verbose = verbose
)
verbose = verbose)

if (nrow(merged) == 0) {
return(NA)
}

out <- merged %>%
dplyr::select(BIOME_NAME, new_area)

out_tibble <- tibble(
biomes = out[[1]],
area = out[[2]]
)
area = out[[2]])

results_biome <- out_tibble %>%
dplyr::group_by(biomes) %>%
dplyr::summarise(area = sum(as.numeric(area)))

results_biome
}

Expand Down
15 changes: 11 additions & 4 deletions R/calc_ecoregion.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,27 @@ NULL
new_area <- NULL
ecoregions <- NULL
area <- NULL

if (nrow(teow[[1]]) == 0) {
return(NA)
}

merged <- .comp_teow(
x = x,
teow = teow,
verbose = verbose
)
verbose = verbose)

if (nrow(merged) == 0) {
return(NA)
}

out <- merged %>%
dplyr::select(ECO_NAME, new_area)

out_tibble <- tibble(
ecoregions = out[[1]],
area = out[[2]]
)
area = out[[2]])

out_tibble %>%
dplyr::group_by(ecoregions) %>%
dplyr::summarise(area = sum(as.numeric(area)))
Expand Down
6 changes: 6 additions & 0 deletions R/calc_indicators.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ calc_indicators <- function(x, indicators, ...) {
.bind_assets <- function(results) {
# bind results to data.frame
index_tbl <- purrr::map_lgl(results, function(x) inherits(x, c("tbl_df", "data.frame")))
# check for 0 length tibbles
n_rows <- sapply(results[index_tbl], nrow)
if (any(n_rows == 0)) {
stop(paste("0-length tibbles returned for some assets.\n",
"Make sure the indicator function returns NA if it cannot be calculated for an asset."))
}

# case all assets returned tibbles
if (all(index_tbl)) {
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-calc_biome.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ test_that("biome computation works", {
18352.24,
tolerance = 1e-4
)
# check NA is returned for 0-length tibbles
st_geometry(shp) <- st_geometry(shp) + 5
st_crs(shp) <- st_crs(4326)
expect_equal(
.calc_biome(shp, teow),
NA)
})
7 changes: 7 additions & 0 deletions tests/testthat/test-calc_ecoregion.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ test_that("ecoregion computation works", {
18352.24,
tolerance = 1e-4
)

# check NA is returned for 0-length tibbles
st_geometry(shp) <- st_geometry(shp) + 5
st_crs(shp) <- st_crs(4326)
expect_equal(
.calc_biome(shp, teow),
NA)
})

0 comments on commit 87b99ba

Please sign in to comment.