6

Let's assume there two layers, see image below

  • a point layer 'random_points_test' with 10 features
  • a polygon layer 'test' with three features

input

I am trying to modify the number of features in the point layer based on a number of overlaps with polygon features. So, a point feature that overlaps three polygon features will now exist three times in the attribute table and so on.

Of course, I am aware of the "Intersection" tool, however, it drops features where the overlap does not exist, see image below.

attempt

I can do it with a Virtual Layer, like this:

SELECT
    a.id AS "a_id",
    b.fid AS "b_id",
    a.geometry as geom
FROM
    "random_points_test" AS a,
    "test" AS b
WHERE
    ST_Intersects(a.geometry, b.geometry)
UNION
SELECT
    a.id AS "a_id",
    '' AS "b_id",
    a.geometry as geom
FROM
    "random_points_test" AS a
WHERE
    a.id NOT IN (
    SELECT
        a.id
    FROM
        "random_points_test" AS a,
        "test" AS b
    WHERE
        st_intersects(a.geometry, b.geometry)
    GROUP BY
        a.id)

So, this is my desired output:

result

Another solution might be to build a model in the Graphical Modeler, where I will need to (1) split features into two groups with overlap and without, then (2) proceed with the "Intersection" geoalgorithm for the first group, and finally (3) merge them back into one layer. So, I was wondering if there is a smarter native solution to achieve the desired output?

1 Answer 1

8

The "Join Attributes by Location" tool, with a 'one-to-many' join type, should give your desired result.

enter image description here

enter image description here

1
  • 2
    Thank you. Seems like everything simple is really powerful.
    – Taras
    Commented Feb 17, 2022 at 6:08

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.