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.
Asked
Active
Viewed 5,915 times
5
-
1Where 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: https://gis.stackexchange.com/questions/224665/qgis-extract-nodes-with-m-values-for-linear-referencing Thank you in advance! – Stephan Feb 01 '19 at 11:35
-
Hi. Answers are ment to be answers not questions. Please post it as a new question. – Mat Feb 01 '19 at 11:59
-
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – Jochen Schwarze Feb 01 '19 at 12:07
-
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review – Erik Feb 01 '19 at 12:14
1 Answers
13
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_idfield. (It was not in my test sample. Sorry for the confusion caused.)
Kazuhito
- 30,746
- 5
- 69
- 149
-
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
-
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. – 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
-
Hi @Mohsin Just to check; do you have both
idandroad_idfields in your point layer? (sequentialidrepresents individual point, androad_idis indicator for the road respectively). – Kazuhito Apr 23 '18 at 01:00 -
Yes I have a id field in points as well as in road line layer. I do not find any attachment option of snapshot while adding this comment. – Mohsin Apr 23 '18 at 02:24
-
@Mohsin Adding the snapshots to your original post would be helpful (there is an
editunderneath). Such details would also help reopening the question, then others can give you more suggestions. – Kazuhito Apr 23 '18 at 11:15 -
Thanks. I added snapshot in original post showing points and road line attributes. – Mohsin Apr 23 '18 at 14:18
-
Thanks @Mohsin for the images of attribute tables. Please see revised syntax in Update3. – Kazuhito Apr 23 '18 at 18:51
-
Dear Kazuhito, great and very valuable support from you and really appreciate. I have some issue to assign values in POINTZM and LINEZM geometry from attributes. Can I ask here or I need to ask new question? Thanks again. – Mohsin Apr 24 '18 at 00:32
-





