# Replace two closeby points with one

I'm working on a pet project. The blue circles in the map below are highway exits. They exists in pairs of two (one for each side). I'd like to replace them with one point in the middle, indicating that in roughly that area one can access the highway. How would I go about achieving this in QGIS?

• Make lines, then get the centroid of the created lines.
– Erik
Commented Apr 17, 2020 at 13:27
• Hi and welcome! Do you have any kind of labels on your exits, or is the just the proximity that tell which pairs that belong together? (In the data you are showing, it is easy to see which belongs together, if you also want to work on roads in a more urban environment, it may be much more difficult) Commented Apr 17, 2020 at 13:27
• @MortenSickel No, I don't have labels, it's visually that I pair these exits. However, I'm not working with urban data, so I don't suspect this is a problem. Based on the first suggestion, I opted to create buffers around the points using MMQGIS. I then dissolve these buffer zones such that two nearby points belong to the same polygon (using geoprocessing tools). I then wanted to calculate the centroid of the new dissolved polygons. That's where I encountered the problem that only one centroid is calculated. When I check the attributes of the dissolved polygons, only one point is given... Commented Apr 17, 2020 at 13:58

You can achieve this in a single step using a virtual layer.

Extending on this post, it is possible to join the point layer with itself based on point proximity. For every pair of points within the given distance, the virtual layer will make a line and finally return a point at the line centroid.

Let's note that the distance is in the layer CRS unit, so make sure to use a CRS whose units is in meters, or if it is in lat-long use a much smaller distance threshold (like 0.01 degrees for roughly 1 km)

Go to the menu `Layer > Add Layer > Add/Edit Virtual Layer...` and enter the following query.

Note that the line is created once between 2 points (A-B is the same as B-A).

``````select st_centroid(make_line(a.geometry, b.geometry)) as geometry, a.id as fromID, b.id as toID
FROM myPointLayer a
JOIN myPointLayer b
ON ST_Distance(a.geometry, b.geometry) < 2000 AND a.id < b.id
``````

Here the green point is the centroid. The two points at the bottom are too far from each others, so there is no central point.

Thanks to the suggestions of the respondents I figured out a solution. Since I'm sure that in this setting nearby points belong together as one highway entrance point, I:

1. Created buffer zones using MMQGIS such that all pairs overlap.
2. Dissolved them using Geoprocessing tools, which creates a multiple part polygon.
3. Used multiple to single part to make sure we have individual polygons.
4. Calculated the centroids using geometry tools.

I figured out that step 3 was necessary thanks to this answer:

https://gis.stackexchange.com/a/215459/161811

Here's my end result (dissolved polygons deleted):