Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I need a query that return the area of two polygons that have been split by a line.

This is what I did so far:

SELECT ST_Area(ST_Split(surface.geom, limite_a.geom)) from surface,
limite_a   where surface.id = 945 and limite_a.id=123
share|improve this question
add comment

1 Answer 1

The docs specify that ST_Split returns a collection of geometries. You can confirm this using some test data:

WITH rect AS (SELECT 'POLYGON ((0 0, 10 0, 10 1, 0 1, 0 0))'::geometry as geom),
     line AS (SELECT 'LINESTRING (3 0, 3 1)'::geometry as geom)

SELECT ST_GeometryType(ST_Split(rect.geom, line.geom)) FROM rect, line
-- returns 'ST_GeometryCollection'

As you've noticed, computing ST_Area on the GeometryCollection result gives the total area of the split polygons, which is the same area as your input polygon. To get the area of the individual components of the split polygon, you need to deaggregate the GeometryCollection using ST_Dump:

WITH rect AS (SELECT 'POLYGON ((0 0, 10 0, 10 1, 0 1, 0 0))'::geometry as geom),
     line AS (SELECT 'LINESTRING (3 0, 3 1)'::geometry as geom)

SELECT ST_Area((ST_Dump(ST_Split(rect.geom, line.geom))).geom) FROM rect, line
-- returns 3, 7

The extra parentheses around ST_Dump are necessary because this function actually returns a special composite type: geometry_dump. You want to pull the geometry out of the geometry_dump, which you accomplish by wrapping the result of ST_Dump in parentheses and accessing the .geom field. Check out the ST_Dump docs for some more examples of this.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.