Update: see here for a model to automatically run this workflow.
Create a vector grid with the same extent and resolution as the raster you intend to create.
Calculate the percentage (area) each grid cell intersects with the polygons to be rasterized - see screenshot 1. Use filed calculator to create a new field with this value. To calculate the percentage, you can use this expression (replace poly
with the name of your polygon layer):
area (
intersection (
$geometry,
collect_geometries(
overlay_intersects(
'poly',
$geometry
)
)
)
)
/area ($geometry) * 100
- Use this percentage value for the raster value to be created: from 100 (complete interesection) to 0 (no intersection at all) wit in-between values based on intersection percentage, see screenshot 2.
For rasterize, use
Menu Raster / Conversion / Rasterize (vector to raster)
- see screenshot below for settings: set the grid and the attribute field with the calculated percentage (named pixelval
in my case) is input and set Output raster size units
to the same size as your grid and Output size
to the extent of your grid-layer (or your polygon-layer, because the extent of the grid should be the same as the polygon, see step 1). Be aware: the screenshot shows a layer in EPSG:3857
, this normally is not ideal for processing, just due to creation of a fast test dataset for visualization.
Screenshot 1: Polygons in yellow, grid in blue. The grid-cells are labeled (for visualization purpose) with the expression from above + a round() function:
Screenshot 2: And that's how the final raster looks like:
Screenshot 3: setting of the rasterize dialog:
Edit/addition
I created a model for this workflow that can be downloaded here (update: improved version here). You have to adapt the values and make sure that the project CRS is the same as the CRS of the input polygon layer you load. The expression from step 2 refers to the layer named polygon
, so the input layer should be named like this.
Screenshot of the model: