This question is continuing the discussion from here.

Since I want to adopt another approach and see if it is feasible since I tried different approaches (K-means, Convex hulls, etc. and they didn't work as expected since the points are very close together (urban areas)

I have a layer with points/centroids representing the different buildings in the area. I want to group them in blocks/grids each having x number of points (e.g., 30 or 50 points etc.).

I am using the 'Add line feature' in another layer and I am drawing manually the blocks while counting also manually the points. Is there a way to count automatically/dynamically the points after drawing each block/grid?

I know that there is 'Count points in polygons' in vector analysis tools but I am using lines and I don’t want to add an extra step of converting the lines into polygons, then count the points in the polygons and if there is an error, I have to delete the polygons and the lines and then do it all over again.

Is it possible to use a script or something?

  • 1
    So, you want a not yet existing feature to have an attribute? – Erik Jun 2 at 9:20
  • Mmm. When you put it like this :) Maybe after drawing the polylines/lines and not during it (Similar to count points in polygons). Sorry if it seems a dumb question! Learning QGIS by trial and error! – wollyka Jun 2 at 9:35
  • 1
    For this to work with lines you'd have to create closed lines - so you simply could use polygons. Why are polygons not an option? – Erik Jun 2 at 9:54
  • Since later the idea was to use Split with lines or Polygonize to divide the original polygon into smaller ones and each small polygon has x number of buildings/points. If it can also be done with polygons, I have no complaints! – wollyka Jun 2 at 10:12

You can achieve this using a Virtual Layer.

Go the the menu layer / add layer / add-edit virtual layer and enter the following query. Replace myPointLayer and MyPolyLayer with your layer names.

select po.id, po.geometry, count(*) cnt
from MyPoly po
 JOIN myPointLayer pt
  ON st_intersects(po.geometry, pt.geometry)
group by po.id;

It selects each of your polygons, gets its ID and geometry, then select the intersecting points and count them.

You can then style this layer to have no fill, and show the computed field cnt as the label.

When you edit the polygon layer, as soon as you pan the map a little bit or refresh the map (F5), the point count will be automatically updated.

enter image description here

PS: autodetect should work just fine for the geometry section of the virtual layer

  • Neat solution. I assume that they only works with polygons and points and not close lines (polylines) and points right? – wollyka Jun 2 at 21:36

Just create a view where you count points per polyline layer. Making it a view means it's dynamic and will update as the polyline layer changes.

  1. Create a GeoPackage
  2. Make a point layer in it, or import your point layer into it
  3. Make a polyline layer, cf. above.

You now have something like this: enter image description here

  1. Go to DB Manager and run this query:
SELECT a.fid, a.geom, count(b.geom) AS point_count
FROM lines a
LEFT JOIN points b ON st_contains(st_makepolygon(a.geom), b.geom)
GROUP BY a.fid;

Where 'lines' is the name of your polyline layer, 'points' the name of your point layer, 'a.geom' the name of your polyline geometry field and 'b.geom' the name of your point geometry field. These will be different depending on how you made the layer, so just check to see what they're called in the DB Manager Info pane for your table.

  1. Load it as a layer: enter image description here

  2. You can now see and symbolize the polygons: enter image description here

  3. Adding new polylines or changing existing ones will update the layer. Make sure to do the changes on the original polyline layer; not the dynamic view (which doesn't contain any data, just a query on the point and polyline table). Just go to edit mode on the polyline layer, do your edits, save them and then the virtual layer will update, showing you the point counts per polyline.

  • Neat solution. I assume that they only works with polygons and points and not close lines (polylines) and points right? – wollyka Jun 2 at 21:36
  • 1
    I actually didn't realize you wanted this with lines rather than polygons. I'm updating the answer -- it works just fine with st_makepolygon. – Encomium Jun 2 at 21:47
  • It is just easier to divide one big polygon (after converting it to polylines) into smaller polygons using the split with lines. Unless I am mistaken! – wollyka Jun 2 at 21:49
  • I updated it to be working with polylines. This method will work equally well on both types of spatial data. – Encomium Jun 8 at 12:53

You can do this using the new overlay features in the field calculator: use this expression in the labels of the polygon layer:

array_length (overlay_contains ('LayerPoint', $id))

enter image description here

starting with >= QGIS 3.16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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