3

I have two layers (polygons and points) in a geopackage (area_and_points.gpkg, LINK). I need to extract all point that lie within the polygons and also inherit one attribute (Name in this case) from that polygons. I want to use aggregate expression to solve this. I used the following "Geometry Generator" code on the points layer. I get an empty layer.

aggregate(
layer:='polygons', 
   aggregate:='collect', 
   expression:=intersection(geometry(@parent),$geometry),
   filter:=intersects(geometry(@parent), $geometry) 
)

enter image description here

It even shows "Preview: <geometry: MultiPoint>". But the result is that there is no actual filtering and the generator geometry (Modified geometry) has all the points (15 in this case) as shown in the image below. I would be happy to get any solution using expression.

enter image description here

I am not even getting the points filtered what to speak of adding the attribute (Name).

5
  • In the geopackage you provided, the polygon layer is empty (containing 0 features).
    – Babel
    Commented Sep 4, 2022 at 20:28
  • Sorry about that. The polygons are restored.
    – Stat-R
    Commented Sep 5, 2022 at 3:47
  • Should actually work. Check if both layers are in the same CRS.
    – MrXsquared
    Commented Sep 5, 2022 at 7:01
  • The expression works, but it will not add any new attributes (name of polygon the points are located within). Creating a new attribute and a selection of the points is not possible in one step/expression using QGIS expressions alone. Also be aware to use the full layer name instead of just polygons in line 2. In the sample project you provided, this worked: aggregate( layer:='polygons_e2b7c2ad_6eec_4a0a_b6cc_2b580b12acef', aggregate:='collect', expression:=intersection(geometry(@parent),$geometry), filter:=intersects(geometry(@parent), $geometry) )
    – Babel
    Commented Sep 5, 2022 at 7:06
  • Geometry generator is not for creating new attributes ("inherit one attribute (Name in this case) from that polygons") and neither does it "extract all point that lie within the polygons". It simply creates a new style for all those points that fulfill the condition. If you want to work with the "extracted" points, you must either create a new attribute and run processing based on that or create a new layer with only the features you want.
    – Babel
    Commented Sep 5, 2022 at 7:10

1 Answer 1

4

There are different options: 1) select points inside polygons and copy/paste them to a new layer (fastest and easiest); 2) Virtual layer (most elegant) 3) using QGIS expressions (a bit cumbersome with with some shortcomings). In detail:

Remark: changing you layer names to something easier than area_and_points(1) — points could be a good idea.

Solution 1: copy/paste selected features

Use Select by expression to select the points inside the polygons using overlay_within ('polygons') (use the name of your polygon layer).

Then copy/paste the selected features to a new layer. Add the name of the polygon layer they are within using the expression in step 1 of solution 3 below.

Solution 2: virtual layer

Create a virtual layer with this query:

select *, pt.geometry 
from "area_and_points(1) — points" as pt, 
"area_and_points(1) — polygons" as poly
where st_within ( pt.geometry, poly.geometry)

enter image description here

Using QGIS expressions

  1. Create a new attribute name on the point layer with this expression, using Field calculator:

    overlay_within(
        'polygons_e2b7c2ad_6eec_4a0a_b6cc_2b580b12acef',
        name
    )[0]
    
  2. Extract the points that are within polygons to a new layer with Geometry by expression and the following expression:

    case 
    when 
        overlay_within(
            'polygons_e2b7c2ad_6eec_4a0a_b6cc_2b580b12acef'
        )
    then $geometry
    end
    

    However, you will get a layer with all features from the initial layer, but the points outside polygons will come without features (only attributes) - so you probably want to delete these using Remove null geometries

1
  • 1
    Thanks. I learned many things
    – Stat-R
    Commented Sep 5, 2022 at 11:59

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.