I've got two tables in a PostgreSQL/PostGIS database, blue
and purple
, both holding polygons:
Now I need to calculate the overlapping area, for each polygon of the blue grid with only one of the purple polygons (the one in yellow):
For that, I've built the following query
SELECT b.name,
SUM (ST_AREA (
ST_INTERSECTION (
b.geom,
(SELECT geom FROM purple WHERE id = 'A')
)
)
)
FROM blue b LEFT JOIN purple p
ON ST_INTERSECTS (b.geom, p.geom)
GROUP BY b.name, b.geom
;
However, the calculation is incorrect in some cases (i.e. producing figures that exceed the total area of the squares in the grid). What's wrong with the query?
SUM()
, which indicates you actually want "the sum of areas for the neighboring polygons".