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?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Abhijit Gujar
  • 2,750
  • 3
  • 29
  • 43

3 Answers3

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.

Joseph
  • 75,746
  • 7
  • 171
  • 282
8
layer # your vector layer (line type)

for feature in layer.getFeatures():
    geom = feature.geometry().asPolyline()
    print "Start: " + geom[0]
    print "End: " + geom[-1]
dmh126
  • 6,732
  • 2
  • 21
  • 36
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

xunilk
  • 29,891
  • 4
  • 41
  • 80