Download and Process a GEE Raster Layer into a Cloud-Optimized GeoTIFF
download_gee_layer.RdGeneric function for downloading any raster data from Google Earth Engine. Handles authentication, export management, file downloading, and processing into a local Cloud-Optimized GeoTIFF. Exports to Google Drive root to avoid GEE folder duplication bugs.
Uso
download_gee_layer(
boundary_layer,
iso3,
gee_project,
asset_id,
file_prefix,
output_dir = here::here(),
scale = 10,
datatype = "INT1U",
googledrive_folder = NULL,
wait_time = 5,
band = NULL,
composite_method = c("mosaic", "mode", "median", "mean"),
year_override = NULL,
pus = NULL,
aggregate_to_pus = FALSE,
aggregation_reducer = c("mode", "mean", "sum")
)Argumentos
- boundary_layer
An
sfobject defining the spatial boundary of interest.- iso3
Character. Three-letter ISO country code for filename generation.
- gee_project
Character. Google Earth Engine cloud project ID. This is required and must be a valid GEE project you have access to. Find your project ID in the GEE Code Editor.
- asset_id
Character. Earth Engine ImageCollection asset ID (e.g., "projects/sat-io/open-datasets/...").
- file_prefix
Character. Prefix for export filename and GEE task description.
- output_dir
Character. Path to directory for saving the final raster file. Defaults to project root via
here::here().- scale
Numeric. Resolution of the exported image in meters. Default is 10. Ignored when
aggregate_to_pus = TRUE(uses PU resolution instead).- datatype
Character. Output datatype (GDAL style), e.g., "INT1U" or "FLT4S". Default is "INT1U".
- googledrive_folder
Character or NULL. Google Drive folder name for exports. Currently defaults to NULL (Drive root) to avoid a GEE folder duplication bug.
- wait_time
Numeric. Maximum time in minutes to wait for the GEE export to appear in Google Drive. Default is 5. Increase for large exports.
- band
Character or NULL. Specific band to select from the image. If NULL, uses all bands. Useful for datasets like Dynamic World ("label") or ESA WorldCover ("Map").
- composite_method
Character. Method for creating temporal composite: "mosaic" (default), "mode" (most frequent value), "median", or "mean". Use "mode" for categorical data like Dynamic World.
- year_override
Numeric or NULL. If provided, use this year instead of automatically detecting the most recent year with data.
- pus
SpatRaster or NULL. Planning units raster for GEE-side aggregation. When provided with
aggregate_to_pus = TRUE, the export will be resampled to match the PU resolution and projection in GEE before download.- aggregate_to_pus
Logical. If TRUE and
pusis provided, aggregate the data to planning unit resolution in GEE before export. This significantly speeds up processing by eliminating local resampling. Default is FALSE.- aggregation_reducer
Character. Reducer to use for GEE-side aggregation: "mode" (default, for categorical data), "mean", or "sum".
Valor
A SpatRaster object written to disk, or NULL if export timed out.
When aggregate_to_pus = TRUE, the result has attribute pre_aggregated = TRUE.
Detalles
For most use cases, prefer the higher-level wrapper functions like
download_esri_lulc_data() or download_global_pasture_data() which
have sensible defaults for common datasets.
Ejemplos
if (FALSE) { # \dontrun{
# Download ESRI LULC data for Ghana
lulc <- download_gee_layer(
boundary_layer = ghana_boundary,
iso3 = "GHA",
gee_project = "my-gee-project",
asset_id = "projects/sat-io/open-datasets/landcover/ESRI_Global-LULC_10m_TS",
file_prefix = "esri_10m_lulc",
scale = 10,
datatype = "INT1U"
)
# Download with GEE-side aggregation to planning units
lulc_agg <- download_gee_layer(
boundary_layer = ghana_boundary,
iso3 = "GHA",
gee_project = "my-gee-project",
asset_id = "projects/sat-io/open-datasets/landcover/ESRI_Global-LULC_10m_TS",
file_prefix = "esri_10m_lulc_agg",
pus = planning_units,
aggregate_to_pus = TRUE,
aggregation_reducer = "mode"
)
} # }