Clip features and dissolve the result by region#

Measure the area covered once within each region. Use valid polygon layers in a common projected CRS with suitable area units and a stable region key. Carry the key through the intersection so a feature spanning two regions contributes a piece to each.

Create a control case#

Create regions.csv.

region_id,shape
R,"POLYGON((0 0,4 0,4 3,0 3,0 0))"

Create features.csv on the same planar grid.

fid,shape
A,"POLYGON((-1 0,3 0,3 2,-1 2,-1 0))"
B,"POLYGON((1 0,5 0,5 2,1 2,1 0))"

Intersect, dissolve and measure#

Save this query as coverage.sql.

WITH r AS (
    SELECT region_id, ST_GeomFromWKB(geometry) AS g FROM regions
), f AS (
    SELECT ST_GeomFromWKB(geometry) AS g FROM features
), pieces AS (
    SELECT r.region_id, ST_CollectionExtract(ST_Intersection(r.g, f.g), 3) AS g
    FROM r JOIN f ON ST_Intersects(r.g, f.g)
), dissolved AS (
    SELECT region_id, ST_Union(g) AS g
    FROM pieces
    WHERE g IS NOT NULL AND NOT ST_IsEmpty(g)
    GROUP BY region_id
)
SELECT region_id, ST_GeometryType(g) AS family, ST_Area(g) AS covered_area
FROM dissolved

With cugeo-geometry-query available and CUGEO_GEOMETRY_CUDF_LIBRARY exported with the library path, run the query and inspect the TSV.

cugeo-geometry-query --sql coverage.sql --csv regions=regions.csv wkt:shape --csv features=features.csv wkt:shape

Check for one polygonal result for R covering 8 of its 12 square coordinate units. Each clip covers 6 square coordinate units. Summing them gives 12 because their shared strip of 4 square coordinate units is counted twice. The CSV inputs do not declare a CRS or units.

Extracting polygons and filtering empty pieces means a region with only boundary contact or no contributing features has no group. If the report requires every region, carry the original region list into reporting and distinguish known zero coverage from missing input geometry. For the exact overlay, union and NULL/EMPTY semantics, see the spatial functions reference.