dsn <- "/vsicurl/https://data.source.coop/vizzuality/hfp-100/hfp_2021_100m_v1-2_cog.tif"
dsn <- basename(dsn)
info <- vapour::vapour_raster_info(dsn)
library(grout)
g <- grout(info$dimension, info$extent, blocksize = c(2048, 2048))
index <- tile_index(g)
## also create the extents that we want
tiles <- tibble::tibble(dsn = sprintf("vrt://%s?srcwin=%i,%i,%i,%i", dsn,
index$offset_x, index$offset_y, index$ncol, index$nrow),
xmin = index$xmin, xmax = index$xmax, ymin = index$ymin, ymax = index$ymax)
## so here's my thinking, each row in this tibble has a tile we set up and an extent for the vector read
## (I think the vector read is all-inclusive but I will check)
for (i in 1:nrow(tiles)) {
s <- dplyr::sample_n(tiles, 1L)
r <- rast(s$dsn)
vcrs <- crs(vect("pad-mobi.parquet", proxy = TRUE))
## project the extent so we can window read the vector
ex <- ext(project(ext(r), to = vcrs, from = crs(r)))
v <- vect("pad-mobi.parquet", extent = ex)
if (nrow(v) > 0) {
result <- terra::extract(r, project(v, crs(r)))
stop()
}
}
It took until i = 13 for me to get a result, that looks like this:
str(result)
'data.frame': 384481 obs. of 2 variables:
$ ID : num 1 2 3 4 5 6 7 8 9 10 ...
$ hfp_2021_100m_v1-2_cog: int NA NA NA NA NA NA NA NA NA NA ...
> str(na.omit(result))
'data.frame': 377600 obs. of 2 variables:
$ ID : num 39 39 39 39 39 39 39 39 39 39 ...
$ hfp_2021_100m_v1-2_cog: int 4435 4432 4432 4408 4336 4222 4072 3895 3715 4012 ...
- attr(*, "na.action")= 'omit' Named int [1:6881] 1 2 3 4 5 6 7 8 9 10 ...
..- attr(*, "names")= chr [1:6881] "1" "2" "3" "4" ...
the sample tile looks like this:
nlist(s)
dsn
"vrt://hfp_2021_100m_v1-2_cog.tif?srcwin=79872,32768,2048,2048"
xmin
"-10052895.696"
xmax
"-9848095.696"
ymin
"5343378.852"
ymax
"5548178.852"
here's what one of the tiles looks like
raster extent
vector extent (so we are getting inclusive window read)