How to Buffer Any Layer in QGIS Using a Python Script

QGIStutorialGIStree crowndrone mappingPython
How to Buffer Any Layer in QGIS Using a Python Script

Buffering a layer manually in QGIS means navigating through the Processing Toolbox, setting parameters, and then loading the output. This script does all of that in one run from the Python Console.

What it does

The buffer_active_layer.py script takes whatever layer is currently selected (active) in the Layers panel and expands every feature outward by a fixed radius in metres. The result is saved as a GeoJSON alongside the input file and automatically loaded into your QGIS project.

This is useful for:

  • Defining a zone of influence around trees (e.g. canopy spread radius)
  • Adding a setback distance around polygon boundaries
  • Creating proximity rings around survey points

What you need

  • QGIS 3.28 or newer
  • A point or polygon layer loaded into QGIS and set as the active layer
  • The buffer_active_layer.py script from the GIS Utilities Pack

Steps

  1. Load your vector layer into QGIS and click it in the Layers panel to make it active.
  2. Open Plugins → Python Console → Show Editor.
  3. Paste the script contents into the editor.
  4. Find the line at the bottom that calls the buffer function and set buffer_radius (in metres) to what you need — for example, buffer_radius=1.5 for a 1.5 m buffer around each tree.
  5. Click Run.

The output file {input_name}_buffered.geojson is saved in the same directory as the input and added to the project immediately.

A common real-world use: buffering individual tree points into crown-sized polygons before sampling height or NDVI per tree — one step in a complete per-tree health workflow on a real coconut plantation survey. The active layer here is a point layer — one digitized point per tree trunk:

A point layer with one digitized point per coconut palm trunk, before buffering — the active layer this script needs

Running the script turns each point into a crown-sized circle:

Each tree point buffered into a crown-sized circle, ready to be used as a sampling zone

Configurable parameters

ParameterDefaultWhat it changes
buffer_radiusset at call siteBuffer distance in metres
LOG_PATH~/Documents/QGIS_Projects/buffer_log.txtWhere the run log is written

Output

A new GeoJSON file with all features buffered outward. The geometry type changes to polygon (even for point inputs — each point becomes a circle). The original attributes are preserved.

Get the script

buffer_active_layer.py is part of the GIS Utilities Pack — four everyday QGIS automation scripts for $25 USD.