5

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?

3
  • 1
    you tried to do a section by expression using an expression like: overlay_within('namePolygonLayer', filter:=id=2) expression to write for the point layer.
    – pigreco
    Feb 15 at 13:57
  • 1
    Expression is invalid Mate
    – MKR
    Feb 15 at 14:33
  • 1
    I replied, look down
    – pigreco
    Feb 15 at 15:04

3 Answers 3

10

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

enter image description here

1
  • That's fantastic! Thank you for this swift solution!
    – MKR
    Feb 15 at 14:38
9

Using selection by expression:

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

enter image description here

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

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.

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.