Skip to content

Commit

Permalink
Merge pull request #287 from mapme-initiative/dev
Browse files Browse the repository at this point in the history
GDAL based backend (#240)
  • Loading branch information
goergen95 authored Jun 11, 2024
2 parents dd4600e + 2e5b227 commit 0297fa5
Show file tree
Hide file tree
Showing 169 changed files with 2,082 additions and 3,109 deletions.
13 changes: 7 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ URL: https://mapme-initiative.github.io/mapme.biodiversity/index.html,
https://github.com/mapme-initiative/mapme.biodiversity/
BugReports: https://github.com/mapme-initiative/mapme.biodiversity/issues
Depends: R (>= 3.5.0)
SystemRequirements: GDAL (>= 3.0.0), PROJ (>= 4.8.0)
Imports: dplyr, furrr, httr, magrittr, purrr, R.utils, sf, terra, tibble, tidyr
SystemRequirements: GDAL (>= 3.7.0), PROJ (>= 4.8.0)
Imports: dplyr, furrr, httr, jsonlite, magrittr, purrr, sf, terra, tibble,
tidyr
Suggests: exactextractr, future, knitr, landscapemetrics, progressr, rmarkdown,
rstac, rvest, testthat (>= 3.0.0)
VignetteBuilder: knitr
Config/Needs/website: DiagrammeR, ggplot2, lubridate, manipulateWidget, rgl, spatstat,
wdpar
Config/Needs/website: DiagrammeR
Config/testthat/edition: 3
Encoding: UTF-8
Language: en-GB
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Collate:
'register.R'
'calc_active_fire_counts.R'
'calc_biome.R'
'calc_burned_area.R'
'calc_deforestation_drivers.R'
'calc_drought_indicator.R'
'calc_ecoregion.R'
Expand Down Expand Up @@ -73,7 +73,7 @@ Collate:
'get_gfw_treecover.R'
'get_gmw.R'
'get_gsw.R'
'get_nasa_firms.R'
'get_mcd64A1.R'
'get_nasa_grace.R'
'get_nasa_srtm.R'
'get_nelson_et_al.R'
Expand All @@ -84,4 +84,5 @@ Collate:
'get_worldpop.R'
'mapme.biodiversity-pkg.R'
'portfolio.R'
'spatial-utils.R'
'utils.R'
10 changes: 4 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

export(available_indicators)
export(available_resources)
export(calc_active_fire_counts)
export(calc_biome)
export(calc_burned_area)
export(calc_deforestation_drivers)
export(calc_drought_indicator)
export(calc_ecoregion)
Expand Down Expand Up @@ -32,7 +32,6 @@ export(check_available_years)
export(check_engine)
export(check_namespace)
export(check_stats)
export(download_or_skip)
export(get_chirps)
export(get_esalandcover)
export(get_fritz_et_al)
Expand All @@ -45,7 +44,7 @@ export(get_global_surface_water_recurrence)
export(get_global_surface_water_seasonality)
export(get_global_surface_water_transitions)
export(get_gmw)
export(get_nasa_firms)
export(get_mcd64a1)
export(get_nasa_grace)
export(get_nasa_srtm)
export(get_nelson_et_al)
Expand All @@ -57,6 +56,7 @@ export(get_worldclim_max_temperature)
export(get_worldclim_min_temperature)
export(get_worldclim_precipitation)
export(get_worldpop)
export(make_footprints)
export(make_global_grid)
export(mapme_options)
export(portfolio_long)
Expand All @@ -65,17 +65,15 @@ export(prep_resources)
export(register_indicator)
export(register_resource)
export(select_engine)
export(unzip_and_remove)
export(spds_exists)
export(write_portfolio)
import(sf)
import(terra)
importFrom(httr,http_error)
importFrom(magrittr,"%>%")
importFrom(purrr,walk)
importFrom(stats,sd)
importFrom(stats,var)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
importFrom(utils,download.file)
importFrom(utils,read.csv)
importFrom(utils,unzip)
19 changes: 19 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@

- updates `gfw_lossyear` resource to `v20240402` which entails emission data
between 2000 - 2023
- removes the `nasa_firms` resource and associated `active_fire_counts` indicator
- adds `mcd64a1` resource and `burned_area` indicator
- `mapme.biodiveristy` now leverages GDAL for data I/O meaning that all GDAL
readable source data sets and writable destinations are now supported
- `README.md` now includes a section on how to set up cloud-storages to use
as a destination for resource data
- The quickstart vignette now uses GFW data as example data
- chunking is now applied based on the area of an assets bounding box instead
of its own area

## Internal

- exports `make_footprints()` to ease the process of creating footprints
for resource functions
- exports `spds_exists()` for resource function to check if a data source
is exists
- resource functions are now expected to return footprints for all resource elements including opening and creation options as well as standardized filenames
- in case a user-specified destination is found, the package now uses `gdal_translate` to write data from source to destination
- tests for long-running examples and tests are skipped on GA and CRAN

# mapme.biodiversity 0.7.0

Expand Down
88 changes: 0 additions & 88 deletions R/calc_active_fire_counts.R

This file was deleted.

90 changes: 90 additions & 0 deletions R/calc_burned_area.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#' Calculate Monthly Burned Area based on MODIS (MCD64A1)
#'
#' Calculates Monthly Burned Area based on the Terra and Aqua combined
#' MCD64A1 Version 6.1. which s a monthly, global gridded 500 meter (m)
#' product containing per-pixel burned-area information.
#'
#' The required resources for this indicator are:
#' - [mcd64a1]
#'
#' @name burned_area
#' @param engine The preferred processing functions from either one of "zonal",
#' "extract" or "exactextract" as character.
#' @keywords indicator
#' @returns A function that returns a tibble with a column for terrain
#' ruggedness index statistics (in meters).
#' @references Giglio, L., C. Justice, L. Boschetti, D. Roy. MODIS/Terra+Aqua
#' Burned Area Monthly L3 Global 500m SIN Grid V061. 2021, distributed by
#' NASA EOSDIS Land Processes Distributed Active Archive Center.
#' \doi{https://doi.org/10.5067/MODIS/MCD64A1.061}
#' @include register.R
#' @export
#' @examples
#' \dontshow{
#' mapme.biodiversity:::.copy_resource_dir(file.path(tempdir(), "mapme-data"))
#' }
#' \dontrun{
#' library(sf)
#' library(mapme.biodiversity)
#'
#' outdir <- file.path(tempdir(), "mapme-data")
#' dir.create(outdir, showWarnings = FALSE)
#'
#' mapme_options(
#' outdir = outdir,
#' verbose = FALSE
#' )
#'
#' aoi <- system.file("extdata", "sierra_de_neiba_478140_2.gpkg",
#' package = "mapme.biodiversity"
#' ) %>%
#' read_sf() %>%
#' get_resources(get_mcd64a1(years = 2010)) %>%
#' calc_indicators(calc_burned_area(engine = "extract")) %>%
#' portfolio_long()
#'
#' aoi
#' }
calc_burned_area <- function(engine = "extract") {
engine <- check_engine(engine)

function(x,
mcd64a1 = NULL,
name = "burned_area",
mode = "asset",
aggregation = "sum",
verbose = mapme_options()[["verbose"]]) {
if (is.null(mcd64a1)) {
return(NULL)
}

x_proj <- st_transform(x, st_crs(mcd64a1))
mcd64a1 <- terra::mask(mcd64a1, x_proj)
mcd64a1 <- terra::subst(mcd64a1, from = c(-2, -1, 0, NA), to = 0, others = 1)
arearaster <- cellSize(mcd64a1, mask = FALSE, unit = "ha")
mcd64a1 <- mcd64a1 * arearaster

stats <- select_engine(
x = x_proj,
raster = mcd64a1,
stats = "sum",
engine = engine,
name = "burned_area",
mode = "asset"
)

names(stats) <- "value"

dates <- gsub("^.*?\\.A([0-9]+)\\..*$", "\\1", names(mcd64a1))
stats[["datetime"]] <- as.Date(dates, "%Y%j")
stats[["unit"]] <- "ha"
stats[["variable"]] <- "burned_area"
stats[, c("datetime", "variable", "unit", "value")]
}
}

register_indicator(
name = "burned_area",
description = "Monthly burned area detected by MODIS satellites",
resources = "mcd64a1"
)
9 changes: 5 additions & 4 deletions R/calc_fatalities.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
#'
#' mapme_options(
#' outdir = outdir,
#' verbose = FALSE
#' verbose = FALSE,
#' chunk_size = 1e8
#' )
#'
#' aoi <- system.file("extdata", "burundi.gpkg",
Expand Down Expand Up @@ -117,10 +118,10 @@ calc_fatalities <- function(years = 1989:2023,
date_prec <- where_prec <- date_start <- type_of_violence <- NULL
year <- month <- deaths_a <- deaths_b <- event_count <- type_of_death <- NULL

ucdp_ged <- ucdp_ged[[1]]
if (length(ucdp_ged) == 0) {
return(NA)
if (is.null(ucdp_ged)) {
return(NULL)
}
ucdp_ged <- ucdp_ged[[1]]

months_tibble <- tidyr::expand_grid(
year = as.character(years),
Expand Down
Loading

0 comments on commit 0297fa5

Please sign in to comment.