5

I am looking for a QGIS function to count specific point values inside polygons. I have a number of points, all sorted in specific categories "HOOFDGROEP" and a rectangular grid (see image).

I would like to know per grid cell, how many points per category "HOOFDGROEP" are in every grid cell.

enter image description here

Does anyone have any idea how to do this?

4
  • 2
    I think this question must be reopened, this post does't solve this question, and is really old.
    – Mayo
    Jan 3, 2023 at 22:29
  • 1
    How would you store this data, as a JSON string, as a comma separated text in a text field, what is the file format of the grid layer?
    – Mayo
    Jan 3, 2023 at 22:35
  • If you run an Intersect and then a pivot table on the resulting table you will get the info you need. I have always used Excel for its ease and power but to stay within QGIS you could use the Group Stats plugin.
    – John
    Jan 4, 2023 at 10:34
  • What do you want to do with the generated data: show all categories / sums as a Label, have the data to export, have the data in a QGIS Table for other processing ?
    – Kasper
    Jan 4, 2023 at 13:48

3 Answers 3

6

One thing you can do to get the data, is to create a Virtual Layer, using SQL count(), st_contains() and group_by

Starting from a Grid and a source_layer with a 'category' field:

enter image description here

Create a Virtual Layer with the following definition:

select Grid.id as grid_id, source_layer.category, count(*) as count, Grid.geometry
 from Grid,source_layer
 where st_contains(Grid.geometry,source_layer.geometry)
 group by Grid.id,source_layer.category

enter image description here


Resulting virtual layer with geometries, attributes and count:

The resulting Attribute Table is the following:

enter image description here with the grid_id, category, count along with the grid.geometry There are as many lines per grid item than there are unique categories present in that grid item.


Using the virtual layer to display Labels in the original Grid

If you want to use this data in your Grid layer for instance to label it with the count per category, you can do the following:

Create an expression based label with this expression:

array_to_string(overlay_contains(layer:='count_layer',expression:='\n' || category || ' : ' || count ))

enter image description here

4

Open the field calculator in the grid layer and calculate a new text field with a considerable length (lets say 64 characters) with this expression.

array_to_string(
    with_variable(
        'classes',
        overlay_contains(
            'Points',  -- set here point layer name / id
            "HOOFDGROEP" -- set here name inside double quotes of the category field 
        ),
        array_foreach(
            array_distinct(@classes),
            format(
                '%1: %2',
                @element,
                array_count(@classes, @element)
            )
        )
    ),
    ','
)

What this expression does, is to get all the values of the "HOOFDGROEP" field from the points layer that are contained by the current cell that's being calculated and count how many times each different value appears. If you want to display labels you can set in the Formatting tab the Wrap on character option to , (comma).

2

1
  • Really nice and elegant solution, I'll look at it in details. I spent quite some time on it and failed getting a result with Expressions, got stuck by the inability to use filter:=category_field=@element, which you solve nicely! Learnt 'Wrap on character' as well
    – Kasper
    Jan 4, 2023 at 18:17
2

I have just added the processing algorithm "Count Features in Features by Category" to v1.4 of ProcessX-PlugIn.

You can find it in your processing toolbox in ProcessX -> Vector - Conditional -> Count Features in Features by Category.

You can use all geometry types as Source and Overlay, so you can e.g. also count polygons intersecting lines if you wish. Then select a field or an expression that contain your categories. You may choose how the desired output shall look like and can choose between three options:

  1. Create a new Source-Feature for each category
  2. Create one Source-Feature with a new field for each category (be warned: if you have too many categorys (lets say be careful if there are more than 250; but there is no fixed limit), this may crash QGIS when you open the resulting attribute table due to an overflow)
  3. Create one Source-Feature with one new field for all categorys and store the result as a string in form of a Python dictionary (be warned: if the string exceeds the limit of maximum characters (lets say be careful if its longer than 1000 chars, but there is no fixed limit), this may crash QGIS when you open the resulting attribute table due to an overflow)

Then simply choose the desired geometric predicates, whether at least one or all of them must match, and if a feature should be counted more than once.

enter image description here

An example result:

enter image description here

Disclaimer: I am the author of this PlugIn

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.