Buffering in PyQGIS

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()
provider.select(alls)

while provider.nextFeature(feat):
    buff = feat.geometry().buffer(5,2)
    lyr.dataProvider().changeGeometryValues({feat.id(): buff})

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.angle--------------------------------------->grass:v.buffer.angl
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
v.buffer.minordistance------------------------------->grass:v.buffer.minordistance

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.