I would like to do some basic linear referencing (label the distance along a line every 1000 m e.g.) without using plugins or PostGIS or creating points along the line. I tried working with line_interpolate_point (for both rule based labeling and the repetition of single labels) but I don't think this is the correct direction.

Basically, I want to have a simple labeling style which displays geometry-based values every X m.

  • 1
    which geometry-based values do you want to label? – PieterB Oct 23 '19 at 14:49
  • "label the distance along a line every 1000 m e.g." - as in distance from starting point – Erik Oct 23 '19 at 14:50

This is Python solution for the QGIS Python console.

Just paste the code (after renaming "lines" to your line layer name) in the console.

It creates points every 100m along every single line feature in a line layer. Depending on your data, points are located differently when a new line starts/ends (see the picture below). A solution could be to merge lines depending on IDs, etc..

from contextlib import contextmanager
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import os

# silence stdout in QGIS Python console
def silence_stdout():
    new_target = open(os.devnull, "w")
    old_target, sys.stdout = sys.stdout, new_target
        yield new_target
        sys.stdout = old_target

l_layer = QgsMapLayerRegistry.instance().mapLayersByName('lines')[0]

p_lyr = QgsVectorLayer('Point', 'Points', 'memory')
pprov = p_lyr.dataProvider()
pprov.addAttributes([QgsField("sum", QVariant.Int)])

with silence_stdout():
    for elem in l_layer.getFeatures():
        sum = 0
        while elem.geometry().length() > sum:
            sum += 100                                    # here you can change the distance
            geom_point = elem.geometry().interpolate(sum)
            point_feat = QgsFeature()


enter image description here

| improve this answer | |
  • Thank you for the effort, but I do know the points along lines tool and would prefer not to use it - or similar solutions. The labels should be attached directly to the line layer. If you doubt this is possible, then this is also a valuable insight. – Erik Oct 24 '19 at 9:11
  • So a better solution would be to segmentize a the line layer to 100m segments for labeling. I would update my answer regarding this. – Stefan Oct 24 '19 at 9:22
  • I wouldn't call it "better". It is a possible, feasible solution (and known to me) but not the desired one. Sorry – Erik Oct 24 '19 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.