# Clustering vectorpoints

I created a layer of vector points out of a raster. The raster was a .png, where I extracted all the pixels with the value 1. Initially, it was a Paper, where I marked points with a pen. Now I am about to digitalise it via QGis. So far, I have a lot of vector points, which I created with "Raster to point". Is there a possibility to cluster the points (which are located right next to each other) to one point? I only have coordinates as attributes. I was looking for some feature with nearest neighbor function. Has somebody an idea, how it could work out?

• What exactly do you mean when saying "summarize the points"?
– Erik
Sep 9, 2021 at 8:00
• I would like to summarize them to one point! or reduce them. Sep 9, 2021 at 8:02
• You want to merge them? So from 6 points you get only one? What is the condition? As long as they are inside the same white space in your screenshot? Sep 9, 2021 at 8:13
• For some of the points towards the bottom, it is quite easy to guess how you want them combined, but towards the top, you have some cases that are not quite as clear. E.g you have an area of 8 and 6 points that are bridged by one point. Should those be one or two points in your final dataset? For good measure, the leftmost part of that cluster is diagonally touching another cluster - should those be two different points at the end or should they be combined. (For the screenshot, I would guess you expect 20 points at the end - but there may be as few as 17) Sep 9, 2021 at 8:57
• Maybe the "DBSCAN clustering" can help u Sep 9, 2021 at 8:57

The solution: the principle Create a line from each point to all direct neighbors. Buffer these lines and get the centroid.

The solution here is based on 4 neighbors in a regular 9 * 9 grid: the point in the center has 4 direct neighbors: left/right and top/down, without the points in the corners (connected by a diagonal). However, you can easily adapt the solution to include these points (8 neighbors) by only changing a number, see below.

Only horizontal/vertical (black lines) neighbors are considered, not diagonal ones:

Implementation in QGIS

1. Use Geometry by expression (see last screenshot below) with the following expression on your point layer. Replace `0.05` in line 3 with the distance between neighboring points (horizontal/vertical distance from one pixel centroid to the neighboring one) and replace `'point'` in line 8 with the name of your point layer.

If you want a solution with 8 (instead of 4) neighbors, replace `1.1` on line 21 with `1.9`.

``````with_variable(
'distance',
0.05,
collect_geometries (
array_foreach (
array_foreach (
overlay_nearest(
'point',
\$geometry,
limit:=9
),
make_line (
\$geometry,
@element
)
),
if (
distance(
start_point (@element),
end_point ( @element)
)<@distance*1.1,
buffer (@element,@distance/2),
buffer (@element,-@distance)
)
)
)
)
``````
1. Run Menu Vector / Geoprocessing / Buffer with a buffer size of `0` and check the box to dissolve the result.

And here you are with one point for each group:

Screenshot: red= initial points, light yellow: buffers around each group of neighboring points, blue: centroids (=results):

Fill in the expression in the dialog of `Menu Processing / Toolbox / Geometry by expression` - see screenshot:

• Wow, cool&clean! Sep 9, 2021 at 9:40
• I'm sorry, I can't find, where to enter that script? Sep 9, 2021 at 9:57
• `Menu Processing / Toolbox / Geometry by expression`, see: i.sstatic.net/SE7Gy.png Sep 9, 2021 at 10:03
• Perfect! thank you so much! Sep 9, 2021 at 10:18

What you need is called "clustering". There are several options/algorithms to achieve what you need, but `DBSCAN Clustering` is the first I would try (as also suggested by Taras):

`Processing` -> `Toolbox`

and enter "cluster" in the filter field.

this will create a vector file containing the "clusterized" points.