QGIS Clustering – Points Clustering Techniques in QGIS 3.18

categorized-rendererclusteringpythonqgis

I already asked a similar question here and I tried the different solutions but I wasn’t very successful.
Thus, I was thinking of using another approach if possible and I created another question.

Summary: I have a layer of centroids/points. The layer has thousands of points. I want to group each x number of points close together in one group/polygon. The points are numbered using a function (Numbering adjacent points/centroids in sequential order) from Numbering adjacent points/centroids in sequential order – QGIS 3.18

enter image description here

My question: is there a way

  1. To highlight automatically each x sequential points (e.g., each 30 points) using a different color (so every 30 points have the same other, the next 30 points have another color etc…)
  2. Or to create automatically a line/polygon in another layer or on the same layer grouping each those x sequential points together (so each polygon has 30 points in it)
    My idea is then later I can manually segment or tweak the areas using the different points’ colors or using the lines/polygons created automatically.

I tried using the points render: https://docs.qgis.org/3.16/en/docs/user_manual/working_with_vector/vector_properties.html#point-cluster-renderer
However, since it is dynamic the number of clusters is changing based on the zoom level and at the same time, I cannot tell the render to join 30 close points together and render them as one point and it is not easy to tell which points are included in one cluster and which points are counted in another cluster

enter image description here etc…

Best Answer

You can do the first by creating bins in a new field with the expression

ceil(id/30)

Where id is your sequential field. This returns 1 for the first 30 points, 2 for points 31-60 and so on.

Then, go into Symbology, choose Categorized, your bin field as the Value and classify.

This is what it'll look like:

enter image description here

Map result, showing 30 features per bin (note that my test data isn't ordered spatially).

enter image description here

You can do the second thing easily with this sql expression, using your bins:

select bins,st_convexhull(st_collect(geom)) as geom
from table
group by bins

Collects all the points by bin number and creates a convex hull around them. Result:

enter image description here

And here's a guide to running SQL queries within QGIS:

  1. Save your layer as a Geopackage.
  2. Go to DB Manager and connect to the Geopackage
  3. Click the 'SQL Window' button to the left of the 'Import Layer/File' button:

enter image description here

  1. Paste in the query above, replacing 'table' with the name of your layer. Run it and choose to 'Load as new layer'. The unique ID column is bin, name it whatever you want.

enter image description here

  1. Click Load. This will generate a temporary view enclosing your points with polygons, grouped by bins. You can see the query in action in the layer's info panel, in the Source field:

enter image description here

Related Question