|
37 | 37 | fi <- .fi_filter_interval(
|
38 | 38 | fi = .fi(tile),
|
39 | 39 | start_date = timeline[[1]],
|
40 |
| - end_date = timeline[[length(timeline)]] |
| 40 | + end_date = timeline[[length(timeline)]] - 1 |
41 | 41 | )
|
42 | 42 | groups <- cut(
|
43 | 43 | x = .fi_timeline(fi),
|
|
70 | 70 | #' @return a data cube with assets of the same period (file ID)
|
71 | 71 | .reg_merge_asset <- function(asset, res, roi, output_dir) {
|
72 | 72 | # Get band conf missing value
|
73 |
| - band_conf <- .conf_eo_band( |
74 |
| - source = "MPC", |
75 |
| - collection = "SENTINEL-1-GRD", |
76 |
| - band = asset[["asset"]] |
77 |
| - ) |
| 73 | + band_conf <- .tile_band_conf(asset, band = asset[["asset"]]) |
78 | 74 | # Prepare output file name
|
79 | 75 | out_file <- .file_eo_name(
|
80 | 76 | tile = asset,
|
|
97 | 93 | }
|
98 | 94 |
|
99 | 95 | # Create template based on tile metadata
|
100 |
| - block <- list(ncols = floor((.xmax(asset) - .xmin(asset)) / res), |
101 |
| - nrows = floor((.ymax(asset) - .ymin(asset)) / res)) |
102 |
| - bbox <- list(xmin = .xmin(asset), |
103 |
| - xmax = .xmin(asset) + .ncols(block) * res, |
104 |
| - ymin = .ymax(asset) - .nrows(block) * res, |
105 |
| - ymax = .ymax(asset), |
106 |
| - crs = .crs(asset)) |
| 96 | + if (!.has(roi)) { |
| 97 | + roi <- .bbox_as_sf(.tile_bbox(asset)) |
| 98 | + } |
| 99 | + roi_bbox <- .bbox(sf::st_intersection( |
| 100 | + x = .roi_as_sf(roi, as_crs = .crs(asset)), |
| 101 | + y = .bbox_as_sf(.bbox(asset)) |
| 102 | + )) |
| 103 | + block <- list(ncols = floor((.xmax(roi_bbox) - .xmin(roi_bbox)) / res), |
| 104 | + nrows = floor((.ymax(roi_bbox) - .ymin(roi_bbox)) / res)) |
| 105 | + bbox <- list(xmin = .xmin(roi_bbox), |
| 106 | + xmax = .xmin(roi_bbox) + .ncols(block) * res, |
| 107 | + ymin = .ymax(roi_bbox) - .nrows(block) * res, |
| 108 | + ymax = .ymax(roi_bbox), |
| 109 | + crs = .crs(roi_bbox)) |
107 | 110 | out_file <- .gdal_template_block(
|
108 | 111 | block = block,
|
109 | 112 | bbox = bbox,
|
|
128 | 131 | update_bbox = TRUE
|
129 | 132 | )
|
130 | 133 | }
|
131 |
| - |
132 |
| -.reg_s2tile_convert <- function(cube, roi) { |
133 |
| - # TODO: check cube |
| 134 | +#' @title Convert a SAR cube to MGRS tiling system |
| 135 | +#' @name .reg_s2tile_convert |
| 136 | +#' @noRd |
| 137 | +#' @description Produces the metadata description for a data cube |
| 138 | +#' to be produced by converting SAR data to MGRS tiling system |
| 139 | +#' @param cube SAR data cube |
| 140 | +#' @param roi Region of interest |
| 141 | +#' @param tiles List of MGRS tiles |
| 142 | +#' @return a data cube of MGRS tiles |
| 143 | +.reg_s2tile_convert <- function(cube, roi = NULL, tiles = NULL){ |
| 144 | + UseMethod(".reg_s2tile_convert", cube) |
| 145 | +} |
| 146 | +#' @noRd |
| 147 | +#' @export |
| 148 | +#' |
| 149 | +.reg_s2tile_convert.grd_cube <- function(cube, roi = NULL, tiles = NULL) { |
134 | 150 |
|
135 | 151 | # generate Sentinel-2 tiles and intersects it with doi
|
136 |
| - tiles <- .s2tile_open(roi) |
137 |
| - tiles <- tiles[.intersects(tiles, .roi_as_sf(roi)), ] |
| 152 | + tiles_mgrs <- .s2tile_open(roi, tiles) |
138 | 153 |
|
139 | 154 | # prepare a sf object representing the bbox of each image in file_info
|
140 | 155 | fi_bbox <- .bbox_as_sf(.bbox(
|
|
145 | 160 |
|
146 | 161 | # create a new cube according to Sentinel-2 MGRS
|
147 | 162 | cube_class <- .cube_s3class(cube)
|
148 |
| - cube <- tiles |> |
| 163 | + cube <- tiles_mgrs |> |
149 | 164 | dplyr::rowwise() |>
|
150 | 165 | dplyr::group_map(~{
|
151 | 166 | file_info <- .fi(cube)[.intersects({{fi_bbox}}, .x), ]
|
|
172 | 187 | cube_class <- c(cube_class[1], "sar_cube", cube_class[-1])
|
173 | 188 | .cube_set_class(cube, cube_class)
|
174 | 189 | }
|
| 190 | +#' @noRd |
| 191 | +#' @export |
| 192 | +#' |
| 193 | +.reg_s2tile_convert.rtc_cube <- function(cube, roi = NULL, tiles = NULL) { |
| 194 | + |
| 195 | + # generate Sentinel-2 tiles and intersects it with doi |
| 196 | + tiles_mgrs <- .s2tile_open(roi, tiles) |
| 197 | + |
| 198 | + # create a new cube according to Sentinel-2 MGRS |
| 199 | + cube_class <- .cube_s3class(cube) |
| 200 | + |
| 201 | + cube <- tiles_mgrs |> |
| 202 | + dplyr::rowwise() |> |
| 203 | + dplyr::group_map(~{ |
| 204 | + # prepare a sf object representing the bbox of each image in file_info |
| 205 | + cube_crs <- dplyr::filter(cube, .data[["crs"]] == .x$crs) |
| 206 | + fi_bbox <- .bbox_as_sf(.bbox( |
| 207 | + x = .fi(cube_crs), |
| 208 | + default_crs = .crs(cube_crs), |
| 209 | + by_feature = TRUE |
| 210 | + )) |
| 211 | + browser() |
| 212 | + file_info <- .fi(cube_crs)[.intersects({{fi_bbox}}, .x), ] |
| 213 | + .cube_create( |
| 214 | + source = .tile_source(cube_crs), |
| 215 | + collection = .tile_collection(cube_crs), |
| 216 | + satellite = .tile_satellite(cube_crs), |
| 217 | + sensor = .tile_sensor(cube_crs), |
| 218 | + tile = .x[["tile_id"]], |
| 219 | + xmin = .xmin(.x), |
| 220 | + xmax = .xmax(.x), |
| 221 | + ymin = .ymin(.x), |
| 222 | + ymax = .ymax(.x), |
| 223 | + crs = paste0("EPSG:", .x[["epsg"]]), |
| 224 | + file_info = file_info |
| 225 | + ) |
| 226 | + }) |> |
| 227 | + dplyr::bind_rows() |
| 228 | + |
| 229 | + # Filter non-empty file info |
| 230 | + cube <- .cube_filter_nonempty(cube) |
| 231 | + |
| 232 | + # Finalize customizing cube class |
| 233 | + cube_class <- c(cube_class[1], "sar_cube", cube_class[-1]) |
| 234 | + .cube_set_class(cube, cube_class) |
| 235 | +} |
175 | 236 |
|
0 commit comments