Creation of Buffer zone using Python in QGIS

Опубликовано: 15 Октябрь 2024
на канале: MSR Geo Spatial Solutions
3,145
43

Hi Viewers,
In this video, you will come to know the python program coding for the creation of buffer zone for point features in QGIS.

Find the program below for your reference.

iface.newProject()
iface.addVectorLayer("d:/msr-geospatial-solutions/qgis-python/points.shp","points", "ogr")
InputFile = 'points'
OutputFile = "d:/msr-geospatial-solutions/qgis-python/bufferzone.shp"
bufferDist = 0.02

##To get the fields from map layer

Data = QgsProject.instance().mapLayersByName(InputFile)
layer = Data[0]
fields = layer.fields()

QGS Vector File Writer will create buffered features and add to output file.

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

Geometry along with buffer. Use writer to add the feature to the buffer layer.

for f in layer.getFeatures():
geom = f.geometry()
buffer = geom.buffer(bufferDist, 0.02)
f.setGeometry(buffer)
writer.addFeature(f)
print('done')
Run Python code

##Finally, add the buffered layer to the QGIS interface and delete the writer.

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