Skip to content

Commit

Permalink
Merge pull request #16 from emlab-ucsb/dev
Browse files Browse the repository at this point in the history
better checks in sf to sf grid for no intersected data
  • Loading branch information
jflowernet authored Mar 28, 2024
2 parents 08928f9 + efaed29 commit 8991bac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
45 changes: 27 additions & 18 deletions R/sf_to_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ sf_to_grid <- function(dat, spatial_grid, matching_crs, name, feature_names, ant
sf::st_transform(sf::st_crs(spatial_grid))
}

if(nrow(dat_cropped) == 0) stop("No data in grid.")

if(is.null(feature_names)){
if(is.null(name)) name <- "data"

Expand Down Expand Up @@ -81,24 +83,31 @@ sf_to_grid <- function(dat, spatial_grid, matching_crs, name, feature_names, ant

dat_list <- if(is.null(feature_names)) list(dat_grouped) %>% setNames(layer_names) else split(dat_grouped, layer_names)

intersected_data_list <- suppressWarnings(
lapply(layer_names, function(x) dat_list[[x]] %>%
sf::st_intersection(spatial_grid_with_id, .) %>%
dplyr::mutate(area = as.numeric(sf::st_area(.))) %>%
sf::st_drop_geometry(.) %>%
dplyr::full_join(spatial_grid_with_area, ., by = c("cellID")) %>%
dplyr::mutate(perc_area = .data$area / .data$area_cell, .keep = "unused", .before = 1) %>%
dplyr::mutate(perc_area = dplyr::case_when(is.na(.data$perc_area) ~ 0,
.default = as.numeric(.data$perc_area))) %>%
dplyr::left_join(spatial_grid_with_id, ., by = "cellID") %>%
{if(!apply_cutoff) dplyr::select(., .data$perc_area, {{x}} := .data$perc_area) else {
dplyr::mutate(.,
{{x}} := dplyr::case_when(.data$perc_area >= cutoff ~ 1,
.default = 0)
) %>%
dplyr::select({{x}})
}})
)
intersected_data_list <- list()

for (layer in layer_names) {
temp_intersection <- sf::st_intersection(spatial_grid_with_id, dat_list[[layer]])

if(nrow(temp_intersection)>0) {
intersected_data_list[[layer]] <- temp_intersection %>%
dplyr::mutate(area = as.numeric(sf::st_area(.))) %>%
sf::st_drop_geometry(.) %>%
dplyr::full_join(spatial_grid_with_area, ., by = c("cellID")) %>%
dplyr::mutate(perc_area = .data$area / .data$area_cell, .keep = "unused", .before = 1) %>%
dplyr::mutate(perc_area = dplyr::case_when(is.na(.data$perc_area) ~ 0,
.default = as.numeric(.data$perc_area))) %>%
dplyr::left_join(spatial_grid_with_id, ., by = "cellID") %>%
{if(!apply_cutoff) dplyr::select(., .data$perc_area, {{layer}} := .data$perc_area) else {
dplyr::mutate(.,
{{layer}} := dplyr::case_when(.data$perc_area >= cutoff ~ 1,
.default = 0)
) %>%
dplyr::select({{layer}})
}}
}
}

if(length(intersected_data_list) == 0) stop("No data in grid")

lapply(intersected_data_list, function(x) sf::st_drop_geometry(x) %>% dplyr::select(dplyr::where(~any(. != 0)))) %>%
do.call(cbind, .) %>%
Expand Down
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,6 @@ kir_abyssal_plains <- get_data_in_grid(spatial_grid = kir_grid, dat = abyssal_pl
#> Warning: attribute variables are assumed to be spatially constant throughout
#> all geometries

#> Warning: attribute variables are assumed to be spatially constant throughout
#> all geometries

#> Warning: attribute variables are assumed to be spatially constant throughout
#> all geometries

#> Warning: attribute variables are assumed to be spatially constant throughout
#> all geometries

#plot
plot(kir_abyssal_plains, border = FALSE)
```
Expand Down
Binary file modified man/figures/README-grid_sf_data-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8991bac

Please sign in to comment.