2

I have a line vector layer with a field name of "buffer_distance". I want create a dissolved buffer layer based on it's field. I write below code:

from qgis.utils import iface
from qgis.analysis import QgsGeometryAnalyzer 
mc = iface.mapCanvas() 
layer = mc.currentLayer()
Distance_Field = layer.fieldNameIndex("buffer_distance")
QgsGeometryAnalyzer().buffer(layer, "/tmp/buffer.shp", False, True, Distance_Field)

when run this code in QGIS 2.18 python console, It creates a new shapefile with all table fields of input layer but there is not any feature. What is problem?

asked Oct 24, 2017 at 17:52

1 Answer 1

3

You have several issues in your code.

  1. Name field too long.
  2. You don't need index name field as parameter. You need attribute value.
  3. Wrong parameters number and wrong order.

So, following code works as expected:

from qgis.utils import iface
from qgis.analysis import QgsGeometryAnalyzer 
mc = iface.mapCanvas() 
layer = mc.currentLayer()
feat = layer.getFeatures().next()
att = feat.attribute('buf_dist')
QgsGeometryAnalyzer().buffer(layer,
 "/tmp/buffer.shp",
 att,
 False,
 False,
 -1)

After running it at Python Console of QGIS, I got desired buffer (retrieved from "/tmp/buffer.shp"):

enter image description here

as it can see at above image.

Editing Note:

In this case I would use QgsGeometry class because it also has a buffer method. Following code does the work:

mc = iface.mapCanvas() 
layer = mc.currentLayer()
feats = [ feat for feat in layer.getFeatures() ]
buffers = [ feat.geometry().buffer(feat.attribute('buf_dist'), -1).exportToWkt()
 for feat in feats ]
epsg = layer.crs().postgisSrid()
uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer""&index=yes"
mem_layer = QgsVectorLayer(uri,
 'buffers',
 'memory')
prov = mem_layer.dataProvider()
feats = [ QgsFeature() for i in range(len(buffers)) ]
for i, feat in enumerate(feats):
 feat.setAttributes([i])
 feat.setGeometry(QgsGeometry.fromWkt(buffers[i]))
prov.addFeatures(feats)
QgsMapLayerRegistry.instance().addMapLayer(mem_layer) 

I tried it out with line layer of following image:

enter image description here

and it works as expected.

answered Oct 24, 2017 at 18:57
5
  • Hi @xunilk, I used your code but it created equal buffer size for each feature while in my field there are different values. Commented Oct 24, 2017 at 19:52
  • It seems "att" in QgsGeometryAnalyzer is first value that used to create buffer size for all features. Commented Oct 24, 2017 at 20:19
  • 1
    In this case I would use QgsGeometry class because it also has a buffer method. Please, see my Editing Note. Commented Oct 24, 2017 at 20:58
  • in second code Is it possible to dissolve buffers? Commented Oct 25, 2017 at 3:51
  • 1
    Yes, it's possible. You can use gdalogr:dissolvepolygons, qgis:dissolve or grass:v.dissolve processing methods. I recommend qgis:dissolve because it has less parameters. Commented Oct 25, 2017 at 13:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.