0

Code https://youtu.be/kvXbpN1fYmY

I'm using \ because this is on Windows 10 so the slashes are not the problem

layerName = 'Calapan_points'
outFn = r'C:\Users\imper\Desktop\XYbuffer_tool\test.shp'
bufferDist = 10 # meters

layers = QgsProject.instance().mapLayersByName(layerName)
layer = layers[0]
fields = layer.fields()
feats = layer.getFeatures()

writer = QgsVectorFileWriter(outFn, \
'UTF-8', \
fields, \
QgsWkbTypes.Polygon, \
layer.sourceCrs(), \
'ESRI Shapefile')

for feat in feats:
    geom  = feat.geometry()
    buff = geom.buffer(bufferDist, 5, QgsGeometry.CapFlat, \
    QgsGeometry.JoinStyleMiter, 2)
    feat.setGeometry(buff)
    writer.addFeature(feat)

iFace.addVectorLayer(outFn, '', 'ogr')
del(writer)

Error

NameError: name 'iFace' is not defined

Result https://i.stack.imgur.com/7EF2u.png

The buffer layer is created in the project folder but not loaded in the project itself. The buffer layer is blank with no buffer and uses the same fields and attributes of the point layer it should be buffering.

1 Answer 1

3

The name error is caused by a simple typo- it should be iface not iFace!

However you also have a few other problems. In your loop, you must create a new empty QgsFeature for each each feature in the source layer and set the buffered geometry to those. You also need to set the attributes from the source point features to the new buffer polygon features.

The following code works for me:

layerName = 'Calapan_points'
outFn = r'C:\Users\imper\Desktop\XYbuffer_tool\test.shp'
bufferDist = 10 # meters

layers = QgsProject.instance().mapLayersByName(layerName)
layer = layers[0]
fields = layer.fields()
feats = layer.getFeatures()

writer = QgsVectorFileWriter(outFn, \
'UTF-8', \
fields, \
QgsWkbTypes.Polygon, \
layer.sourceCrs(), \
'ESRI Shapefile')

for feat in feats:
    new_feat = QgsFeature()
    geom  = feat.geometry()
    buff = geom.buffer(bufferDist, 25)
    new_feat.setGeometry(buff)
    new_feat.setAttributes(feat.attributes())
    writer.addFeature(new_feat)

iface.addVectorLayer(outFn, '', 'ogr')
del(writer)

However, I'm not too sure why you wouldn't just do the following:

import processing

layerName = 'Calapan_points'
outFn = r'C:\Users\imper\Desktop\XYbuffer_tool\test.shp'
bufferDist = 10 # meters
layers = QgsProject.instance().mapLayersByName(layerName)
layer = layers[0]

processing.runAndLoadResults('qgis:buffer', {'INPUT': layer, 'DISTANCE': bufferDist, 'OUTPUT': outFn})
2
  • Oh. Sorry about that. Thank you, Ben W. "you must create a new empty QgsFeature for each each feature in the source layer and set the buffered geometry to those. You also need to set the attributes from the source point features to the new buffer polygon features." This is really helpful. I was not understanding the "flow" of the code until now. Thank you
    – BallpenMan
    Feb 24, 2020 at 5:43
  • No problem, @BallpenMan- you're welcome!
    – Ben W
    Feb 24, 2020 at 6:01

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.