Skip to content

Commit

Permalink
Merge pull request #1009 from rolfsimoes/feature/s1
Browse files Browse the repository at this point in the history
Feature/s1
  • Loading branch information
gilbertocamara committed Sep 25, 2023
2 parents 52bc52d + b91736d commit 8ed520e
Show file tree
Hide file tree
Showing 22 changed files with 671 additions and 40 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ Collate:
'api_raster_sub_image.R'
'api_raster_terra.R'
'api_reclassify.R'
'api_regularize.R'
'api_roi.R'
'api_s2tile.R'
'api_samples.R'
'api_segments.R'
'api_sf.R'
Expand Down
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ S3method(.cube_token_generator,default)
S3method(.cube_token_generator,mpc_cube)
S3method(.data_get_ts,class_cube)
S3method(.data_get_ts,raster_cube)
S3method(.gc_arrange_images,"mpc_cube_sentinel-1-grd")
S3method(.gc_arrange_images,raster_cube)
S3method(.mosaic_split_band_date,derived_cube)
S3method(.mosaic_split_band_date,raster_cube)
S3method(.raster_check_package,terra)
Expand Down Expand Up @@ -114,6 +116,7 @@ S3method(.raster_ymax,terra)
S3method(.raster_ymin,terra)
S3method(.raster_yres,terra)
S3method(.slice_dfr,numeric)
S3method(.source_collection_access_test,"mpc_cube_sentinel-1-grd")
S3method(.source_collection_access_test,mpc_cube)
S3method(.source_collection_access_test,stac_cube)
S3method(.source_collection_access_test,usgs_cube)
Expand All @@ -131,6 +134,7 @@ S3method(.source_items_cube,stac_cube)
S3method(.source_items_fid,stac_cube)
S3method(.source_items_new,"aws_cube_landsat-c2-l2")
S3method(.source_items_new,"mpc_cube_landsat-c2-l2")
S3method(.source_items_new,"mpc_cube_sentinel-1-grd")
S3method(.source_items_new,"mpc_cube_sentinel-2-l2a")
S3method(.source_items_new,aws_cube)
S3method(.source_items_new,bdc_cube)
Expand All @@ -140,13 +144,15 @@ S3method(.source_items_new,sdc_cube)
S3method(.source_items_new,usgs_cube)
S3method(.source_items_tile,"aws_cube_landsat-c2-l2")
S3method(.source_items_tile,"mpc_cube_landsat-c2-l2")
S3method(.source_items_tile,"mpc_cube_sentinel-1-grd")
S3method(.source_items_tile,"mpc_cube_sentinel-2-l2a")
S3method(.source_items_tile,aws_cube)
S3method(.source_items_tile,bdc_cube)
S3method(.source_items_tile,deafrica_cube)
S3method(.source_items_tile,hls_cube)
S3method(.source_items_tile,sdc_cube)
S3method(.source_items_tile,usgs_cube)
S3method(.source_tile_get_bbox,"mpc_cube_sentinel-1-grd")
S3method(.source_tile_get_bbox,stac_cube)
S3method(.tile,default)
S3method(.tile,raster_cube)
Expand Down Expand Up @@ -317,6 +323,7 @@ S3method(sits_model_export,sits_model)
S3method(sits_reclassify,class_cube)
S3method(sits_reclassify,default)
S3method(sits_reclassify,tbl_df)
S3method(sits_regularize,"mpc_cube_sentinel-1-grd")
S3method(sits_regularize,default)
S3method(sits_regularize,derived_cube)
S3method(sits_regularize,raster_cube)
Expand Down
11 changes: 7 additions & 4 deletions R/api_cube.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,13 @@ NULL
}
#' @export
.cube_s3class.raster_cube <- function(cube) {
unique(c(
.source_s3class(source = .cube_source(cube = cube)),
class(cube)
))
s3_class <- .source_s3class(source = .cube_source(cube = cube))
col_class <- paste(
s3_class[[1]],
tolower(.tile_collection(cube)),
sep = "_"
)
unique(c(col_class, s3_class, class(cube)))
}
#' @export
.cube_s3class.default <- function(cube) {
Expand Down
49 changes: 37 additions & 12 deletions R/api_gdal.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,54 @@
#' @param file Files to be written to (with path)
#' @param base_files Files to be copied from (with path)
#' @param multicores Number of cores to be used in parallel
#' @param roi ROI to crop base_files
#' @returns Name of file that was written to
.gdal_merge_into <- function(file, base_files, multicores) {
.gdal_merge_into <- function(file, base_files, multicores, roi = NULL) {
r_obj <- .raster_open_rast(file)
roi <- .roi_as_sf(roi, as_crs = .raster_crs(r_obj))
# Merge src_files
file <- .try(
{
.gdal_warp(
file = file,
base_files = base_files,
params = list(
"-wo" = paste0("NUM_THREADS=", multicores),
"-multi" = TRUE,
"-q" = TRUE,
"-overwrite" = FALSE
),
quiet = TRUE
)
if (.has(roi)) {
# Write roi in a temporary file
roi_file <- .roi_write(
roi = roi,
output_file = tempfile(fileext = ".shp"),
quiet = TRUE
)
.gdal_warp(
file = file,
base_files = base_files,
params = list(
"-wo" = paste0("NUM_THREADS=", multicores),
"-multi" = TRUE,
"-cutline" = roi_file,
"-q" = TRUE,
"-overwrite" = FALSE
),
quiet = TRUE
)
} else {
.gdal_warp(
file = file,
base_files = base_files,
params = list(
"-wo" = paste0("NUM_THREADS=", multicores),
"-multi" = TRUE,
"-q" = TRUE,
"-overwrite" = FALSE
),
quiet = TRUE
)
}
},
.rollback = {
unlink(file)
},
.finally = {
# Delete auxiliary files
unlink(paste0(file, ".aux.xml"))
if (.has(roi)) unlink(roi_file)
}
)
# Return file
Expand Down
98 changes: 84 additions & 14 deletions R/api_gdalcubes.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
#' @param cube Data cube.
#' @param timeline Timeline of regularized cube
#' @param period Period of interval to aggregate images
#'
#' @param roi Optional. Used only for Sentinel-1 cube.
#' @param ... Additional parameters.
#'
#' @return Data cube with the images arranged by cloud.
.gc_arrange_images <- function(cube, timeline, period, ...) {
UseMethod(".gc_arrange_images", cube)
}

#' @keywords internal
#' @noRd
#' @export
.gc_arrange_images.raster_cube <- function(cube, timeline, period, ...) {
# include the end of last interval
timeline <- c(
timeline,
Expand Down Expand Up @@ -44,6 +51,64 @@
return(cube)
}

#' @keywords internal
#' @noRd
#' @export
`.gc_arrange_images.mpc_cube_sentinel-1-grd` <- function(cube,
timeline,
period,
roi,
...) {
# dummy local variables to avoid warnings from tidyverse syntax
.x <- NULL

# pre-requisites
.check_that(nrow(cube) == 1,
local_msg = "cube must have one row",
msg = "invalid sentinel-1 cube")

# include the end of last interval
timeline <- c(
timeline,
timeline[[length(timeline)]] %m+% lubridate::period(period)
)

# generate Sentinel-2 tiles and intersects it with doi
tiles <- .s2tile_open(roi)
tiles <- tiles[.intersects(tiles, .roi_as_sf(roi)), ]

# prepare a sf object representing the bbox of each image in file_info
fi_bbox <- .bbox_as_sf(.bbox(
x = cube$file_info[[1]],
default_crs = cube$crs,
by_feature = TRUE
))

# create a new cube according to Sentinel-2 MGRS
cube_class <- .cube_s3class(cube)
cube <- tiles |>
dplyr::rowwise() |>
dplyr::group_map(~{
file_info <- .fi(cube)[.intersects({{fi_bbox}}, .x), ]
.cube_create(
source = .tile_source(cube),
collection = .tile_collection(cube),
satellite = .tile_satellite(cube),
sensor = .tile_sensor(cube),
tile = .x$tile_id,
xmin = .x$xmin,
xmax = .x$xmax,
ymin = .x$ymin,
ymax = .x$ymax,
crs = paste0("EPSG:", .x$epsg),
file_info = file_info
)
}) |>
dplyr::bind_rows()

.cube_set_class(cube, cube_class)
}

#' @title Create a cube_view object
#' @name .gc_create_cube_view
#' @keywords internal
Expand Down Expand Up @@ -171,13 +236,19 @@
unlink(path_db)
}

# use crs from tile if there is no crs in file_info
if ("crs" %in% names(.fi(cube))) {
file_info <- dplyr::select(cube, "file_info") |>
tidyr::unnest(cols = c("file_info"))
} else {
file_info <- dplyr::select(cube, "file_info", "crs") |>
tidyr::unnest(cols = c("file_info"))
}

# can be "proj:epsg" or "proj:wkt2"
crs_type <- .gc_detect_crs_type(.cube_crs(cube))
crs_type <- .gc_detect_crs_type(file_info$crs[[1]])

file_info <- dplyr::select(
cube, "file_info", "crs"
) |>
tidyr::unnest(cols = c("file_info")) |>
file_info <- file_info |>
dplyr::transmute(
fid = .data[["fid"]],
xmin = .data[["xmin"]],
Expand Down Expand Up @@ -320,9 +391,10 @@
#' @noRd
#' @param cube Data cube.
#' @param period ISO8601 time period.
#' @param extra_date_step Add an extra date in the end of timeline?
#'
#' @return a \code{vector} with all timeline values.
.gc_get_valid_timeline <- function(cube, period) {
.gc_get_valid_timeline <- function(cube, period, extra_date_step = FALSE) {
# set caller to show in errors
.check_set_caller(".gc_get_valid_timeline")

Expand Down Expand Up @@ -380,13 +452,10 @@
tl <- c(tl, date)
}

# timeline cube
tiles_tl <- suppressWarnings(sits_timeline(cube))

if (!is.list(tiles_tl)) {
tiles_tl <- list(tiles_tl)
# Add extra time step
if (extra_date_step) {
tl <- c(tl, tl[[length(tl)]] %m+% lubridate::period(period))
}

return(tl)
}

Expand Down Expand Up @@ -493,7 +562,8 @@
cube <- .gc_arrange_images(
cube = cube,
timeline = timeline,
period = period
period = period,
roi = roi
)

# start processes
Expand Down
Loading

0 comments on commit 8ed520e

Please sign in to comment.