Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/inbo/mbag-bodem
Browse files Browse the repository at this point in the history
  • Loading branch information
hansvancalster committed Dec 2, 2024
2 parents 9a454ee + 81501a4 commit 434acfb
Show file tree
Hide file tree
Showing 16 changed files with 894 additions and 603 deletions.
4 changes: 1 addition & 3 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
## creative commons

# Attribution 4.0 International
# Creative Commons Attribution 4.0 International

Creative Commons Corporation (“Creative Commons”) is not a law firm and does not provide legal services or legal advice. Distribution of Creative Commons public licenses does not create a lawyer-client or other relationship. Creative Commons makes its licenses and related information available on an “as-is” basis. Creative Commons gives no warranties regarding its licenses, any material licensed under their terms and conditions, or any related information. Creative Commons disclaims all liability for damages resulting from their use to the fullest extent possible.

Expand Down
2 changes: 2 additions & 0 deletions checklist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ spelling:
other:
en-GB:
- source/r/geocomputations.R
- source/rmarkdown/analyses_diversity/test_breakaway_diversity.Rmd
- source/rmarkdown/compositional_analysis/test_rademu.Rmd
6 changes: 6 additions & 0 deletions inst/en_gb.dic
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
β
Akker
Mangiola
modelmisspecification
poisson
sccomp
56 changes: 56 additions & 0 deletions inst/nl_be.dic
Original file line number Diff line number Diff line change
@@ -1,36 +1,92 @@
'by
+/-
-SNE
.Rdata
.txt
18S
18s
@example
AICc
Achaeta
Aitchinson
Annelida
Beta
Cmon
Dicranocentrus
Entomobryidae
Folsomia
GRTS
HPC
Henlea
Isotomidae
Isotomurus
Lumbricus
MBAG
Marionina
Megalothorax
Names
OTU
OTUs
Ordinaties
Protaphorura
Rarefaction
Scientific
WestEurope
archaea
arthropoda
ascomycota
basidiomycota
be
beta-binomiaal
bimodaliteit
bodembiodiversiteit
bodemkoolstofmeetnet
bodemstaal
bodemstaalnamelocaties
can
centered-log-ratio
chytridiomycota
collembola
collineaire
counts
covariaat
covariaten
credibiliteitsinterval
csv
design'
design-variabelen
diversity
eDNA
from
fysico-chemische
fysicochemische
gepaardheid
intercept
macro-invertebraten
meso-
metabarcoding
metazoa
mollusca
multivariaat
names
natuurgrasland
natuurgraslanden
nematoda
nematodenstalen
nolint
nulwaarnemingen
ordinatieruimte
ordinaties
phyloseq
phylum
platyhelminthes
presence
randomisatieprocedure
rarefaction
rarefy
read
reads
result
sub-sampled
tardigrada
which
29 changes: 16 additions & 13 deletions source/r/check_presence.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' Deze functie gaat na of een soort volgens GBIF voorkomt in een gekozen set
#' van landen
#' van landen # nolint start
#'
#' Default zijn West-Europese landen ingesteld: België, Frankrijk, Duitsland,
#' Luxemburg, Nederland, Zwitserland, Oostenrijk
Expand All @@ -10,7 +10,7 @@
#'
#' @example
#' result_WestEurope <- check_presence_from_file()
#' print(result_WestEurope)
#' print(result_WestEurope) # nolint end
check_presence <- function(
input = NULL,
countries = c("BE", "FR", "DE", "LU", "NL", "CH", "AT")) {
Expand All @@ -35,20 +35,23 @@ check_presence <- function(
for (country in countries) {
# Function to check occurrence data and handle errors
check_occurrence <- function(name) {
data <- tryCatch({
rgbif::occ_data(
scientificName = name,
country = country, # Specify one country at a time
limit = 1
)
}, error = function(e) {
return(list(error = TRUE))
})
data <- tryCatch(
{
rgbif::occ_data(
scientificName = name,
country = country, # Specify one country at a time
limit = 1
)
},
error = function(e) {
return(list(error = TRUE))
}
)

if (inherits(data, "error") || is.null(data$data)) {
return(FALSE) # Occurrence data is not present or error occurred
return(FALSE) # Occurrence data is not present or error occurred
} else {
return(TRUE) # Occurrence data is present
return(TRUE) # Occurrence data is present
}
}

Expand Down
64 changes: 35 additions & 29 deletions source/r/geocomputations.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
#'
#' @examples
point_to_gridcell <- function(
xy,
cell_width_m = 500,
point_position = c("center", "lowerleft", "upperleft", "lowerright",
"upperright"),
crs = 31370) {
xy,
cell_width_m = 500,
point_position = c(
"center", "lowerleft", "upperleft", "lowerright",
"upperright"
),
crs = 31370) {
point_position <- match.arg(point_position)

if (point_position != "center") stop(point_position, " not yet implemented")
Expand All @@ -24,13 +26,15 @@ point_to_gridcell <- function(
xy <- sf::st_geometry(xy)

# buffer with 1 point per quandrant
xy_buffer <- sf::st_buffer(x = xy,
dist = cell_width_m / 2,
nQuadSegs = 1)
xy_buffer <- sf::st_buffer(
x = xy,
dist = cell_width_m / 2,
nQuadSegs = 1
)

# rotate 45 degrees around centroid
rot <- function(a) matrix(c(cos(a), sin(a), -sin(a), cos(a)), 2, 2)
pl <- (xy_buffer - xy) * rot(pi/4) + xy
pl <- (xy_buffer - xy) * rot(pi / 4) + xy
pl <- sf::st_sf(data.frame(xy_df, pl), crs = crs)
return(pl)
}
Expand All @@ -51,23 +55,24 @@ point_to_gridcell <- function(
#'
#' @examples
landusemetrics_grid_cell <- function(
grid_cell,
layer,
grid_group_by_col = "POINT_ID",
layer_group_by_col = "",
progress = FALSE
) {
grid_cell,
layer,
grid_group_by_col = "POINT_ID",
layer_group_by_col = "",
progress = FALSE) {
require(duckdb)
if (inherits(layer, "SpatRaster") | inherits(layer, "RasterLayer")) {
require(dplyr)
if (inherits(layer, "SpatRaster") || inherits(layer, "RasterLayer")) {
crs_grid <- gsub("^((.*?),\\n\\s*?){2}", "", sf::st_crs(grid_cell)$wkt)
crs_layer <- gsub("^((.*?),\\n\\s*?){2}", "", terra::crs(layer))
assertthat::assert_that(crs_grid == crs_layer)

landcoverfraction <- function(df) {
df %>%
mutate(frac_total = coverage_fraction / sum(coverage_fraction)) %>%
group_by(!!!syms(grid_group_by_col), value) %>%
summarize(freq = sum(frac_total), .groups = "drop_last")
mutate(frac_total = .data$coverage_fraction /
sum(.data$coverage_fraction)) %>%
group_by(!!!syms(grid_group_by_col), .data$value) %>%
summarize(freq = sum(.data$frac_total), .groups = "drop_last")
}

res <- exactextractr::exact_extract(
Expand All @@ -76,20 +81,20 @@ landusemetrics_grid_cell <- function(
fun = landcoverfraction,
summarize_df = TRUE,
include_cols = grid_group_by_col,
progress = progress)
progress = progress
)

return(res)

}

if (inherits(layer, "sf")) {
assertthat::assert_that(sf::st_crs(grid_cell)$wkt == sf::st_crs(layer)$wkt)

int <- st_intersection(layer, grid_cell)
int <- sf::st_intersection(layer, grid_cell)

cell_areas <- grid_cell %>%
select(!!!syms(grid_group_by_col)) %>%
mutate(cell_area = sf::st_area(geometry)) %>%
mutate(cell_area = sf::st_area(.data$geometry)) %>%
sf::st_drop_geometry()

temparrow <- tempfile(fileext = ".parquet")
Expand All @@ -102,14 +107,15 @@ landusemetrics_grid_cell <- function(

int <- arrow::open_dataset(temparrow) %>%
arrow::to_duckdb() %>%
group_by(!!!syms(grid_group_by_col),
!!!syms(layer_group_by_col),
cell_area) %>%
summarise(area_m2 = sum(area)) %>%
mutate(area_prop = area_m2 / cell_area) %>%
group_by(
!!!syms(grid_group_by_col),
!!!syms(layer_group_by_col),
.data$cell_area
) %>%
summarise(area_m2 = sum(.data$area)) %>%
mutate(area_prop = .data$area_m2 / .data$cell_area) %>%
collect()

return(int)
}
}

Loading

0 comments on commit 434acfb

Please sign in to comment.