QGIS – Calculating Shortest Distance Between Point and Line via Field Calculator and Overlay_Nearest

distanceexpressionfield-calculatorlineqgis

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)

Best Answer

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