I'm looking for some examples to do simple python scripts in QGIS. How would I do a buffer analysis on a dataset?

I can't seem to find much in the manual and QGIS python that quite matches the ESRI documentation.

4 Answers 4


You have different ways to get what you want by PyQGIS Console:

  1. Aragon's suggestion;
  2. by using QgsGeometryAnalyzer class:
from qgis.utils import iface
from qgis.analysis import QgsGeometryAnalyzer 
mc = iface.mapCanvas() 
layer = mc.currentLayer()
QgsGeometryAnalyzer().buffer(layer, "path_to/output.shp", 500, False, False, -1)
  1. by using Sextante class:
from sextante.core.Sextante import Sextante
Sextante.runalg("ftools:fixeddistancebuffer","input_path.shp", False, 500, 5, True, "output_path_buffer.shp")

To get the sextante parameters type Sextante.alghelp("ftools:fixeddistancebuffer") in PyQGIS Console.

Hope this helps !


if you want basic code, you can try:

#Don't forget to Toggle Editing

lyr = qgis.utils.iface.activeLayer()
provider = lyr.dataProvider()
feat= QgsFeature()
alls = provider.attributeIndexes()

while provider.nextFeature(feat):
    buff = feat.geometry().buffer(5,2)
    lyr.dataProvider().changeGeometryValues({feat.id(): buff})
  • Thanks - is QgsFeature the layer name or should it include an absolute path? And buffer(5,2) is that distance?
    – GIS Danny
    Commented Nov 16, 2012 at 15:49
  • 1
    you can also do this with a for loop and you also can avoid selecting the attributes if not needed gist.github.com/4094707
    – Nathan W
    Commented Nov 17, 2012 at 10:25
  • 1
    @GISDanny QgsFeature is a container class for the a feature e.g. attribute and geometry in QGIS. The layer is the qgis.utils.iface.activeLayer() bit, that will use the currently active layer in QGIS.
    – Nathan W
    Commented Nov 17, 2012 at 12:21
  • 1
    Is it possible to specify units when buffering in Python? I keep trying to buffer a point layer I believe I have set to a CRS with units in feet but the buffers I am getting are huge- the point layer was originally imported from csv with lat long cords but when I created a vector layer out of it I specified a local system. Something is obviously wrong.
    – kflaw
    Commented Mar 9, 2015 at 11:43

Just a little thing to add to the last reply.

To search for a SEXTANTE algorithm about a given topic, use Sextante.alglist(). For instance, in the case of searching for something containing "buffer", you would do

>>> from sextante.core.Sextante import Sextante
>>> Sextante.alglist("buffer")

And you would get:

Grid Buffer------------------------------------------>saga:gridbuffer
Grid Proximity Buffer-------------------------------->saga:gridproximitybuffer
Shapes Buffer---------------------------------------->saga:shapesbuffer
Threshold Buffer------------------------------------->saga:thresholdbuffer
Fixed distance buffer-------------------------------->ftools:fixeddistancebuffer
Variable distance buffer----------------------------->ftools:variabledistancebuffer
r.buffer - Creates a raster map layer showing buffer zones surrounding cells that contain non-NULL category values.--->grass:r.buffer
v.buffer.column - Creates a buffer around features of given type.--->grass:v.buffer.column
v.buffer.distance - Creates a buffer around features of given type.--->grass:v.buffer.distance

That way, you can find the name of the algorithm to call (ftools:fixeddistancebuffer, in the example proposed in the reply above)

You can turn your script into a new algorithm in SEXTANTE. The SEXTANTE documentation has detailed information about that.

  • That a great tip worth mentioning on your sextante blog. I was looking for this yesterday.
    – underdark
    Commented Nov 17, 2012 at 8:38

Have a look at the PyQGIS Cookbook.

Follow the example on how to iterate over a vector layer. Accessing the geometry, you can apply the buffer() method. See also the QGIS API: http://www.qgis.org/api/classQgsGeometry.html#a98208752e1beb1a5d3a7eedffbfdb2e4


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.