In the r-package terra
there is the function terra::extend
. To my understanding it just expands the raster with no data values on each side right?
I wanted to do something similar, but using a stars
object. My approach kind of looked like this:
# add.y and add.y are scalars between 0 and 1 to extend the number of cols/rows by e.g. add.x percent
grid_vals = star[[1]]
ncol = ncol(grid_vals)
nrow = nrow(grid_vals)
new_cols = floor(ncol * add.x); if(!new_cols %% 2 == 0) new_cols = new_cols -1
new_rows = floor(nrow * add.y); if(!new_rows %% 2 == 0) new_rows = new_rows -1
# new columns
for (i in 1:new_cols) {
if (i %% 2 == 0) {
grid_vals = cbind(grid_vals, rep(NA, nrow))
} else{
grid_vals = cbind(rep(NA, nrow), grid_vals)
}
}
# new rows
cols = ncol(grid_vals)
for (j in 1:new_rows) {
if (j %% 2 == 0) {
grid_vals = rbind(grid_vals, rep(NA, cols))
} else{
grid_vals = rbind(rep(NA, cols), grid_vals)
}
}
I can create a new matrix with this. However, I do not know how I could assign this to my old stars
-object that has a different extent
and bounding-box
etc...