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.
You have different ways to get what you want by PyQGIS Console:
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)
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.
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