5

I am trying to automate buffering of objects in QGIS using Python. The function below using QgsGeometryAnalyzer works but the output is blocky. I understand that the smoothness of the buffer edge can be improved by increasing the number segments per quarter but i am unsure of how to do this using Python or even if it is possible.

This note and the linked github pull request suggests not.

Is there an alternate method i am missing that would allow me to increase the number of segments and so the smoothness of the buffer object?

QgsGeometryAnalyzer().buffer(LayerToBeBuffered, "Output.shp", 2000, False, False, -1)

Blocky buffer object output from QgsGeometryAnalyzer

2
  • 2
    You could also use the Processing algorithm qgis:fixeddistancebuffer which includes a parameter for segments :)
    – Joseph
    Commented Jun 29, 2017 at 13:40
  • 1
    Beautiful. I amended to; import processing processing.runalg("qgis:fixeddistancebuffer", LayerToBeBuffered, 2000, 30, False, "Output.shp")
    – benj
    Commented Jun 29, 2017 at 16:23

1 Answer 1

5

You can use the buffer() method. It applies to a geometry object. For example, using the following code:

layer = iface.activeLayer() # for example
for feat in layer.getFeatures():
    geom = feat.geometry()
    buffered_geom = geom.buffer(200, 30) # this is the relevant part

you will create a buffer around the current feature with a radius of 200 (expressed in the units of measurement of the project) and composed of 30 segments.

2
  • I appreciate this would achieve a buffered object but would do so within the existing layer. I need the buffered object to be in a separate layer as with in the original snippet.
    – benj
    Commented Jun 29, 2017 at 16:25
  • @benj you asked for a method and I answered mentioning a method. Instead, if you want to create the object in a new layer, you may follow the suggestion by Joseph (the qgis:fixeddistancebuffer algorithm works using the buffer() method).
    – mgri
    Commented Jun 29, 2017 at 17:10

Your Answer

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

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