6

On the figure below, I have:

  • a shapefile of cells of 0.5x0.5 degrees around the African continent
  • a shapefile of different regions in Africa

enter image description here

I would like to count the number of separate regions which pass through out each cell (see the second picture for an example). In the selected cell, there are three separations (three regions).

Hence, in the final output, I would like to assign a three to that cell. I would like to do it with every cell of my grid.

My version of QGIS is 3.20.1.

enter image description here

Second problem: when I try to do the suggestion of @BERA

I do what @BERA suggested (look the screenshot). "polygons" is my grid cell and border_tribes is the shapefile with the regions. However, I do not obtain the results. In fact, in every cell I obtain a value of 1 for each grid cell. In the second image, I attach another screen shot of the kind of results I obtain. The number should be 3 but it appears 1.

enter image description here

enter image description here

Third problem: when I try to do @BERA suggested in the comments as a possible solution to problem 2

When I change "borders_tribes" by grid, I obtain a result of 8 in each cell. I attach a picture of what I am doing and an example of the problem.

enter image description here

enter image description here

10
  • 1
    Show the attribute table of poligoni layer
    – Bera
    Commented Feb 11, 2022 at 18:09
  • 1
    You havent got an attribute called borders_tribes in your Poligoni layer. If gid, ccode or country is unique you can use any of them. That is why your calculation doesnt work
    – Bera
    Commented Feb 13, 2022 at 8:23
  • Thank you @BERA for your answer. So, instead of border_tribes, which is the shapefile with the regions I would like to account for each cell, should I change it by gid, which is unique for each cell?
    – OgeiD
    Commented Feb 14, 2022 at 21:48
  • I have edited my answer because when I did when you told me I am still not having what I would like to have
    – OgeiD
    Commented Feb 15, 2022 at 16:52
  • 2
    Would it be possible if you share this data with us?
    – Taras
    Commented Feb 17, 2022 at 8:20

2 Answers 2

7
+50

An alternative using Processing tools:

Search for "Join Attributes By Location (Summary)" in the processing toolbox. Choose the following settings:

  • Base Layer: Your Grid Layer
  • Join Layer: Your Regions Layer
  • Geometric Predicate: Intersects
  • Fields to summarise: A unique key of your Regions layer
  • Summaries to calculate: Count and/or Unique

Run it. Output is a copy of your grid layer containing the count and/or unique count of intersections with your regions layer.

enter image description here

Example result:

enter image description here

In case you cannot find the processing toolbox, go to View -> Panels and enable Processing Toolbox. Then type the name of the processing tool into the search box:

enter image description here

1
  • It really works!! Thank you so, so much!!!
    – OgeiD
    Commented Feb 18, 2022 at 8:36
7

Use Field Calculator:

array_length(array_distinct(overlay_intersects( 'regions', kom_kod)))

Replace regions with the name of your region layer, and kom_kod with some unique region attribute

enter image description here

8
  • Thank you for your answer. I am trying to do it but I am not understanding very well how to do it. When I go to "Field Calculator" it appears a difference screen than the one in your comment. I have to fill the "input layer", which I guess I should put there my grid. But after I am not sure what I should do. When you say "region layer" do you refer to the grid cell?
    – OgeiD
    Commented Feb 10, 2022 at 21:03
  • When I try to do it, it says to me "Parser error with formula expression "array_length(array_distinct(overlay_intersects( 'Polígono'), borders_tribes)))": array_distinct function is called with wrong number of arguments. Expected 1 but got 2. Execution failed after 0.09 segundos"
    – OgeiD
    Commented Feb 10, 2022 at 21:08
  • remove closing parenthesis nest to Poligono. it has to be like .....ects( 'Polígono', bord......
    – Lilium
    Commented Feb 10, 2022 at 23:23
  • 1
    @OgeiD you can try replacing kom_kod with $id, maybe this solves your 1 result issue.
    – MrXsquared
    Commented Feb 17, 2022 at 19:14
  • 1
    @OgeiD $id returns the unique id of a feature. It is not related to an attribute. It ensures to be always available and always be unique.
    – MrXsquared
    Commented Feb 18, 2022 at 9:33

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.