gdal_rasterize
What is gdal_rasterize?
gdal_rasterize converts vector features — points, lines, or polygons — into pixel values in a raster. It can create a new raster with an explicit extent and resolution, or burn features into an existing raster, updating pixels either with a constant value or with values taken from an attribute. This is the canonical tool for building masks, categorical rasters from polygon layers, and sparse point-density rasters.
gdal_rasterize [options] <src_datasource> <dst_filename>Commonly used options:
-b <band>— target band(s) in an existing raster-burn <value>— constant burn value (repeat for multiple bands)-a <attribute>— burn attribute from the vector source-3d— use Z coordinate as burn value-add— accumulate into existing values rather than overwrite-l <layer>/-sql <statement>/-where <expr>— source selection-te <xmin> <ymin> <xmax> <ymax>/-tr <xres> <yres>/-ts <xsize> <ysize>— extent, resolution, or pixel size for new outputs-tap— snap grid to target resolution-a_nodata <value>/-init <value>— NoData or initialisation value-at— enable "all-touched" mode so every pixel touched by geometry is burned-of <format>/-ot <type>/-co <NAME>=<VALUE>— output driver, type, creation options
When would you use gdal_rasterize?
Use gdal_rasterize whenever vector data needs to be represented as pixels. Typical jobs: creating a binary mask raster from a watershed boundary (gdal_rasterize -burn 1 -tr 10 10 -te ... boundary.gpkg mask.tif), building a categorical land-use raster from a polygon layer by burning a class-code attribute (-a class_code), or producing a sparse presence raster from point observations. Pair with gdal_proximity to convert that sparse raster into a distance-to-features surface.
For analysis that requires alignment with an existing raster, create the target with gdal_create or copy the reference grid, then burn into it: gdal_rasterize -a class_code -b 1 polygons.gpkg reference.tif. The -at (all-touched) mode is essential for thin features that would otherwise miss many intersecting pixels under the default centre-point rule.
FAQs
When should I use -at (all-touched) mode?
Enable -at whenever the source features are narrow relative to the pixel size — linear features like roads or streams, small polygons, or point data. Without -at, GDAL burns only pixels whose centres fall inside a geometry, which will miss most pixels crossed by a road or a one-metre-wide polygon on a 10-metre grid. The trade-off is that -at over-represents area for polygons.
How do I create a new raster sized to the vector extent?
Pass -te <xmin> <ymin> <xmax> <ymax> and either -tr <xres> <yres> or -ts <xsize> <ysize>. Snap the grid to a meaningful origin with -tap so outputs align with other rasters at the same resolution. Set -a_nodata and -init to control the initial fill of non-burned pixels.
Can I burn different values per feature?
Yes — use -a <attribute> and the source field's numeric value is burned for each feature. Combine with -sql or -where to subset first. For multi-band outputs, repeat -burn per band or use -a on several invocations with different -b targets.
Why does my rasterised polygon layer have stair-step artefacts?
Rasterisation is inherently discrete; boundaries are approximated to the nearest pixel. Use a finer target resolution if the artefact matters, or consider anti-aliased rendering in a cartographic stack. -at reduces omission but does not produce smooth edges — it just switches the rule for which pixels count as "inside".