4

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?

enter image description here

6
  • 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.
    – Katja
    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?
    – Babel
    Sep 9, 2021 at 8:13
  • 1
    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
  • 1
    Maybe the "DBSCAN clustering" can help u
    – Taras
    Sep 9, 2021 at 8:57

2 Answers 2

2

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: enter image description here

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.

  2. Run Menu Vector / Geometry Tools / Multipart to singleparts

  3. Run Menu Vector / Geometry Tools / Centorids

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): enter image description here

Fill in the expression in the dialog of Menu Processing / Toolbox / Geometry by expression - see screenshot: enter image description here

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

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.

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.