[GIS] How to place points along a line using a list of distances in QGIS

pyqgisqgisqgis-2.14

I am using QGIS 2.14 and I have a list of distances and descriptions similar to the table below

pipe1,50,lateral
pipe1,100,lateral
pipe2,51,break in pipe

I would like to add a point along a pipe at the specified distance (eg. 50,100,51) . I know there is a processing algorithm but I don't think I can use it with selected lines.

How can I add points along a line using my table?

Best Answer

One workaround by using PyQGIS is in the next code. I put your original distances in a list but, it was assumed that each point should be placed in positions along the line based in a sum_distances list ([50, 150, 201]).

registry = QgsMapLayerRegistry.instance()

layer = registry.mapLayersByName('my_line2')

distances = [50, 100, 51]

sum = 0

sum_distances = []

for dist in distances:
    sum += dist
    sum_distances.append(sum)

feat = layer[0].getFeatures().next()

geom_points = [feat.geometry().interpolate(distance).exportToWkt()
               for distance in sum_distances]

epsg = layer[0].crs().postgisSrid()

uri = "Point?crs=epsg:" + str(epsg) + "&field=id:integer""&index=yes"

mem_layer = QgsVectorLayer(uri,
                           'points',
                           'memory')

prov = mem_layer.dataProvider()

feats = [ QgsFeature() for i in range(len(sum_distances)) ]

for i, feat in enumerate(feats):
    feat.setAttributes([i])
    feat.setGeometry(QgsGeometry.fromWkt(geom_points[i]))

prov.addFeatures(feats)

QgsMapLayerRegistry.instance().addMapLayer(mem_layer)

After running the code at the Python Console of QGIS I got:

enter image description here

It seems to work correctly.