|
1 |
| -#' GDAL VRT utilities for R |
| 1 | +#' @title vrtility: GDAL VRT utilities for R |
| 2 | +#' |
| 3 | +#' @description vrtility is a package for building raster (primarily remote sensing) |
| 4 | +#' processing pipelines. It makes use of GDAL's VRT (virtual raster format) |
| 5 | +#' capabilities for efficient processing of large raster datasets. This |
| 6 | +#' package's primary focus is on the use of GDAL VRT pixel functions. These |
| 7 | +#' pixel functions (currently implemented with python) are used to apply |
| 8 | +#' cloud masks and summarise pixel values (e.g. median) from multiple images |
| 9 | +#' (i.e create a composite image). We hope to add C++ or expression based |
| 10 | +#' pixel functions in time. |
| 11 | +#' @section Spatial helpers: |
| 12 | +#' \describe{ |
| 13 | +#' \item{\code{\link{bbox_to_projected}}}{Convert a long/lat bounding box to a projected bounding box} |
| 14 | +#' \item{\code{\link{to_wkt}}}{Get a wkt string from a numeric epsg code or character such as a proj4 string} |
| 15 | +#' } |
| 16 | +#' @section STAC helpers: |
| 17 | +#' \describe{ |
| 18 | +#' \item{\code{\link{stac_query}}}{Query a STAC catalog} |
| 19 | +#' \item{\code{\link{sentinel2_stac_query}}}{Query a STAC catalog for Sentinel-2 data} |
| 20 | +#' \item{\code{\link{sign_planetary_computer}}}{Sign a URL for the Planetary Computer} |
| 21 | +#' } |
| 22 | +#' @section VRT utilities: |
| 23 | +#' \describe{ |
| 24 | +#' \item{\code{\link{vrt_collect}}}{Create a vrt_collection object} |
| 25 | +#' \item{\code{\link{vrt_set_maskfun}}}{Set a mask function for a vrt object} |
| 26 | +#' \item{\code{\link{vrt_stack}}}{Create a vrt_stack object} |
| 27 | +#' \item{\code{\link{vrt_set_pixelfun}}}{Set a pixel function for a vrt stack object} |
| 28 | +#' \item{\code{\link{vrt_warp}}}{Warp a vrt_x object to a warped vrt} |
| 29 | +#' \item{\code{\link{vrt_compute}}}{Compute a vrt pipeline (using GDAL)} |
| 30 | +#' } |
| 31 | +#' @section VRT pixel functions: |
| 32 | +#' \describe{ |
| 33 | +#' \item{\code{\link{vrt_set_pixelfun}}}{Set a pixel function for a vrt stack object} |
| 34 | +#' \item{\code{\link{bitmask_numba}}}{A pixel function to apply a bitmask} |
| 35 | +#' \item{\code{\link{bitmask_numpy}}}{A pixel function to compute the median} |
| 36 | +#' \item{\code{\link{median_numba}}}{A pixel function to compute the median} |
| 37 | +#' \item{\code{\link{median_numpy}}}{A pixel function to compute the median} |
| 38 | +#' } |
| 39 | +#' @section python environment helpers: |
| 40 | +#' \describe{ |
| 41 | +#' \item{\code{\link{vrtility_py_require}}}{Require a python package} |
| 42 | +#' \item{\code{\link{set_py_env_vals}}}{Set python environment variables as options} |
| 43 | +#' } |
| 44 | +#' @section VRT helpers: |
| 45 | +#' \describe{ |
| 46 | +#' \item{\code{\link{save_vrt}}}{Save a VRT object to a file} |
| 47 | +#' \item{\code{\link{vrt_schema}}}{(data object)The official GDAL VRT schema as a character object} |
| 48 | +#' } |
| 49 | +#' |
2 | 50 | #'
|
3 |
| -#' THE GDAL VRT (Virtual Format) is an extremely powerful tool for combining |
4 |
| -#' and manipulating raster data. This package provides a set of utilities |
5 |
| -#' for working with VRTs in R. It is primarily focussed on harnessing the |
6 |
| -#' power of VRT pixel functions to carry out complex raster operations. This |
7 |
| -#' is achieved through the use of python and in particular the numba library |
8 |
| -#' which allows for the creation of fast, compiled functions that can be |
9 |
| -#' used in the VRT. All very much a work in progress... |
10 | 51 | #'
|
11 | 52 | #' @docType package
|
12 | 53 | #' @aliases vrtility-package
|
|
15 | 56 |
|
16 | 57 |
|
17 | 58 | .onLoad <- function(libname, pkgname) {
|
18 |
| - python_init_checks() |
| 59 | + vrtility_py_require(c("numpy", "numba")) |
19 | 60 | cache_init_checks()
|
20 | 61 | gdal_init_opts()
|
21 | 62 | }
|
22 |
| - |
23 |
| -python_init_checks <- function() { |
24 |
| - vrtility_python <- Sys.getenv("VRTILITY_PYTHON", unset = NA) |
25 |
| - |
26 |
| - if (!is.na(vrtility_python)) { |
27 |
| - reticulate::use_virtualenv(vrtility_python, required = TRUE) |
28 |
| - set_py_env_vals(vrtility_python) |
29 |
| - } else { |
30 |
| - if (reticulate::virtualenv_exists("vrtilitypy")) { |
31 |
| - reticulate::use_virtualenv("vrtilitypy", required = TRUE) |
32 |
| - set_py_env_vals("vrtilitypy") |
33 |
| - } else { |
34 |
| - cli::cli_inform( |
35 |
| - c( |
36 |
| - "!" = "Cannot locate the {cli::style_bold('VRTILITY_PYTHON')} environment", |
37 |
| - "i" = "Run {cli::code_highlight('`build_vrtility_python()`')} to install it" |
38 |
| - ), |
39 |
| - class = "packageStartupMessage" |
40 |
| - ) |
41 |
| - } |
42 |
| - } |
43 |
| -} |
44 |
| - |
45 |
| -cache_init_checks <- function() { |
46 |
| - op <- options() |
47 |
| - op_vrtility <- list( |
48 |
| - vrt.cache = tempdir() |
49 |
| - ) |
50 |
| - |
51 |
| - toset <- !(names(op_vrtility) %in% names(op)) |
52 |
| - if (any(toset)) options(op_vrtility[toset]) |
53 |
| -} |
54 |
| - |
55 |
| -gdal_init_opts <- function() { |
56 |
| - gdal_config_opts <- c( |
57 |
| - # Existing good settings |
58 |
| - GDAL_VRT_ENABLE_PYTHON = "YES", |
59 |
| - VSI_CACHE = "TRUE", |
60 |
| - GDAL_CACHEMAX = "50%", |
61 |
| - |
62 |
| - # Optimize VSI caching for COGs |
63 |
| - VSI_CACHE_SIZE = "25000000", # Increase cache size |
64 |
| - # CPL_VSIL_CURL_CHUNK_SIZE = "262144", |
65 |
| - # CPL_VSIL_CURL_CACHE_SIZE = "33554432", |
66 |
| - |
67 |
| - # Optimize HTTP connections |
68 |
| - GDAL_HTTP_MAX_RETRY = "5", |
69 |
| - GDAL_HTTP_RETRY_DELAY = "3", |
70 |
| - GDAL_HTTP_MULTIPLEX = "YES", |
71 |
| - CPL_VSIL_CURL_ALLOWED_EXTENSIONS = ".tif .TIF", # NEW: Allow both case variants |
72 |
| - CPL_VSIL_CURL_USE_HEAD = "NO", # NEW: Skip HEAD requests |
73 |
| - |
74 |
| - # Existing settings |
75 |
| - GDAL_DISABLE_READDIR_ON_OPEN = "EMPTY_DIR", |
76 |
| - GDAL_MAX_DATASET_POOL_SIZE = "500", |
77 |
| - GDAL_INGESTED_BYTES_AT_OPEN = "32000", |
78 |
| - GDAL_HTTP_VERSION = "2", |
79 |
| - GDAL_HTTP_MERGE_CONSECUTIVE_RANGES = "YES", |
80 |
| - GDAL_NUM_THREADS = "ALL_CPUS" |
81 |
| - ) |
82 |
| - |
83 |
| - gdal_warp_opts <- c( |
84 |
| - # Resampling |
85 |
| - "-r", |
86 |
| - "near", |
87 |
| - |
88 |
| - # Compression options |
89 |
| - "-co", |
90 |
| - "COMPRESS=LZW", # CHANGED: Often faster than DEFLATE for many datasets |
91 |
| - "-co", |
92 |
| - "PREDICTOR=2", |
93 |
| - "-co", |
94 |
| - "NUM_THREADS=ALL_CPUS", |
95 |
| - "-co", |
96 |
| - "BIGTIFF=YES", |
97 |
| - |
98 |
| - # Tiling options - optimized for COGs |
99 |
| - "-co", |
100 |
| - "TILED=YES", |
101 |
| - "-co", |
102 |
| - "BLOCKXSIZE=256", # CHANGED: 256 often better aligns with internal COG structure |
103 |
| - "-co", |
104 |
| - "BLOCKYSIZE=256", # CHANGED: 256 often better aligns with internal COG structure |
105 |
| - "-co", |
106 |
| - "COPY_SRC_OVERVIEWS=YES", # NEW: Copy source overviews when available |
107 |
| - |
108 |
| - # Memory and threading options |
109 |
| - "-wm", |
110 |
| - "50%", |
111 |
| - "-multi", |
112 |
| - # "-wo", |
113 |
| - # "NUM_THREADS=ALL_CPUS", |
114 |
| - |
115 |
| - # Overview options |
116 |
| - "-ovr", |
117 |
| - "AUTO" # NEW: Use overviews when available |
118 |
| - ) |
119 |
| - |
120 |
| - op <- options() |
121 |
| - op_gdal <- list( |
122 |
| - vrt.gdal.config.options = gdal_config_opts, |
123 |
| - vrt.gdal.warp.options = gdal_warp_opts |
124 |
| - ) |
125 |
| - |
126 |
| - toset <- !(names(op_gdal) %in% names(op)) |
127 |
| - if (any(toset)) options(op_gdal[toset]) |
128 |
| -} |
0 commit comments