This function returns the barycentric weight for a grid of coordinates from a geographic raster.
bary_index(x, coords = NULL, grid = NULL, ...)
a 'RasterLayer' source
optional input coordinates
target 'RasterLayer', a target regular grid
ignored
RasterLayer
It's not as fast as raster::projectRaster()
(e.g. projectRaster(x, grid)
) but it
also accepts a coords
argument and so can be used for non-regular raster
reprojection.
'coords' may be 'NULL' or longitude, latitude in a 2-layer raster brick or stack as with
mesh_plot
.
library(raster)
#> Loading required package: sp
p_srs <- "+proj=stere +lat_0=-90 +lat_ts=-71 +datum=WGS84"
polar <- raster(extent(-5e6, 5e6, -5e6, 5e6), crs = p_srs, res = 25000)
etopo <- aggregate(etopo, fact = 4)
index <- bary_index(etopo, grid = polar)
ok <- !is.na(index$idx)
r <- setValues(polar, NA_integer_)
r[ok] <- colSums(matrix(values(etopo)[index$tri[, index$idx[ok]]], nrow = 3) * t(index$p)[, ok])
plot(r)