6

I have a set of points, which are located in the same area where the other, polygon layer is located.

enter image description here

I would like to have all the points selected, but ONLY within the one polygon belonging to the different layer. I.e. I would lie to have all points selected within the Polygon ID = 1 and so forth.

The following option: Vector -> Research Tool -> Select by location -> "Are within"
simply doesn't work, because it selects the points falling within the whole polygon layer, which comproses of 7 polygons exactly. I would like to have the selection only within one polygon.

I think, the issue could be an analog to this thread:

Selecting only polygons that contain another polygon with Select by Location in QGIS

However, these formulae applies didn't work.

Potentially this option:

points within a polygon

could be doable, but it looks like I have to create some virtual layer first.

I tried also these approaches:

Intersection between points and polygons in QGIS

QGIS select by location doesn't use selected features only

https://www.youtube.com/watch?v=CU1A86JJMIk&ab_channel=OpenSourceOptions

Selecting features within polygon from another layer using QGIS

QGIS how to select entries not in another layer

but they didn't work for me.

Is there any way to make this selection just within one polygon of the external layer?

Matt
  • 16,843
  • 3
  • 21
  • 52
Geographos
  • 4,087
  • 2
  • 27
  • 88

3 Answers3

12

If you check "Select features only" option for the polygon layer, you can select the points within the selected polygon.

enter image description here

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
10

Using selection by expression:

overlay_within (
LAYER:= 'POLYGON',
filter:= "COD_PROV" = 88)

enter image description here

pigreco
  • 4,460
  • 1
  • 15
  • 32
  • 2
    Much simpler than mine! I couldn't get this format to work at first, not sure why. – Matt Feb 15 '22 at 15:50
6

You can use the Geometry by expression tool on the points layer, with an expression like this:

aggregate(
        layer:='points', 
        aggregate:='collect',    -- collect the resulting geometries
        expression:=$geometry, 
        filter:=within(
                    $geometry,   -- the current point geometry
                    geometry(    -- make a geometry from a particular polygon feature
                        get_feature('polygons', 'name', 'polygon 001') -- get the feature where "name" = 'polygon 001', be sure to use single quotes for the field name inside the get_feature() function
                    )
                )
)

enter image description here


Or should you wish to select, rather than create a new layer (which is more in line with the question), you can use this expression in Select by expression on the points layer:

array_contains( 
    aggregate(layer:='points', 
              aggregate:='array_agg', 
              expression:=$id, 
              filter:=within(
                         $geometry, 
                         geometry(get_feature('polygons', 'name', 'polygon 001')
                      )
              )
    ), 
    $id
)

enter image description here

It is similar to the first expression, but checks if the feature id is in the array of points within the polygon in question.

Matt
  • 16,843
  • 3
  • 21
  • 52