10

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...

wollyka
  • 447
  • 3
  • 10

1 Answers1

10

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

Encomium
  • 3,133
  • 2
  • 14
  • 41
  • Very neat solution. I really appreciate it. However, I have a question. If I choose pretty breaks, I get this link. Does that mean that I get a cyan blue circles for the first 50 points? If I choose equal interval, I get this link. I get 1 to 30. Since I want each 30 points with the same color, should I choose equal interval or equal quartile? – wollyka Jun 01 '21 at 18:33
  • Hi I followed the screenshot and updated instructions. I have 146 bins or classes. I obtained this. However, when I count the circles colored blue/purple at the top I get 60 and not 30 link since according to the screenshot, bin values 1 and 2 (30+30) should be colored blue – wollyka Jun 01 '21 at 19:43
  • 1
    Updated to simplify. Try now :) – Encomium Jun 01 '21 at 19:53
  • Still, Instead of getting a value of 1 with a legend of 1 I am still getting value of 1-2 with a legend of 1 so double the 30. link Maybe because my points total is 4367 (not multiple of 30) and my bins field is decimal (145.56)? edit: I noticed that your total points are also not a multiple of 30, so it is not the case! – wollyka Jun 01 '21 at 20:12
  • 1
    I think you went wrong somewhere. Ceil never gives a floating number. It rounds up to the next integer. You should have a bin field with the first 30 rows having the same integer value, and so on. Once you have that, you can use the Categorized symbology to just colour points by the bin value. – Encomium Jun 01 '21 at 20:16
  • I know, my bins are correct I am getting 146 bins which is correct https://imgur.com/MsRM8W7 https://imgur.com/k8yHxvb Could be something with the QGIS version? Using Windows QGIS 3.18.2 – wollyka Jun 01 '21 at 20:20
  • 1
    Use Categorized :) I updated my post to reflect that - the whole graduated thing was silly thinking on my part and totally unnecessary, apologies. I also updated the answer to also give a solution for the second part of the question. Refresh if you don't see that, please. – Encomium Jun 01 '21 at 20:37
  • 1
    Didn't see the categorized. I apologize!!! Working great. I just saw the second part – wollyka Jun 01 '21 at 20:39
  • Thanks I tried running it using a virtual layer or the execute SQL but I am getting an error https://imgur.com/a44TJah My knowledge in SQL is very basic – wollyka Jun 01 '21 at 21:41
  • 1
    Updated to show you how you would go from a shapefile layer to a Geopackage, where you can leverage the power of SQL much more readily within QGIS. – Encomium Jun 01 '21 at 23:12
  • Thanks! much appreciated. I used the geopackage like the instructions (which is new to me so double thanks). I also managed to do the same using a virtual layer with a slightly different expression Mazraa is the name of the table 'select bins,st_convexhull(st_collect(geometry)) as geom from Mazraa group by bins' However, on both methods I am getting the same crazy polygons :p https://imgur.com/a/gYYLNAG – wollyka Jun 01 '21 at 23:24
  • 1
    That query envelops all points with the same bin number. Is that not what's happening? – Encomium Jun 01 '21 at 23:27