4

I tried to find out and read multiple similar questions but not able to get answer to my question. I have two layers; points (various location data) and lines (roads). My aim is to calculate all points distance from the start of the road layer and populate this in points layer attribute (snapshot attached). Points are little away from the road line. I checked LRS but I did not find there. I am using QGIS 2.18.

Points distance from start

points distance of multiple roads

  • 1
    Where is the "start of the road layer"? Is the road layer just one road, and the start is the first vertex on it? – Spacedman Apr 19 '18 at 16:16
  • Star of road layer is direction and first vertex. I have multiple roads with unique code field and similarly multiple points along these roads. – Mohsin Apr 19 '18 at 16:22
  • So you want the straight line distance from points with some code=i to the first vertex (which for your example might be the bottom left point of the line) of the road with code=i ? All roads are a simple single linestring (not complex features with branches)? – Spacedman Apr 19 '18 at 17:38
  • first of all thanks for the very helpful manual. I would like to take up the topic again to determine the m-value. Is it possible to calculate the corresponding m-values of the points using a routed and calibrated line? The expression "m($geometry)" can be used to determine the m-values. Unfortunately, I couldn't include this in the calculation. Further information can be found at: gis.stackexchange.com/questions/224665/… Thank you in advance! – Stephan Feb 1 at 11:35
  • Hi. Answers are ment to be answers not questions. Please post it as a new question. – Mat Feb 1 at 11:59
7

QGIS 2.18 has a new function line_locate_point().

Open the Field Calculator and try this expression:

line_locate_point( geometry:=geometry(get_feature('Line', 'id', '1')), point:=$geometry)

(I assumed the id field of your first line is 1).

enter image description here

It will return the distance along the line, something like below.

enter image description here


Update

I noticed ongoing discussion about what distance is required for this question (i.e. straight or along the line). So I thought I would post this picture may add some clarity what this line_locate_point() tool gives us.

enter image description here


Update2

To work on multiple roads, you would better to have a field (road_id) in your point layer, to identify corresponding road. Then the expression becomes:

line_locate_point(geometry:=geometry(get_feature('Line','id',"road_id")),point:=$geometry)

enter image description here


Update3

According to your data, please try:

line_locate_point(geometry:=geometry(get_feature('Line','road_id',"road_id")),point:=$geometry)
  • Note the change ('id' to 'road_id') of the second input parameter for get_feature() function.
  • I noticed your attribute table of line data (road_line) has its own road_id field. (It was not in my test sample. Sorry for the confusion caused.)
  • It is working fine Spacedman and thanks a lot for your support. Kazuhito appreciate your further clarification. Sure I want to get distance along the line not straight. – Mohsin Apr 19 '18 at 19:08
  • As I have hundreds of roads with codes and also thousands of points. How can I apply the code so that multiple roads and point distance can be calculated? Can we calculate 3D distance as I have elevation of each point as well? Much appreciate your support. – Mohsin Apr 19 '18 at 19:13
  • @Mohsin thanks for your feedback. I understand your situation. I have no knowledge how you store your data, but updated the expression as much as I can. Hope this helps. – Kazuhito Apr 19 '18 at 19:28
  • Currently it is in shapefile and I am going to import in Postgres/PostGIS. How can we apply to multiple roads? I greatly appreciate your support. – Mohsin Apr 19 '18 at 19:47
  • Hi Kazuhitu, thanks for your code. I tested the code and it is not calculating distance for multiple roads. Road_id field is text (please see attached snapshot and code). I am using following code: line_locate_point(geometry:=geometry(get_feature('road_line','id',"road_id")),point:=$geometry) – Mohsin Apr 22 '18 at 22:22

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.