5

In QGIS 3.16.11 on Windows 10 I have a layer with single polygons.

Given this polygon layer with, I'm trying to select only the polygons that that contain at least one other polygon (A & B)

enter image description here

  1. My first thought was, gonna be easy just do:

    Research tools -> Select by Location

    INPUT: original_layer  
    INTERSECT: original_layer  
    PREDICATE: contain   
    

    But everything got selected. It's acting like contain select polygon that contain but also that are equal. same thing happens with are within.

  2. Then I tried with intermediary layer of slightly inflated polygons and to do the contain on that layer

    Geoprocessing Tools -> Buffer

    INPUT: original_layer  
    DISTANCE: 0.00001
    OUPUT: inner_polygon_slighly_inflated  
    

    Research tools -> Select by Location

    INPUT: original_layer  
    INTERSECT: inner_polygon_slighly_inflated  
    PREDICATE: contain
    

    Then I got selected B, but not A.

Can't find a way to this and which seems quite simple. Is there a way to make a PREDICATE: contain but not equal?

Taras
  • 32,823
  • 4
  • 66
  • 137
codekoriko
  • 205
  • 1
  • 5

2 Answers2

5

Use "Select by expression" with this expression: overlay_contains(@layer).

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208
  • 1
    Thank you very much for that! This "select by expression" opens a whole new realm of possibility, I've seen virtual layer using SQL syntax. So much things to learn! – codekoriko Sep 19 '21 at 10:25
  • Indeed! I have the impression that QGIS expressions are easier to understand than SQL syntax - especially as you have the Help at hand wherever you use expressions (expand Show Help on top left of the expression builder). – Babel Sep 19 '21 at 10:26
3

This is an extension to @Babel's answer which I found absolutely efficient.

Hence the statement "selecting only polygons that contain another polygon" was not really clear to me, I think some users may find it also a bit obscure. So there must be a differentiation between (1) selecting only polygons that contain another polygon as filled holes and (2) selecting only polygons that contain another polygon that lay over.

Let's assume there is a polygon layer called 'poly_test', see image below.

input

Case 1. Selecting only polygons that contain another polygon as filled holes

selecting polygons like "id" = 5 but not "id" = 1

case_1
P.S. Here I moved the feature with "id" = 6 for visual purposes

In the "Select by expression" from the Selection Toolbar use the following expression

overlay_touches(@layer) and num_interior_rings($geometry)

and get the final output

result_1

Case 2. Selecting only polygons that contain another polygon that lay over, i.e.

selecting polygons like "id" = 2 but not "id" = 5

case_2

In the "Select by expression" from the Selection Toolbar use the following expression

overlay_contains(@layer) and num_interior_rings($geometry)

and get the final output

result_2

Case 3. Selecting both from Case 1 & 2

In the "Select by expression" from the Selection Toolbar use the following expression

(overlay_contains(@layer) or overlay_touches(@layer)) and num_interior_rings($geometry)

and get the final output

result_3

Taras
  • 32,823
  • 4
  • 66
  • 137