17

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

12

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 !

11

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}) 
4
  • Thanks - is QgsFeature the layer name or should it include an absolute path? And buffer(5,2) is that distance? 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 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. 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. Commented Mar 9, 2015 at 11:43
9

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.

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

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

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.