PyQGIS – Extracting Start and End Points of Line in PyQGIS

pyqgisqgis-processing

I want to make a buffer on the start or end point of the line and not the line itself.

Is there some thing in QGIS python plugin development that can get me the points?

Best Answer

You can use the following code to get the start and end points of your line layer and loads this as a memory point layer:

line_layer = qgis.utils.iface.activeLayer()
feat = QgsFeature()

point_layer = QgsVectorLayer("Point?crs=epsg:4326", "point_layer", "memory")
pr = point_layer.dataProvider()

for feature in line_layer.getFeatures():
    geom = feature.geometry().asPolyline()
    start_point = QgsPoint(geom[0])
    end_point = QgsPoint(geom[-1])
    feat.setGeometry(QgsGeometry.fromPoint(start_point))
    pr.addFeatures([feat])
    feat.setGeometry(QgsGeometry.fromPoint(end_point))
    pr.addFeatures([feat])

QgsMapLayerRegistry.instance().addMapLayer(point_layer)

Result:

Result

You can then use this memory layer to create buffers around the points.

Related Question