There are multiple ways of rasterizing in R. This is a whirlwind tour with a tiny dataset to make sure
an attempt at a more abstracted approach is in alignment with more standard "spatial-ways" of doing things.
I'm not going to explain or excuse the code below, I just want to record it for now.
polstemp <- tempfile(fileext = "fgb")
pols <- silicate::minimal_mesh
sf::write_sf(pols, polstemp, driver = "FlatGeobuf")
## wrap everything in a function so we have just one parameter
fun <- function(n) {
library(silicate)
library(vaster)
library(controlledburn)
sc <- SC0(minimal_mesh)
e0 <- c(apply(sc_vertex(sc), 2, range))
dxy <- diff(e0)[c(1, 3)] / n
res <- round(max(dxy), 3)
e <- vaster::buffer_extent(e0 + c(-1, 1, -1, 1) * rep(dxy, each = 2), res)
dm <- as.integer(diff(e)[c(1, 3)]/res)
print(dm)
rr <- raster::raster(rast(ext(e), ncols = dm[1], nrows = dm[2]))
tr <- rasterize(vect(polstemp), rast(rr))
fr <- fasterize::fasterize(pols, rr)
plot(tr, col = scales::alpha("black", .5))
#par(xpd = NA)
#plot.new(); plot.window(e[1:2], e[3:4], asp = 1)
#plot_extent(e, asp = 1, add = TRUE)
plot(x, add = T)
abline(v = x_corner(dm, e),
h = y_corner(dm, e),
col = "darkgrey")
cb <- controlledburn:::burn_polygon(pols, extent = e,
dimension = dm)
## our index is triplets of start,end,line where the polygon edge was detected -
## this essentially an rle by scanline of start,end polygon coverage
index <- matrix(unlist(cb, use.names = F), ncol = 4L, byrow = TRUE) + 1 ## plus one because 0-index internally
#cb <- controlledburn:::burn_polygon(minimal_mesh, extent = e, dimension = dm)
#str(cb)
## what does controlleburn know?
index <- matrix(unlist(cb, use.names = F), ncol = 4L, byrow = TRUE) + 1 ## plus one because 0-index internally
x0 <- x_from_col(dm, e, index[,1L])
x1 <- x_from_col(dm, e, index[,2L])
y0 <- y_from_row(dm, e, index[,3L])
y1 <- y_from_row(dm, e, index[,3L])
segments(x0, y0, x1, y1)
if (n < 34) {
points(x0, y0, pch = "+", cex = .7)
points(x1, y1, pch = "x", cex = .7)
points(xyFromCell(fr, seq_len(prod(dm))[!is.na(values(fr))]), cex = 2)
} else {
points(x0, y0, pch = 19, cex = .25, col = "green")
points(x1, y1, pch = 19, cex = .25, col = "red")
}
invisible(NULL)
}
i <- 29
i <- i - 1
par(bg = "grey70")
fun(5)
Needs some fixing for the output extent
title: "Minute Detail Rasterizing"
author: "Michael Sumner"
format: html
editor: visual
editor_options:
chunk_output_type: console
There are multiple ways of rasterizing in R. This is a whirlwind tour with a tiny dataset to make sure
an attempt at a more abstracted approach is in alignment with more standard "spatial-ways" of doing things.
I'm not going to explain or excuse the code below, I just want to record it for now.
The parameter
nis the number of pixels we want in the plane. So, first let's do a sensible default, a raster that is 10x10.What do we see here? The yellow is good old
terra::rasterize(), a single pixel raster in which was detected our two polygons