# How to place points along a line using a list of distances in QGIS?

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?

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]))



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

It seems to work correctly.

• The distances are not a total. The first column in the table is a pipe ID. If the feature attribute matches the ID then start adding the points along those matching lines (pipes). You answer gave me a starting point. – GreyHippo Mar 14 '17 at 19:58

You can get this with the interpolate function in PyQGIS.

I manually tested this with a multilinestring layer.

1. Open the python console Plugins -> Python Console
2. Select the layer you want to place points on, and select the linestring feature you want to place a point on
3. In the Python console do the following commands

lineLayer = iface.activeLayer() lineLayer now represents the layer you have selected

line = lineLayer.selectedFeatures()[0] line now represents the first feature selected on your line layer. Make sure you only have one feature selected

feature = QgsFeature() creates a new feature

point = line.geometry().interpolate([distance along line]) point represents the geometry of the point at the specified distance along your line feature

feature.setGeometry(point) sets the geometry of the feature

1. Create a new multipoint layer and select that layer in the layers panel and then do the following commands.

pointLayer = iface.activeLayer() pointLayer is now the multipoint layer just created.

pointLayer.addFeature(feature) add the point feature you created with interpolate to the new layer.

Notes: The distance used in interpolate is based on the coordinate reference system that you are using. Make sure it is a projected CRS with appropriate feet or meters conversions to match the distances you want to offset. Test to make sure the distances are correct before doing too much.

With this general pattern you should be able to set up a simple script in python to create all of the points from your offsets.