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.
Add a virtual field to the line layer by using the Field Calculator to create an unlimited length text field with this expression:
Install the refFunctions plugin. This plugin gives you additional functions in the Field Calculator, including
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)
geomnearestretrieves the "WKTgeometry" value from the line closest to the current point
geom_from_wktconverts the "WKTgeometry" into a geometry
line_locate_pointreturns the distance along a linestring to the closest position the linestring comes to the point geometry
It ain't pretty and I'd love if someone found a more elegant solution but here goes:
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".
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 locationfunction.
- 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.
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"))