As per the title really. I have a layer consisting of a network of Lines. I have another layer of point items 'snapped' to the lines. I'd like to attach a length value to the point based on how far along the line it is.

2 Answers 2

  1. Add a virtual field to the line layer by using the Field Calculator to create an unlimited length text field with this expression: geom_to_wkt( $geometry)

    enter image description here

  2. Install the refFunctions plugin. This plugin gives you additional functions in the Field Calculator, including geomnearest.

  3. Add a "distance along line" field to the point layer using the Field Calculator with this expression:

    line_locate_point( geom_from_wkt( geomnearest('Lines', 'WKTgeometry')),$geometry)
    • geomnearest retrieves the "WKTgeometry" value from the line closest to the current point
    • geom_from_wkt converts the "WKTgeometry" into a geometry
    • line_locate_point returns the distance along a linestring to the closest position the linestring comes to the point geometry

    enter image description here


It ain't pretty and I'd love if someone found a more elegant solution but here goes:

  1. Add fields for X and Y coordinates to the point features, to do this create two fields populated using the functions $x and $y respectively. I'm going to call those "x" and "y".

  2. Now we need to give those coordinates to the lines that they are on, this needs a spatial join. To do this use the Join attributes by location function.

    • When I tried joining the points to the lines it wouldn't work and I don't know why.
    • My workaround was to buffer the points by one metre and then join the buffer to the lines
      • input layer = lines
      • join layer = buffered points
      • join type = take attributes of first location feature only

    Now the lines have fields with the coordinates of the points.

  3. Next we create a field for the distance along the line of those coordinates, use the field calculator with this function:

     line_locate_point($geometry, make_point("x", "y"))
  • 1
    Probably the point to line join failed because the points aren't exactly on the lines (I know you said they were snapped to the line, but maybe the snapping algorithm is less precise than the join by location algorithm.) You could skip making point buffers if you use the NNJoin plugin to do a nearest-neighbor join, instead of joining by location.
    – csk
    Commented Jul 12, 2019 at 17:15

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.