# Get nearest neighbour on same or another layer to connect points by drawin a line in QGIS

In my project I try to find the shortest way from a point to a specific line (street). Fortunately, this is no big deal in QGIS 3.0 using "Points along lines" and "Distance to Nearest Hub (Line)". But if not the street but another point on the same layer is closer, I would like to connect to the nearest point on the same layer. (A little tolerance to prefer the connection to the street would be ideal)

So this is what is now: This would help me a lot One of the difficulties I'm struggling with is to find the nearest point on the same layer (and draw a line to it). A solution for that as a first step would be very helpful, too.

• Indeed, an interesting question! Have a look at QGIS expressions (using Geometry generator or Geometry by expression) with `overlay_nearest` function to get the closest point on the same layer: `overlay_nearest (@layer, \$geometry)`. Also have a look at `closest_point(geometry1,geometry2)` to get the point on geometry1 that is closest to geometry2. Dec 16, 2021 at 14:38

## The basic idea

Create lines connecting each point to it's closest neigbour. You will get lines with gaps in between. To close the gaps, connect each of these lines with the closest point on the nearest neighboring line. Finally, connect each of these lines to the closest point on the nearest section of the street layer.

## How to do it in QGIS: create lines with `Geometry by expression`

1. Create a line from each point to its closest neighbor using the following expression with Geometry by expression and replace `points` with the name of your point layer: `make_line ( \$geometry, overlay_nearest( 'points', \$geometry, limit:=2))`

Each point connected with red lines to the nearest neighboring point: 2. Clean the output of step 1 using these four steps: i) apply Explode lines, ii) Delete duplicate geometries, iii) select all features and then Merge selected and iv) finally turn Multipart to single parts.

3. Connecting lines: each line should be connected to the closest point of the nearest line. Use this expression on the output of step 2 with Geometry by expression and replace `Singleparts` with the name of the layer from step 2:

`````` union (
\$geometry,
make_line (
closest_point (
\$geometry,
overlay_nearest(
'Singleparts',
\$geometry,
limit:=2
)
),
closest_point (
overlay_nearest(
'Singleparts',
\$geometry,
limit:=2
),
\$geometry
)
)
)
``````
4. Clean the output of step 3, repeating the four steps of step 2 above.

The red dotted line is the output from step 4: 5. Now you have to connect each resulting line to the street layer. Use once again Geometry by expression on the output of step 4 with this expression and replace `street` with the name of your street layer:

`````` union (
\$geometry,
make_line (
closest_point (
overlay_nearest ('street', \$geometry),
\$geometry
),
closest_point (
\$geometry,
overlay_nearest ('street', \$geometry)
)
)
)
``````

See final output:  You can install NNjoin plugin on extensions to find the nearest point on the SAME layer

or try this is symboloy >> geometry generator :

``````make_line(\$geometry,(overlay_nearest("your layer",\$geometry)))
``````
• Hi LM10, thanks for your adivse! Sadly I can't get some results out of it. Using the code I get no lines whatsoever. Even a simplified variation like 'overlay_nearest("your layer",\$geometry)' shows not the nearest point. Is it possible that the Code provides just the nearest point to the point that it finds. So the point itself? Or am I doing some silly beginner's mistake and you get the results as you wish? Dec 16, 2021 at 12:24
• Do you test this : geometry generator : make_line(\$geometry,(overlay_nearest("your layer",\$geometry)))  : return the first nearest point.
– LM10
Dec 16, 2021 at 12:56
• Got the silly(?) mistake: My Point-Layer is a MultiPoint-Layer, it has to be converted in a Single-Point-Layer. Use "mulitpartstosingleparts" in the Toolbox ;) Dec 20, 2021 at 12:56 try : 1 - Geoprocessing tool >>> join by location on your layer

2 - On the new layer go to symbology >>> geometry generator and paste this :

``````make_line ( make_point ( "feature_x" ,  "feature_y" ) , make_point ( "nearest_x" , "nearest_y" ) )
``````
• Hi, I'm using QGIS 3.22 where the joinattributesbylocation-Tool requieres other inputs, e.g. a geometrical predicat. But the your frist answer using the geometry generator works very well for Single-Point-Layers. Dec 20, 2021 at 13:01