From 5e821d2a5012a60762c3f4a7a6cdddcb2abffb9c Mon Sep 17 00:00:00 2001 From: Felipe Carlos Date: Fri, 31 Jan 2025 17:38:49 -0300 Subject: [PATCH 1/2] improve regularize tile management performance --- R/api_regularize.R | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/R/api_regularize.R b/R/api_regularize.R index c2eb1d6ee..b3ddf029f 100644 --- a/R/api_regularize.R +++ b/R/api_regularize.R @@ -190,9 +190,30 @@ tiles_filtered <- .grid_filter_tiles( grid_system = grid_system, tiles = tiles, roi = roi ) + tiles_filtered_crs <- unique(tiles_filtered[["crs"]]) - # save original cube classes - cube_class <- class(cube) + # bind all files + cube_fi <- dplyr::bind_rows(cube[["file_info"]]) + + # get reference files of each ``fid`` + cube_fi_unique <- dplyr::distinct( + .data = cube_fi, + .data[["fid"]], .data[["xmin"]], + .data[["ymin"]], .data[["xmax"]], + .data[["ymax"]], .data[["crs"]] + ) + + # if unique crs pre-calculate bbox + fi_bbox <- NULL + + if (length(tiles_filtered_crs) == 1) { + # extract bounding box from files + fi_bbox <- .bbox_as_sf(.bbox( + x = cube_fi_unique, + default_crs = .crs(cube), + by_feature = TRUE + ), as_crs = tiles_filtered_crs) + } # redistribute data into tiles cube <- tiles_filtered |> @@ -200,15 +221,18 @@ dplyr::group_map(~{ # prepare a sf object representing the bbox of each image in # file_info - cube_fi <- dplyr::bind_rows(cube[["file_info"]]) - # extract bounding box from files - fi_bbox <- .bbox_as_sf(.bbox( - x = cube_fi, - default_crs = cube, - by_feature = TRUE - ), as_crs = .x[["crs"]]) + if (!.has(fi_bbox)) { + fi_bbox <- .bbox_as_sf(.bbox( + x = cube_fi_unique, + default_crs = .crs(cube), + by_feature = TRUE + ), as_crs = .x[["crs"]]) + } # check intersection between files and tile - file_info <- cube_fi[.intersects(fi_bbox, .x), ] + fids_in_tile <- cube_fi_unique[.intersects(fi_bbox, .x), ] + # get fids in tile + file_info <- cube_fi[cube_fi[["fid"]] %in% fids_in_tile[["fid"]],] + # create cube! .cube_create( source = .tile_source(cube), collection = .tile_collection(cube), From 606b6998590b94df9f7051f9ea34701d0af95196 Mon Sep 17 00:00:00 2001 From: Felipe Carlos Date: Fri, 31 Jan 2025 18:16:17 -0300 Subject: [PATCH 2/2] fix url parse --- R/api_cube.R | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/R/api_cube.R b/R/api_cube.R index f62944da6..f5c154a6b 100644 --- a/R/api_cube.R +++ b/R/api_cube.R @@ -1473,13 +1473,19 @@ NULL if (are_local_paths[[i]]) { return(path) } + + path_prefix <- "/vsicurl/" + path <- stringr::str_replace(path, path_prefix, "") + url_parsed <- .url_parse(path) + url_parsed[["path"]] <- paste0(path_prefix, url_parsed[["path"]]) + url_parsed[["query"]] <- utils::modifyList( - url_parsed[["query"]], - token_parsed[["query"]] + url_parsed[["query"]], token_parsed ) # remove the additional chars added by httr new_path <- gsub("^://", "", .url_build(url_parsed)) + new_path <- paste0(path_prefix, new_path) new_path }) file_info[["token_expires"]] <- strptime(