4

I was searching for the shortest distance between a point (point layer with about 22000 points) and the nearest point of a line from another layer (MultiLineString). I tried Distance Matrix, nearest neighbour, distance to nearest hub. None with the expected result. But then I found something interesting on GIS SE, but only displaying the shortest line. (Find nearest line feature from point in QGIS by @babel) It goes:

make_line( 
    $geometry ,
        closest_point( 
            geometry(   get_feature_by_id ('bayern_25832gis_osm_waterways_free_1',
    array_to_string( overlay_nearest ( 'bayern_25832gis_osm_waterways_free_1', "osm_id" ) 
  )
   )
   ),
    $geometry ) ) 

It looked perfect, so I tried copying it in the field calculator by exchanging make_line by 'distance' and voila.

Could it be thus simple?

I am just a biologist and have really no clue about expressions, their functions and so on.

How can I verify my results?

By now I only looked at the data, labeled the distances and measured by hand but cannot do this for 22.000 features.

Shortest distance in meters (converted to int)

1 Answer 1

5

To get the length of the line, you must not replace make_line by distance, but add the function length to the rest of the expression, then use it in field calculator to calculate the length of the shortest line from between each point and the nearest point on a line.

Thus the whole expression will look something like what follows, where you have to replace the [...] part with the settings that work for you (your initial expression):

length ( make_line ( [...] ) )

Don't forget to add another closing bracket ) at the end when adding the length function.

Screenshot: the dotted lines are created with the same expression as the one you see here, just without the length() part. Be sure to apply this on the points layer and make the right settings: get thecorrect name of the line layer, set Output field type to a numerical format (integer or decimal) and check if the preview at the bottom shows the correct result: enter image description here

7
  • Thanks for your answer. Unfortunately I don't get it. When I put this expression into the field calculator, I get NULL for each feature. When I put it into Geometry Generator, I don't see any effect. I need the values of the shortest distance between each point and the nearest point along a line for further statistics. I do not need to display it. Therefore I tried field calculator. I also tried MMQGIS Plugin to calculate the nearest hub. The result again is not what I expected.
    – biologist
    Sep 27, 2021 at 9:00
  • Take the expression that works for you to create the lines in your screenshot. Then enclose this exprssion in a length ( ) statement: so inside these brackets, insert your whole expression. Let's say your expression to create the lines looks like this (shortened): make_line ( [...] ) - then use this to calculate the length: length ( make_line ( [...] ) ) - you see: your initial expression is inside the brackets of length() This does not output a line, but a value for the length. If it doesn't work, please add a screenshot showing your settings and add the exact expression you use.
    – Babel
    Sep 27, 2021 at 9:30
  • I tried the length (make_line (...) expression twice, once in geometry generator, with exactly no effect, eccept that the arrows disappeared, and once in the field calculator, where I got NULL for every feature. The exact expression is: length( make_line ( $geometry, closest_point ( geometry ( get_feature_by_id ( 'bayern_25832gis_osm_waterways_free_1', array_to_string ( overlay_nearest ( 'bayern_25832gis_osm_waterways_free_1', "fid" ) ) ) ) , $geometry ) ) )
    – biologist
    Sep 27, 2021 at 9:58
  • When I put this expression as value for labelling, I can see the values. But I cannot make statistics with them, because they are nowhere in my attribut table. When I take the field calculator and use distance instead of length (without make line), I got all the values in an extra field for further statistics and it seems to be the same values as with the length expression in the labelling field. How could this be?
    – biologist
    Sep 27, 2021 at 10:06
  • Ok, probaböy you set the wrong field type in field calculator? Set ut to decimals. What does the preview in the expression editor show?
    – Babel
    Sep 27, 2021 at 10:10

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.