4

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?

3 Answers 3

14

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.

2
  • how to extend this and add attribute coluns from line object? Commented Feb 8, 2018 at 14:34
  • 1
    @PauloCardoso - Probably something like feat.setAttributes(feature.attributes())? You should ask this as a new question :)
    – Joseph
    Commented Feb 8, 2018 at 14:40
8

You can use v.to.points Processing Toolbox Algorithm.

At the next image, it can be observed:

  • Filtering by v.to.points
  • Selecting 'line' (shapefile used in this example) in 'Input lines layer'
  • Marking 'Write line nodes' option

enter image description here

After click in Run, I got "start and end points of each feature" in the shapefile line layer.

enter image description here

However, by using 'Write line vertices' option you can get all points:

enter image description here

8
layer # your vector layer (line type)

for feature in layer.getFeatures():
    geom = feature.geometry().asPolyline()
    print "Start: " + geom[0]
    print "End: " + geom[-1]

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.