11

I'm trying to intersect polygon geometries in QGIS by using a virtual layer:

SELECT
    sbqry.rowid AS gid,
    sbqry.geom
FROM
    (SELECT
        ST_Intersection(land_parcels.geometry, flood_zone.geometry) AS geom
    FROM land_parcels, flood_zone
    WHERE ST_Intersects(land_parcels.geometry, flood_zone.geometry) AND NOT ST_Touches(land_parcels.geometry, flood_zone.geometry))
    AS sbqry;

Unfortunately, sbqry.rowid AS gid returns NULL instead of auto-incrementing values.

enter image description here enter image description here

Does anybody know how to create an unique identifier column named 'gid'? As far as I know virtual layers are based on SQLite/Spatialite.

9
  • What data type are you using for the virtual layer? Jun 29, 2016 at 19:11
  • The input layers are .shp files, the output geometry type is 'polygon'. Jun 29, 2016 at 19:14
  • I didn't know you could run spatial functions (ST_*) on shapefiles! That's great!!! Jun 29, 2016 at 19:25
  • have you tried removing sbqry from 'SELECT rowid as gid'... I found another post with such an example.
    – kttii
    Jun 29, 2016 at 19:26
  • 'rowid AS gid' works with simple SELECT statements like creating a buffer but not with a subquery. Jun 30, 2016 at 16:23

2 Answers 2

3

In PostGIS, if you just want the row number you can do this:

SELECT
    row_number() over () as gid,
    sbqry.geom
FROM
    (SELECT
        ST_Intersection(land_parcels.geometry, flood_zone.geometry) AS geom
    FROM land_parcels, flood_zone
    WHERE ST_Intersects(land_parcels.geometry, flood_zone.geometry) AND NOT ST_Touches(land_parcels.geometry, flood_zone.geometry))
    AS sbqry;
1
  • 'row_number() over()' seems to work in PostGIS but not in SpatiaLite (QGIS virtual layers). Jun 29, 2016 at 19:15
2
+25
SELECT  f.rowid AS gid, f.geom
FROM
(SELECT sbqry.geom
FROM (SELECT ST_Intersection(land_parcels.geometry, flood_zone.geometry) AS geom FROM land_parcels, flood_zone 
WHERE ST_Intersects(land_parcels.geometry, flood_zone.geometry) AND NOT ST_Touches(land_parcels.geometry, flood_zone.geometry)) AS sbqry) as f
1
  • Unfortunately, the query returns NULL values only. Feb 17, 2017 at 19:13

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.