Open Bartesto opened 1 year ago
raster::rasterize
also has that option (but is especially slow). With terra::rasterize
you can do "sum=TRUE", but I will add min, max and mean (and use the "fun=" argument for that).
Here is another approach (that I would not want to use with large datasets)
u <- union(p)
tu <- t(data.frame(u))
i <- tu==0
tu[tu==1] <- p$year[tu * (1:nrow(tu))]
tu[i] <- NA
u$year <- apply(tu, 2, max, na.rm=TRUE)
template <- rast(p, res = 1)
r <- rasterize(u, template, field = "year")
Thanks @rhijmans. Really interesting to see that work around and appreciate you adding to the rasterize function. I'm weaning some folks off long running vector work in a GIS and this will go a very long way.
Regards Bart
Hello @rhijmans ,
Will there be a possibility to use a custom function to rasterize polygons in the future?
A typical example would be calculating rasters of species richness from IUCN range maps, like the good old example of raster's rasterize:
fun=function(x, ...) {length(unique(na.omit(x)))}
Switching from raster to terra in teaching practicals has made this task more complex to explain than in raster. It requires pre-processing of polygons which makes the process much slower in terra's rasterize than with raster's raterize. See for example how @damariszurell handled it by rasterizing all polygons individually here. In my own practicals I opted to union the polygons beforehand and use the sum function (an example in section #3.3 here).
@Farewe: you can use fun="sum"
or that. I have now also added fun="count"
to make that intent clearer.
library(terra)
v <- vect(system.file("ex/lux.shp", package="terra"))
v <- buffer(v, 10000)
r <- rast(system.file("ex/elev.tif", package="terra"))
x <- rasterize(v, r, fun="sum")
# now you can also do
y <- rasterize(v, r, fun="count")
Hi,
I am wondering if there is a way to apply a function to rasterization when working on overlapping polygons?
Context. I have a large multi-polygon dataset where each polygon represents the footprint of a bushfire and is attributed with the year it occurred. The dataset contains records back to 1937 for the whole of Western Australia. One task is to calculate the year since an area (or the whole State) last burnt.
I rasterize the vectors but I need to order them first by the year for this to create the correct output and the ordering imposes a time penalty when compared to something like
fasterize::fasterize
which can accept a function such as "max" which does the job.Here's a toy example.
What I am after can be created by:
or
Either of these methods when plotted produce the below which has the rasterized polygons layered in descending order, with 2023 (green) on top, followed by 2021 then 1990. With multi-polygon datasets in the order of 1000's + polygons
rasterize
is half as fast when ordering first and I note that a function can only be applied to a point vector dataset.Thanks Bart