Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 3 down vote accepted

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

share|improve this answer
    
This link doesn't actually work for me. –  GIS Danny Nov 16 '12 at 15:50
    
I think it's a temporary problem with qgis.org. I've just tried to access the main website with no joy. –  blackthorn Nov 16 '12 at 17:27

if you wanna a basic code, you can check out following code:

#Dont forget to Toggle Editting

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})

i hope it helps you...

share|improve this answer
    
Thanks - is QgsFeature the layer name or should it include an absolute path? And buffer(5,2) is that distance? –  GIS Danny Nov 16 '12 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 Nov 17 '12 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 Nov 17 '12 at 12:21

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 !

share|improve this answer

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.

share|improve this answer
    
That a great tip worth mentioning on your sextante blog. I was looking for this yesterday. –  underdark Nov 17 '12 at 8:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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