Tuesday 19 April 2016

qgis - How to adjust Python code from PyQGIS 2 to 3 to create a buffer on a shapefile


I am starting to learn PyQGIS and following tutorials online. However, I have QGIS 3.0 and most of the tutorials use QGIS version 2. I am trying to do a buffer on a point shapefile, but only have the code for Qgis 2. I have looked at the backwards incompatible changes (https://qgis.org/api/api_break.html) but cannot work out how to adjust the code. The 'GeometryAnalyzer' class, which contained the buffer function in Qgis 2 no longer exists, so I am not sure where to find it now. I am sure there is a simple solution..


The code for Qgis 2 as far as I know is as follows:



from qgis.analysis import QgsGeometryAnalzer
layer = iface.activeLayer()
QgsGeometryAnalyzer().buffer(layer, 'OutputFilePath/FileName.shp', 0.5, False, False, -1)

How can this be adjusted to Qgis 3?



Answer



The API changes page says:



QgsGeometryAnalyzer. Use the equivalent Processing algorithms instead.




processing.algorithmHelp("native:buffer") gives


processing.run('native:buffer', {"INPUT": lineLayer, "DISTANCE": 5, "OUTPUT": resultLayer})

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...