|
1 | 1 | extracted <- file.exists("longlat_points_dem.parquet")
|
2 | 2 | Sys.setenv("AWS_NO_SIGN_REQUEST" = "YES") ## shouldn't be necessary, added in desperation during ghactions testing
|
| 3 | + |
| 4 | + |
3 | 5 | if (!extracted) {
|
4 | 6 | track <- nanoparquet::read_parquet("longlat_points.parquet")
|
5 | 7 | library(xml2)
|
6 | 8 | library(gdalraster)
|
7 |
| -dsn <- "/vsicurl/https://raw.githubusercontent.com/mdsumner/rema-ovr/main/REMA-2m_dem_ovr.vrt" |
8 |
| -##dsn <- "/vsicurl/https://opentopography.s3.sdsc.edu/raster/COP30/COP30_hh.vrt" |
| 9 | +#dsn <- "/vsicurl/https://raw.githubusercontent.com/mdsumner/rema-ovr/main/REMA-2m_dem_ovr.vrt" |
| 10 | +dsn <- "/vsicurl/https://opentopography.s3.sdsc.edu/raster/COP30/COP30_hh.vrt" |
9 | 11 | url <- gsub("/vsicurl/", "", dsn)
|
10 | 12 | xml <- read_xml(url)
|
11 | 13 | dst <- xml |> xml_find_all(".//DstRect")
|
@@ -58,29 +60,41 @@ tile <- unlist(lapply(geos::geos_strtree_query(tree, wk::xy(xy[,1, drop = TRUE],
|
58 | 60 | extract_pt <- function(x) {
|
59 | 61 | dsn <- x$dsn[1]
|
60 | 62 | bbox <- x$bbox[[1]]
|
| 63 | + if (x$tile[1] == 0) return(rep(NA_real_, length(x$X))) |
61 | 64 | pts <- cbind(x$X, x$Y)
|
62 | 65 | tf <- tempfile(fileext = ".vrt")
|
63 |
| - #bbbbb <- paste0(bbox, collapse = ",") |
64 |
| - #dsn <- glue::glue("vrt://{dsn}?projwin={bbbbb}") |
| 66 | + |
65 | 67 | translate(dsn, tf, cl_arg = c("-projwin", unname(bbox[c(1, 4, 3, 2)])), quiet = TRUE)
|
66 |
| - pixel_extract(new(GDALRaster, tf), pts) |
| 68 | + ds <- new(GDALRaster, tf) |
| 69 | + on.exit(ds$close(), add = TRUE) |
| 70 | + pixel_extract(ds, pts) |
67 | 71 | }
|
68 | 72 |
|
69 | 73 |
|
70 | 74 | v <- vector("list", length(unique(tile)))
|
71 | 75 | ## create the payload
|
72 | 76 | for (i in seq_along(v)) {
|
73 |
| - tib <- tibble::tibble(dsn = dsn, X = xy[tile == unique(tile)[i],1 , drop = TRUE], |
74 |
| - Y = xy[tile == unique(tile)[i],2 , drop = TRUE], |
75 |
| - bbox = list(bb[unique(tile)[i],, drop = TRUE])) |
| 77 | + tile_index <- unique(tile)[i] |
| 78 | + X <- xy[!is.na(tile) & tile == tile_index,1 , drop = TRUE] |
| 79 | + Y <- xy[!is.na(tile) & tile == tile_index,2 , drop = TRUE] |
| 80 | + ## when we don't have a tile call it zero |
| 81 | + if (is.na(tile_index)) { |
| 82 | + tile_index <- 0 |
| 83 | + Y <- X <- rep(NA, sum(is.na(tile))) |
| 84 | + print(sum(is.na(tile))) |
| 85 | + } |
| 86 | + tib <- tibble::tibble(dsn = dsn, X = X, |
| 87 | + Y = Y, |
| 88 | + bbox = list(bb[tile_index,, drop = TRUE]), |
| 89 | + tile = tile_index) |
76 | 90 | v[[i]] <- tib
|
77 | 91 | }
|
78 | 92 |
|
79 | 93 | options(parallelly.fork.enable = TRUE, future.rng.onMisuse = "ignore")
|
80 |
| -library(furrr); plan(multicore) |
81 |
| - |
| 94 | +library(furrr); |
| 95 | +plan(multicore) |
82 | 96 | v1 <- future_map(v, extract_pt)
|
83 |
| - |
| 97 | +plan(sequential) |
84 | 98 |
|
85 | 99 | # for (i in seq_along(v)) {
|
86 | 100 | # v[[i]] <- extract_pt(dsn, bb[unique(tile)[i],, drop = TRUE], xy[tile == unique(tile)[i], , drop = FALSE])
|
|
0 commit comments