2

I have two distinct tables, one called rpg(id, code_culture, geom --polygons) and the other called ia(id, classe, geom --also polygons) Like this :

capture

How should I proceed to get a single table with a single geometry while retaining the two attributes and ids like result(id_rp, id_ia, classe, code_culture, geom)?

Edit:

So far, I managed to do this :

enter image description here

With that query, adapted from some material here on stackex but it missed all the geom from ia that's not intersected by rpg but when they do, it's all good with both geom creating new polygons when intersected :

WITH cte1 AS(
SELECT 
    ia.id AS ia_id, rpg.id AS rpg_id
    , ST_Multi(ST_Intersection(ia.geom, rpg.geom)) AS geom
FROM ia, rpg
WHERE ST_Intersects(ia.geom, rpg.geom) AND NOT ST_Within(ia.geom, rpg.geom)

UNION ALL

SELECT ia.id AS ia_id, rpg.id AS rpg_id , ia.geom AS geom FROM ia, rpg WHERE ST_Within(ia.geom, rpg.geom) ) SELECT ia_id, rpg_id, (ST_Dump(geom)).geom::geometry(Polygon, 2154) AS geom FROM cte1;

Edit 2 with solution :

WITH poly_a AS ( SELECT id, NULL AS code_cultu, classe, geom FROM ref_autres.cosia_small_singlepoly ) ,poly_b AS ( SELECT id, code_cultu, NULL AS classe, geom FROM ref_autres.rpg_small_singlepoly ) ,lines AS ( SELECT ST_Boundary(geom) AS geom FROM poly_a UNION ALL SELECT ST_Boundary(geom) AS geom FROM poly_b ) ,noded_lines AS ( SELECT ST_Union(geom) AS geom FROM lines ) ,resultants AS (
SELECT geom, ST_PointOnSurface(geom) AS pip FROM St_Dump( (SELECT ST_Polygonize(geom) AS geom FROM noded_lines ))
) SELECT a.id AS ida, b.id AS idb, a.classe, b.code_cultu, r.geom FROM resultants r LEFT JOIN poly_a a ON ST_Contains(a.geom, r.pip) LEFT JOIN poly_b b ON ST_Contains(b.geom, r.pip) WHERE a.id IS NOT NULL OR b.id IS NOT NULL;

enter image description here

Geo7
  • 21
  • 2
  • QGIS has a multitude of processing tools that can help you to achieve that. If you can use it. – GforGIS Sep 29 '23 at 08:31
  • 1
    I can and I did (I have a result from GRASS) but I still would like to learn how to do this one in sql :) – Geo7 Sep 29 '23 at 08:37
  • Check out the spatial joins in this tutorial: https://postgis.net/workshops/postgis-intro/ – GforGIS Sep 29 '23 at 08:52
  • Make sure you got spatial indexes on both tables, or the queries will be slow. Your output goal is the equivalent of a QGIS Union operation? – BERA Sep 29 '23 at 09:24
  • Yes, if the qgis union is like the GRASS overlay with or condition (https://grass.osgeo.org/grass83/manuals/v.overlay.html) And yes I added the spatial indexes when importing to postgis via qgis :) – Geo7 Sep 29 '23 at 09:38
  • 1
    https://gis.stackexchange.com/questions/401245/getting-the-composite-of-two-polygon-layers – JGH Sep 29 '23 at 12:52
  • Thanks a lot ! I got a working query, quite expensive in time but I'm gonna compare against the same ops in GRASS on the full datasets ! I'm editing the initial post with the answer :) – Geo7 Sep 29 '23 at 14:21

0 Answers0