I would like to create equidistant points along lines in QGIS, but each of my lines have an attribute of how many points should be on them in a column called "piece" and these values are different in almost every case.

enter image description here

My first thought was to divide lines to equal pieces according to the values in the attribute table and then put points to the centroids of the lines but I could not do it.

This question is very similar to what I would like to do, but it is not for QGIS:

How to create the same number of points along multiple polylines?

Most similar questions that can be found here at stackexchange in this topic are about a specific distance between points, a number that can be easily typed into a field (like in the case of Create points along lines or Convert lines to points algorithms) and not about specific data from an attribute table, which I've been unable to find how to use for such purposes.

It would be fantastic if there were no points right at the start and end point of the line but I would be just over the moon if there were as many points on a line as in its attribute table.

1 Answer 1


Here's a quick PyQGIS script which should do the trick

from qgis.core import QgsFeature, QgsVectorFileWriter, QgsGeometry

def create_points(feat,writer):
    geometry = feat.constGeometry()
    if not geometry:
    length = geometry.length()
    # -----------------
    # change 'num_points' to match your field name for the number of points field
    # -----------------
    num_points = feat['num_points']
    delta = length / ( num_points + 1.0 )
    distance = 0.0
    for i in range(num_points):
        distance += delta
        output_feature = QgsFeature(feat)
        output_feature.setGeometry( geometry.interpolate(distance) )

layer = iface.activeLayer()

# ---------------
# change 'd:/test_points.shp' to desired output file name
# ---------------

writer = QgsVectorFileWriter('d:/test_points.shp',None, layer.fields(), QGis.WKBPoint, layer.crs())

for f in layer.getFeatures():

del writer

Just change the num_points field name and output file name to match your data, select the input layer, and run it in the python console.

  • For some reason, after copying this script an error message pops up: File "<input>", line 1 length = geometry.length() ^ IndentationError: unexpected indent
    – Skye
    Commented May 6, 2016 at 7:41
  • 1
    ndawson - I've edited the formatting so that it's easier to copy/paste into the python console but feel free to roll back if you don't like it =). @Skye - I would suggest copying the code into a text editor, change the field name and output path and then copy the code again into the python console. Hopefully there shouldn't be an indentation error anymore.
    – Joseph
    Commented May 6, 2016 at 9:26
  • 1
    Thank you for both of you!! :) ndawson, your script is brilliant and @Joseph, your formatting edit was such a great help! Plus it turned out that I should have used " / " mark instead of " \ " at the output path.
    – Skye
    Commented May 6, 2016 at 9:40

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.