Probably won't work well with decimal degress out_epsg

make_base_grid(
  aoi,
  out_res = 90,
  out_epsg = 8059,
  lcm = 9000,
  name = "base",
  use_mask = NULL,
  out_file = NULL,
  values = 1,
  ret = "object",
  ...
)

Arguments

aoi

sf from which an extent will be defined for the base grid

out_res

Numeric. Desired resolution of the base grid

out_epsg

Numeric. Appropriate epsg code to define the coordinate reference system for the base grid

lcm

Numeric. Least common multiple. The extent of the resulting base grid will be divisible by lcm. Default 9000 is dervied by numbers::mLCM(c(1, 2, 3, 4, 5, 10, 20, 30, 50, 90, 100, 500, 1000))

name

Character. Name of the layer in the base grid

use_mask

sf. Values in the bast grid outside of use_mask will be NA

out_file

Character. Path into which to save the base grid

values

Used to fill the base grid

ret

Character. If not "path", will return the resulting spatRaster. If "path", will return out_file. If out_file is NULL ret is "object".

...

Passed to terra::writeRaster(). Usually datatype = "INT1U" and, maybe, gdal = c("COMPRESS=NONE")

Value

spatRaster or path. If out_file supplied, the base grid is written to out_file

Examples


  #library(envRaster)

  out_dir <- file.path(system.file(package = "envRaster"), "examples")

  cube_dir <- fs::path(out_dir, "cube")
  settings_path <- fs::path(cube_dir, "settings.rds")

  if(!file.exists(settings_path)) {

    source(fs::path(out_dir, "name_env_tif_ex.R"))

  }

  settings <- rio::import(settings_path)
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.

  base <- make_base_grid(aoi # aoi is sf provided with envRaster
                         , out_res = 30
                         , out_epsg = 8059
                         , out_file = fs::path(cube_dir, "base.tif")
                         , datatype = "INT1U"
                         , overwrite = TRUE
                         )

  # Can't get indices with save_cube = FALSE but can pass cube onto apply_pixel
  cube <- get_sat_data(x = base
                       , start_date = "2023-01-01"
                       , end_date = "2023-12-31"
                       , collections = c("ga_ls9c_ard_3"
                                         , "ga_ls8c_ard_3"
                                         )
                       , property_filter = function(x) {x[["eo:cloud_cover"]] < 20}
                       , period = "P3M"
                       , layers = c("nir", "red")
                       , indices = NULL
                       , save_cube = FALSE
                       )
#> 
  |                                                                            
  |======================================================================| 100%
#> attempt: 1. features: 91

  if(FALSE) {

    # takes a minute or two
    cube %>%
      gdalcubes::apply_pixel("(nbart_nir - nbart_red) / (nbart_nir + nbart_red)", "ndvi") %>%
      gdalcubes::animate(col = viridis::viridis
                         , zlim = c(0, 1)
                         , downsample = FALSE
                         )

  }

  # Save a cube
  if(FALSE) {

    # takes a minute or two
    get_sat_data(x = base
                 , start_date = "2022-12-01"
                 , end_date = "2023-11-30"
                 , collections = settings$collection
                 , period = settings$period
                 , layers = NULL
                 , indices = list("ndvi" = c("nir", "red"))
                 , save_cube = TRUE
                 , out_dir = settings$out_dir

                 # passed to gdalcubes::write_tif
                 , creation_options = list("COMPRESS" = "NONE")
                 )

    # biggest contrast in ndvi in autumn. least contrast in spring
    ndvi_tifs <- fs::dir_ls(settings$out_dir, regexp = "tif$")

    ndvis <- terra::rast(ndvi_tifs)

    names(ndvis) <- gsub("ndvi__|\\.tif", "", basename(ndvi_tifs))

    terra::global(ndvis
                  , fun = \(x) quantile(x, probs = 0.75) - quantile(x, probs = 0.25)
                  )

    ndvis %>%
      terra::panel()

  }