I have a polyline (movement path) and points recorded along the line. I would like to calculate for each point the distance along the line in QGIS. I hope for a result which will add a new column to the point attribute table indicating the distance along which it is on the line.
5 Answers
If you'd like to go with the GRASS plugin, there's a module v.distance that does just that. First you'll bring the polyline and point layers into GRASS using the v.in.ogr module in the GRASS Toolbox. Then locate the v.distance module. The From and To vectors are obviously the points and line. Then select "Distance to nearest point ... along linear feature". You'll also need an attribute column name to accept the distance values (Use v.db.addcol to add that attrib if you don't already have). That should do it.
-
Thanks for the solution. "Upload: Values describing the relation between two nearest features" should be "to_along" Commented May 9 at 19:50
You can use fieldcalculator with an expression such as:
line_locate_point(overlay_nearest('line',$geometry)[0],$geometry)
Replace 'line'
with the name of your line layer. overlay_nearest()
will get the geometry of the nearest line to your point. line_locate_point()
the distance in crs units along that line to the specified point. Of course you can also use this expression on the line layer, therefore just modifiy it a little.
Other way to measure this, it is using Qchainage (QGis plugin) to produce nodes equallly spaced from line. Then, you may use Distance to nearest hub (QGis plugin) to calculate distance among points.
You can use a virtual layer and join each point to a line (I use within 5 m distance below) and measure the distance along the line. ST_Line_Locate_Point
:
return a number (between 0.0 and 1.0) representing the location of the closest point on LineString to the given Point, as a fraction of total 2d line length
so I multiply with the line length.
Replace "waypoint" with the name of your point layer and "path" with the name of your line layer. And add more fields from the point layer if you want to. Like select pnt.id, pnt.someotherfield, ...
select pnt.id, pnt.geometry,
round(ST_Line_Locate_Point(line.geometry, pnt.geometry)*st_length(line.geometry),1) as distalongline
from "waypoint" pnt
left join "path" line
on PtDistWithin(pnt.geometry, line.geometry, 5)
order by ST_Line_Locate_Point(line.geometry, pnt.geometry)
The KP Find plugin does this with a layer of points and another layer, containing a line feature. It projects a point on the line and gives the geodetic distance along the line and the distance to that line. It has an interactive mode or can iterate over an entire point layer, adding new attribute fields.