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.
-
1Where is the "start of the road layer"? Is the road layer just one road, and the start is the first vertex on it?– SpacedmanApr 19, 2018 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.– MohsinApr 19, 2018 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)?– SpacedmanApr 19, 2018 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!– StephanFeb 1, 2019 at 11:35
-
Hi. Answers are ment to be answers not questions. Please post it as a new question.– MatFeb 1, 2019 at 11:59
1 Answer
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
).
It will return the distance along the line, something like below.
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.
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)
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 forget_feature()
function. - I noticed your attribute table of line data (
road_line
) has its ownroad_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.– MohsinApr 19, 2018 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.– MohsinApr 19, 2018 at 19:13
-
1@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.– KazuhitoApr 19, 2018 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.– MohsinApr 19, 2018 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)– MohsinApr 22, 2018 at 22:22