mapme-initiative / mapme.biodiversity

Efficient analysis of spatial biodiversity datasets for global portfolios
https://mapme-initiative.github.io/mapme.biodiversity/dev
GNU General Public License v3.0
33 stars 7 forks source link

soilgrids ressources need to be deleted when changing the extent of the portfolio #132

Closed Shirobakaidou closed 4 months ago

Shirobakaidou commented 1 year ago

When AOI is extended, the raster data for the extended part is not additionally downloaded. A feasible solution at the moment is deleting existing raster resources and downloading data for the whole study area again. But duplicated data download is inefficient, it would be great if the package could download the raster data for the extended aoi in addition to the existing raster files.

library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 7.0.0; sf_use_s2() is TRUE
library(mapme.biodiversity)
library(tidyr)

dir1 <- file.path(tempdir(), "dir1")
unlink(list.files(dir1, full.names = TRUE), recursive = TRUE)
dir.create(dir1)

aoi <- read_sf(system.file("extdata", "sierra_de_neiba_478140_2.gpkg",
                           package = "mapme.biodiversity")) %>% 
  st_geometry() %>% st_as_sf()

ini1 <- init_portfolio(
  x = aoi,
  years = 2015:2020,
  outdir = dir1,
  tmpdir = tempdir(),
  cores = 4,
  verbose = TRUE
) 

getSoil.1 <- get_resources(ini1, resources = "soilgrids",layers = "clay", depths = "0-5cm", stats = "mean")
#> Starting process to download resource 'soilgrids'........
#> Starting to download data for layer 'clay', depth '0-5cm', and stat 'mean'. This may take a while...
getBiome.1 <- get_resources(ini1, resources = "teow")
#> Starting process to download resource 'teow'........

# Extending spatial extent
bbox = st_bbox(c(xmin=44.14, xmax=44.24, ymax=-24.52, ymin=-24.62), crs=st_crs(4326)) %>%
  st_as_sfc() %>% st_as_sf
grid = st_make_grid(bbox, n=c(2,2)) %>% st_as_sf()
aoi2 = aoi %>% rbind(grid)

ini2 <- init_portfolio(
  x = aoi2,
  years = 2015:2020,
  outdir = dir1,
  tmpdir = tempdir(),
  cores = 4,
  add_resources = FALSE,
  verbose = TRUE
)

getSoil.2 = get_resources(ini2, "soilgrids", layers = "clay", depths = "0-5cm", stats = "mean")
#> Starting process to download resource 'soilgrids'........
#> Output file clay_0-5cm_mean.tif exists. Skipping re-download. Please delete if spatial extent has changed.
calc_indicators(getSoil.2, "soilproperties", stats_soil = c("mean"), engine = "extract") %>%
  unnest(soilproperties) %>%
  pivot_wider(names_from = c("layer", "depth", "stat"), values_from = "mean")
#> Simple feature collection with 5 features and 3 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -71.80933 ymin: -24.62 xmax: 44.24 ymax: 18.69931
#> Geodetic CRS:  WGS 84
#> # A tibble: 5 × 4
#>   assetid                                                      x clay_…¹ NA_NA…²
#> *   <int>                                          <POLYGON [°]>   <dbl>   <dbl>
#> 1       1 ((-71.76134 18.66333, -71.76067 18.66267, -71.75932 1…    35.0      NA
#> 2       2 ((44.14 -24.62, 44.19 -24.62, 44.19 -24.57, 44.14 -24…    NA        NA
#> 3       3 ((44.19 -24.62, 44.24 -24.62, 44.24 -24.57, 44.19 -24…    NA        NA
#> 4       4 ((44.14 -24.57, 44.19 -24.57, 44.19 -24.52, 44.14 -24…    NA        NA
#> 5       5 ((44.19 -24.57, 44.24 -24.57, 44.24 -24.52, 44.19 -24…    NA        NA
#> # … with abbreviated variable names ¹​`clay_0-5cm_mean`, ²​NA_NA_NA

getBiome.2 = get_resources(ini2, "teow")
#> Starting process to download resource 'teow'........
calc_indicators(getBiome.2, indicators = "biome") %>%
  unnest(biome)
#> Simple feature collection with 5 features and 3 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -71.80933 ymin: -24.62 xmax: 44.24 ymax: 18.69931
#> Geodetic CRS:  WGS 84
#> # A tibble: 5 × 4
#>   assetid biomes                                  area                         x
#>     <int> <fct>                                  <dbl>             <POLYGON [°]>
#> 1       1 Tropical & Subtropical Coniferous Fo… 18349. ((-71.76134 18.66333, -7…
#> 2       2 Deserts & Xeric Shrublands             2811. ((44.14 -24.62, 44.19 -2…
#> 3       3 Deserts & Xeric Shrublands             2811. ((44.19 -24.62, 44.24 -2…
#> 4       4 Deserts & Xeric Shrublands             2812. ((44.14 -24.57, 44.19 -2…
#> 5       5 Deserts & Xeric Shrublands             2812. ((44.19 -24.57, 44.24 -2…
goergen95 commented 1 year ago

Thanks for opening a new issue! Currently the need to delete the previous downloaded files is only an issue with the soilgrids resource because of the way it is distributed by the provider. For all other ressources, no files that are already present will be redownloaded. I will have to look for a way how we can manage this for the soilgrids. Until a solution is found you will have to delete the soildrid data if you change the extent of your AOI.

goergen95 commented 4 months ago

Latest main uses the VRT file instead of downloading any data from soilgrids. This resource is thus now "online" only and it might be challenging for large-scale portfolios. I am however closing this issue now since changing the extent of the portfolio should no longer be a problem.