Skip to content

Commit

Permalink
Merge pull request #60 from emlab-ucsb/dev-misc
Browse files Browse the repository at this point in the history
Dev misc
  • Loading branch information
jflowernet authored Jan 16, 2025
2 parents 6dd763a + 340eeba commit 9a3060b
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 106 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(get_gfw)
export(get_grid)
export(get_knolls)
export(get_seamounts)
export(remove_empty_layers)
importFrom(graphics,legend)
importFrom(graphics,par)
importFrom(magrittr,"%>%")
Expand Down
2 changes: 1 addition & 1 deletion R/get_features.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ get_features <- function(spatial_grid = NULL, raw = FALSE, features = c("bathyme
if("enviro_zones" %in% features) {
message("Getting environmental zones data... This could take several minutes")
suppressMessages({
enviro_zones <- get_enviro_zones(spatial_grid = spatial_grid, raw = raw, show_plots = FALSE, num_clusters = enviro_clusters, max_num_clusters = max_enviro_clusters, antimeridian = antimeridian)
enviro_zones <- get_enviro_zones(spatial_grid = spatial_grid, raw = raw, enviro_zones = !raw, show_plots = FALSE, num_clusters = enviro_clusters, max_num_clusters = max_enviro_clusters, antimeridian = antimeridian)
})
}

Expand Down
30 changes: 20 additions & 10 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ classify_layers <- function(dat, dat_breaks = NULL, classification_names = NULL)
dplyr::select(3:ncol(.), 2) #put classification before geometry and drop original values
}
}
#' Get an sf polygons in lonlat (EPSG 4326) from terra or sf input object
#'
#' Get an sf polygon in lonlat (EPSG 4326) from terra or sf input object
#'
#' @param spatial_grid
#'
#' @return `sf` polygons
Expand Down Expand Up @@ -169,20 +169,30 @@ polygon_in_4326 <-
}
}

#' Remove empty layers in raster or columns with all zeroes in sf
#' Remove empty layers in spatial object
#'
#' @param dat `sf` or raster object
#' @description
#' Removes any layers (`terra::rast` object) or columns (`sf` object) that are all zero or NA
#'
#' @param dat `sf` or `terra::rast` object
#'
#' @return `sf` or raster depending on input
#' @return `sf` or `terra::rast` depending on input
#'
#' @noRd
#' @export
remove_empty_layers <- function(dat){
if(check_sf(dat)){
dat %>%
dplyr::select(which(!colSums(sf::st_drop_geometry(dat), na.rm = TRUE) %in% 0))
column_sums <- colSums(sf::st_drop_geometry(dat), na.rm = TRUE)

if(sum(column_sums) == 0){
stop("Only NAs and/ or zeroes in sf object")
}else dplyr::select(dat, which(!column_sums %in% 0))

}else{
dat %>%
terra::subset(which(terra::global(dat, "sum", na.rm = TRUE) >0))
index_true_false <- (terra::global(dat, "sum", na.rm = TRUE) >0)

if(all(index_true_false %in% c(NA, FALSE))) {
stop("Only NAs and/ or zeroes in raster")
} else terra::subset(dat, which(index_true_false))
}
}

Expand Down
17 changes: 17 additions & 0 deletions man/remove_empty_layers.Rd

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

17 changes: 5 additions & 12 deletions tests/testthat/test-get_coral_habitat.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,21 @@ test_that("returns raw data for Bermuda", {
# })

test_that("returns Bermuda gridded data - raster", {
expect_s4_class(get_coral_habitat(spatial_grid = get_grid(boundary = get_boundary(name = "Bermuda"),
crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 10000)), class = "SpatRaster")
expect_s4_class(get_coral_habitat(spatial_grid = get_bermuda_grid()), class = "SpatRaster")
})

test_that("returns gridded data for Kiribati - raster", {
expect_s4_class(get_coral_habitat(spatial_grid = get_grid(boundary = get_boundary(name = "Kiribati", country_type = "sovereign"),
crs = '+proj=laea +lon_0=-159.609375 +lat_0=0 +datum=WGS84 +units=m +no_defs',
resolution = 50000)),
expect_s4_class(get_coral_habitat(spatial_grid = get_kiribati_grid()),
class = "SpatRaster")
})

test_that("returns gridded data for Bermuda - sf", {
expect_s3_class(get_coral_habitat(spatial_grid = get_grid(boundary = get_boundary(name = "Bermuda"),
crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 10000,
output = "sf_square")), class = "sf")
expect_s3_class(get_coral_habitat(spatial_grid = get_bermuda_grid(output = "sf_square")),
class = "sf")
})

test_that("returns extra columns as well as coral data for sf grid", {
expect_equal(get_boundary(name = "Bermuda") |>
get_grid(resolution = 10000, crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs', output = "sf_square") |>
expect_equal(get_bermuda_grid(output = "sf_square") |>
dplyr::mutate(extracol1 = 1, extracol2 = 2, .before = 1) |>
get_coral_habitat() |>
ncol(), 6)
Expand Down
14 changes: 3 additions & 11 deletions tests/testthat/test-get_dist.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
test_that("returns gridded Bermuda distance to shore - raster", {
expect_s4_class(suppressWarnings(get_dist(spatial_grid = get_grid(boundary = get_boundary(name = "Bermuda"),
crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 10000))),
expect_s4_class(suppressWarnings(get_dist(spatial_grid = get_bermuda_grid())),
class = "SpatRaster")
})

test_that("returns gridded distance to shore for Kiribati - sf", {
expect_s3_class(suppressWarnings(get_dist(spatial_grid = get_grid(boundary = get_boundary(name = "Kiribati", country_type = "sovereign"),
crs = '+proj=laea +lon_0=-159.609375 +lat_0=0 +datum=WGS84 +units=m +no_defs',
resolution = 50000,
output = "sf_hex"))),
expect_s3_class(suppressWarnings(get_dist(spatial_grid = get_kiribati_grid(output = "sf_hex"))),
class = "sf")
})

test_that("returns gridded distance to port for Kiribati - raster", {
expect_s3_class(suppressWarnings(get_dist(spatial_grid = get_grid(boundary = get_boundary(name = "Kiribati", country_type = "sovereign"),
crs = '+proj=laea +lon_0=-159.609375 +lat_0=0 +datum=WGS84 +units=m +no_defs',
resolution = 50000,
output = "sf_hex"), dist_to = "ports")),
expect_s3_class(suppressWarnings(get_dist(spatial_grid = get_kiribati_grid(output = "sf_hex"), dist_to = "ports")),
class = "sf")
})

Expand Down
18 changes: 4 additions & 14 deletions tests/testthat/test-get_ecoregion.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,20 @@ test_that("returns 6 ecoregions for Kiribati - sf", {
})

test_that("returns Bermuda gridded data - raster", {
expect_s4_class(get_ecoregion(spatial_grid = get_grid(boundary = get_boundary(name = "Bermuda"),
crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 10000,
output = "raster"), type = "MEOW"), class = "SpatRaster")
expect_s4_class(get_ecoregion(spatial_grid = get_bermuda_grid(), type = "MEOW"), class = "SpatRaster")
})

test_that("returns Bermuda gridded data for no data - raster", {
expect_s4_class(get_ecoregion(spatial_grid = get_grid(boundary = get_boundary(name = "Bermuda"),
crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 10000,
output = "raster"), type = "LME"), class = "SpatRaster")
expect_s4_class(get_ecoregion(spatial_grid = get_bermuda_grid(), type = "LME"), class = "SpatRaster")
})

test_that("returns gridded data for Kiribati - sf", {
expect_s3_class(get_ecoregion(spatial_grid = get_grid(boundary = get_boundary(name = "Kiribati", country_type = "sovereign"),
crs = '+proj=laea +lon_0=-159.609375 +lat_0=0 +datum=WGS84 +units=m +no_defs',
resolution = 50000,
output = "sf_square"), type = "Longhurst"),
expect_s3_class(get_ecoregion(spatial_grid = get_kiribati_grid(output = "sf_square"), type = "Longhurst"),
class = "sf")
})

test_that("returns extra columns as well as empty data for sf grid", {
expect_equal(get_boundary(name = "Bermuda") |>
get_grid(resolution = 10000, crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs', output = "sf_square") |>
expect_equal(get_bermuda_grid(output = "sf_square") |>
dplyr::mutate(extracol1 = 1, extracol2 = 2, .before = 1) |>
get_ecoregion(type = "LME") |>
ncol(), 4)
Expand Down
24 changes: 5 additions & 19 deletions tests/testthat/test-get_enviro_zones.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,29 @@ test_that("returns raw Bio-Oracle data - 11 layer raster", {

test_that("returns gridded Bermuda Bio-Oracle data - raster", {
set.seed(500)
expect_s4_class(get_boundary(name = "Bermuda") |>
get_grid(crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 20000)|>
expect_s4_class(get_bermuda_grid()|>
get_enviro_zones(raw = FALSE, enviro_zones = FALSE), class = "SpatRaster")
})

test_that("returns gridded Bermuda Bio-Oracle data - sf", {
set.seed(500)
expect_s3_class(get_boundary(name = "Bermuda") |>
get_grid(crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 20000,
output = "sf_square")|>
expect_s3_class(get_bermuda_grid(output = "sf_square")|>
get_enviro_zones(raw = FALSE, enviro_zones = FALSE), class = "sf")
})

test_that("returns gridded Bermuda envirozones - raster", {
expect_s4_class(get_boundary(name = "Bermuda") |>
get_grid(crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 20000,
output = "raster")|>
expect_s4_class(get_bermuda_grid()|>
get_enviro_zones(raw = FALSE, enviro_zones = TRUE, num_clusters = 3), class = "SpatRaster")
})

test_that("returns gridded Kiribati envirozones - sf", {
expect_s3_class(get_boundary(name = "Kiribati", country_type = "sovereign") |>
get_grid(crs = '+proj=laea +lon_0=-159.609375 +lat_0=0 +datum=WGS84 +units=m +no_defs',
resolution = 50000,
output = "sf_square") |>
expect_s3_class(get_kiribati_grid(output = "sf_square") |>
get_enviro_zones(raw = FALSE, enviro_zones = TRUE, num_clusters = 3), class = "sf")
})

test_that("returns gridded Bermuda envirozones data with extra input columns - sf", {
set.seed(500)
expect_equal(get_boundary(name = "Bermuda") |>
get_grid(crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 20000,
output = "sf_square") |>
expect_equal(get_bermuda_grid(output = "sf_square") |>
dplyr::mutate(extracol1 = 1, extracol2 = 2, .before = 1) |>
get_enviro_zones(raw = FALSE, enviro_zones = TRUE, show_plots = TRUE) |>
ncol(), 6)
Expand Down
11 changes: 3 additions & 8 deletions tests/testthat/test-get_features.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@ test_that("returns raw Bermuda data as list", {

test_that("returns gridded Bermuda features - raster", {
set.seed(500)
expect_s4_class(suppressWarnings(get_features(spatial_grid = get_grid(boundary = get_boundary(name = "Bermuda"),
crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 20000))),
expect_s4_class(suppressWarnings(get_features(spatial_grid = get_bermuda_grid())),
class = "SpatRaster")
})

test_that("returns gridded Kiribati features - sf with extra cols", {
set.seed(1234)
expect_equal(suppressWarnings(get_boundary(name = "Kiribati", country_type = "sovereign") |>
get_grid(crs = '+proj=laea +lon_0=-159.609375 +lat_0=0 +datum=WGS84 +units=m +no_defs',
resolution = 50000,
output = "sf_square") |>
expect_equal(suppressWarnings(get_kiribati_grid(output = "sf_square") |>
dplyr::mutate(extracol1 = 1, extracol2 = 2, .before = 1) |>
get_features(antimeridian = TRUE) |>
ncol()),
39)
41)
})
13 changes: 3 additions & 10 deletions tests/testthat/test-get_geomorphology.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ test_that("return raw data for Bermuda - sf", {
})

test_that("return gridded data for Bermuda - raster", {
expect_s4_class(get_geomorphology(spatial_grid = get_grid(boundary = get_boundary(name = "Bermuda"),
crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 20000)),
expect_s4_class(get_geomorphology(spatial_grid = get_bermuda_grid()),
class = "SpatRaster")
})

test_that("return gridded data for Bermuda - sf", {
expect_s3_class(get_geomorphology(spatial_grid = get_grid(boundary = get_boundary(name = "Bermuda"),
crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 20000,
output = "sf_hex")),
expect_s3_class(get_geomorphology(spatial_grid = get_bermuda_grid(output = "sf_hex")),
class = "sf")
})

Expand All @@ -24,8 +19,6 @@ test_that("return raw data for Kiribati - sf", {
})

test_that("return gridded data for Kiribati - raster", {
expect_s4_class(get_geomorphology(spatial_grid = get_grid(get_boundary(name = "Kiribati", country_type = "sovereign"),
crs = '+proj=laea +lon_0=-159.609375 +lat_0=0 +datum=WGS84 +units=m +no_defs',
resolution = 50000)),
expect_s4_class(get_geomorphology(spatial_grid = get_kiribati_grid()),
class = "SpatRaster")
})
9 changes: 2 additions & 7 deletions tests/testthat/test-get_knolls.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ test_that("returns raw knolls data for Bermuda - sf", {
})

test_that("returns gridded Bermuda data - raster", {
expect_s4_class(suppressWarnings(get_knolls(spatial_grid = get_grid(boundary = get_boundary(name = "Bermuda"),
crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 10000))),
expect_s4_class(suppressWarnings(get_knolls(spatial_grid = get_bermuda_grid())),
class = "SpatRaster")
})

Expand All @@ -18,10 +16,7 @@ test_that("returns raw data for Kiribati - sf", {
})

test_that("returns gridded data for Kiribati - sf", {
expect_s3_class(suppressWarnings(get_knolls(spatial_grid = get_grid(boundary = get_boundary(name = "Kiribati", country_type = "sovereign"),
crs = '+proj=laea +lon_0=-159.609375 +lat_0=0 +datum=WGS84 +units=m +no_defs',
resolution = 50000,
output = "sf_hex"),
expect_s3_class(suppressWarnings(get_knolls(spatial_grid = get_kiribati_grid(output = "sf_hex"),
antimeridian = TRUE)),
class = "sf")
})
18 changes: 4 additions & 14 deletions tests/testthat/test-get_seamounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,29 @@ test_that("returns Kiribati raw peaks data buffered - sf", {
})

test_that("returns buffered gridded Bermuda seamounts - raster", {
expect_s4_class(suppressWarnings(get_seamounts(spatial_grid = get_grid(boundary = get_boundary(name = "Bermuda"),
crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 20000),
expect_s4_class(suppressWarnings(get_seamounts(spatial_grid = get_bermuda_grid(),
raw = FALSE,
buffer = 30000)),
class = "SpatRaster")
})

test_that("returns buffered gridded Kiribati seamounts - raster", {
expect_s4_class(suppressWarnings(get_seamounts(spatial_grid = get_grid(boundary = get_boundary(name = "Kiribati", country_type = "sovereign"),
crs = '+proj=laea +lon_0=-159.609375 +lat_0=0 +datum=WGS84 +units=m +no_defs',
resolution = 50000),
expect_s4_class(suppressWarnings(get_seamounts(spatial_grid = get_kiribati_grid(),
raw = FALSE,
buffer = 30000,
antimeridian = TRUE)),
class = "SpatRaster")
})

test_that("returns buffered gridded Bermuda seamounts - sf", {
expect_s3_class(suppressWarnings(get_seamounts(spatial_grid = get_grid(boundary = get_boundary(name = "Bermuda"),
crs = '+proj=laea +lon_0=-64.8108333 +lat_0=32.3571917 +datum=WGS84 +units=m +no_defs',
resolution = 20000,
output = "sf_square"),
expect_s3_class(suppressWarnings(get_seamounts(spatial_grid = get_bermuda_grid(output = "sf_square"),
raw = FALSE,
buffer = 30000)),
class = "sf")
})

test_that("returns buffered gridded Kiribati seamounts - sf", {
expect_s3_class(suppressWarnings(get_seamounts(spatial_grid = get_grid(boundary = get_boundary(name = "Kiribati", country_type = "sovereign"),
crs = '+proj=laea +lon_0=-159.609375 +lat_0=0 +datum=WGS84 +units=m +no_defs',
resolution = 50000,
output = "sf_hex"),
expect_s3_class(suppressWarnings(get_seamounts(spatial_grid = get_kiribati_grid(output = "sf_hex"),
raw = FALSE,
buffer = 30000,
antimeridian = TRUE)),
Expand Down

0 comments on commit 9a3060b

Please sign in to comment.