Skip to main content
Tweeted twitter.com/StackGIS/status/1494189881609838596
fixed typo
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

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 modellerModeler, 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?

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 modeller, 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?

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?

Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

Creating duplicate points as many times as it overlap with polygons in QGIS

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 modeller, 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?