134
134
# ' (i.e., to aggregate pixel values from multiple images into a single value).
135
135
# ' Options include "merge", which 'stamps' images on top of one another such that
136
136
# ' the "last" value downloaded for a pixel -- which isn't guaranteed to be the most
137
- # ' recent one -- will be the only value used, or any of "sum", "mean", "median",
137
+ # ' recent one -- will be the only value used, or any of "sum", "mean", "median",
138
138
# ' "min", or "max", which consider all values available at each pixel.
139
139
# ' Set to `NULL` to not composite
140
140
# ' (i.e., to rescale and save each individual file independently).
143
143
# ' argument of [sf::gdal_utils()]. The same set of options are used for all
144
144
# ' downloaded data and the final output images; this means that some common
145
145
# ' options (for instance, `PREDICTOR=3`) may cause errors if bands are of
146
- # ' varying data types. The default values are provided by
146
+ # ' varying data types. The default values are provided by
147
147
# ' [rsi_gdalwarp_options()].
148
148
# ' @param gdal_config_options Options passed to `gdalwarp` through the
149
149
# ' `config_options` argument of [sf::gdal_utils()]. The default values are
186
186
# ' end_date = "2022-08-30",
187
187
# ' output_filename = tempfile(fileext = ".tif")
188
188
# ' )
189
- # '
190
- # ' landsat_image |>
189
+ # '
190
+ # ' landsat_image |>
191
191
# ' terra::rast() |>
192
192
# ' terra::stretch() |>
193
193
# ' terra::plotRGB()
194
- # '
195
- # ' # The `get_*_imagery()` functions will download
194
+ # '
195
+ # ' # The `get_*_imagery()` functions will download
196
196
# ' # all available "data" assets by default
197
197
# ' # (usually including measured values, and excluding derived bands)
198
198
# ' sentinel1_data <- get_sentinel1_imagery(
202
202
# ' output_filename = tempfile(fileext = ".tif")
203
203
# ' )
204
204
# ' names(terra::rast(sentinel1_data))
205
- # '
205
+ # '
206
206
# ' # You can see what bands will be downloaded by a function
207
207
# ' # by inspecting the corresponding `band_mapping` object:
208
208
# ' sentinel2_band_mapping$planetary_computer_v1
209
- # '
209
+ # '
210
210
# ' # And you can add additional assets using `c()`:
211
211
# ' c(
212
212
# ' sentinel2_band_mapping$planetary_computer_v1,
213
213
# ' "scl"
214
214
# ' )
215
- # '
215
+ # '
216
216
# ' # Or subset the assets downloaded using `[` or `[[`:
217
217
# ' sentinel2_imagery <- get_sentinel2_imagery(
218
218
# ' aoi,
222
222
# ' output_filename = tempfile(fileext = ".tif")
223
223
# ' )
224
224
# ' names(terra::rast(sentinel2_imagery))
225
- # '
225
+ # '
226
226
# ' # If you're downloading data for a particularly large AOI,
227
227
# ' # and can't composite the resulting images or want to make
228
228
# ' # sure you can continue an interrupted download,
239
239
# ' )
240
240
# ' }
241
241
# ' )
242
- # ' # You'll get a list of tiles that you can then composite or
242
+ # ' # You'll get a list of tiles that you can then composite or
243
243
# ' # work with however you wish:
244
244
# ' unlist(tiles)
245
245
# '
@@ -273,6 +273,17 @@ get_stac_data <- function(aoi,
273
273
)
274
274
}
275
275
276
+ aoi_bbox <- sf :: st_bbox(aoi )
277
+ if (aoi_bbox $ xmin == aoi_bbox $ xmax | aoi_bbox $ ymin == aoi_bbox $ ymax ) {
278
+ rlang :: abort(
279
+ c(
280
+ " `aoi` has no extent." ,
281
+ " i" = " This can occur when `aoi` is a single point geometry."
282
+ ),
283
+ class = " rsi_aoi_is_point"
284
+ )
285
+ }
286
+
276
287
if (sf :: st_is_longlat(aoi ) && ! (is.null(pixel_x_size ) || is.null(pixel_y_size )) && all(c(pixel_x_size , pixel_y_size ) %in% c(10 , 30 ))) {
277
288
rlang :: warn(
278
289
c(
@@ -360,13 +371,14 @@ get_stac_data <- function(aoi,
360
371
if (is.null(asset_names )) {
361
372
asset_names <- rstac :: items_assets(items )
362
373
if (length(asset_names ) > 1 ) {
363
- rlang :: warn(c(
364
- " `asset_names` was `NULL`, so rsi is attempting to download all assets in items in this collection." ,
365
- i = " This includes multiple assets, so rsi is attempting to download all of them using the same download function." ,
366
- i = " This might cause errors or not be what you want! Specify `asset_names` to fix this (and to silence this warning)."
367
- ),
368
- class = " rsi_missing_asset_names"
369
- )
374
+ rlang :: warn(
375
+ c(
376
+ " `asset_names` was `NULL`, so rsi is attempting to download all assets in items in this collection." ,
377
+ i = " This includes multiple assets, so rsi is attempting to download all of them using the same download function." ,
378
+ i = " This might cause errors or not be what you want! Specify `asset_names` to fix this (and to silence this warning)."
379
+ ),
380
+ class = " rsi_missing_asset_names"
381
+ )
370
382
}
371
383
}
372
384
if (is.null(names(asset_names ))) names(asset_names ) <- asset_names
@@ -412,6 +424,9 @@ get_stac_data <- function(aoi,
412
424
if (! is.null(stats :: na.action(download_results ))) {
413
425
items $ features [stats :: na.action(download_results )] <- NULL
414
426
}
427
+
428
+ temp_ras_files <- unlist(download_results )
429
+ on.exit(file.remove(temp_ras_files ), add = TRUE )
415
430
# mask
416
431
if (! is.null(mask_band )) {
417
432
download_results <- rsi_apply_masks(
@@ -440,10 +455,7 @@ get_stac_data <- function(aoi,
440
455
lapply(download_results , rescale_band , scale_strings )
441
456
}
442
457
443
- on.exit(file.remove(unlist(download_results )), add = TRUE )
444
-
445
458
if (drop_mask_band ) items_urls [[mask_band ]] <- NULL
446
-
447
459
mapply(
448
460
function (in_bands , vrt ) {
449
461
stack_rasters(
@@ -462,7 +474,8 @@ get_stac_data <- function(aoi,
462
474
app <- tryCatch(rstac :: items_datetime(items ), error = function (e ) NA )
463
475
app <- gsub(" :" , " " , app ) # #29, #32
464
476
if (any(is.na(app ))) app <- NULL
465
- app <- app %|| % seq_along(download_results )
477
+ app <- app %|| % as.character(seq_along(download_results ))
478
+ app <- make.unique(app , sep = " _" )
466
479
467
480
output_filename <- paste0(
468
481
tools :: file_path_sans_ext(output_filename ),
@@ -724,15 +737,17 @@ rsi_composite_bands <- function(download_locations,
724
737
)
725
738
)
726
739
} else {
727
- do.call(
728
- terra :: mosaic ,
729
- list (
730
- x = terra :: sprc(lapply(download_locations [[band_name ]], terra :: rast )),
731
- fun = composite_function ,
732
- filename = out_file ,
733
- overwrite = TRUE
740
+ capture.output({
741
+ do.call(
742
+ terra :: mosaic ,
743
+ list (
744
+ x = terra :: sprc(lapply(download_locations [[band_name ]], terra :: rast )),
745
+ fun = composite_function ,
746
+ filename = out_file ,
747
+ overwrite = TRUE
748
+ )
734
749
)
735
- )
750
+ } )
736
751
}
737
752
738
753
out_file
@@ -845,11 +860,12 @@ get_rescaling_formula <- function(items, band_name, element) {
845
860
)
846
861
847
862
if (length(unique(elements )) != 1 ) {
848
- rlang :: warn(c(
849
- glue :: glue(" Images in band {band_name} have different {element}s." ),
850
- i = " Returning images without rescaling."
851
- ),
852
- class = " rsi_multiple_scaling_formulas"
863
+ rlang :: warn(
864
+ c(
865
+ glue :: glue(" Images in band {band_name} have different {element}s." ),
866
+ i = " Returning images without rescaling."
867
+ ),
868
+ class = " rsi_multiple_scaling_formulas"
853
869
)
854
870
elements <- NA_real_
855
871
}
0 commit comments