Search code examples
postgresqlcollectionspostgispolygon

Extract POLYGON or MULTIPOLYGON from GEOMETRYCOLLECTION


in my table, I have a geometry field. I realize that there are GeometryCollection type. There are lines and polygons, it creates a lot of problems when I do processing with st_intersections. I would like to convert the GeometryCollection into polygons while keeping the polygons unique. Dots and lines should be deleted.

Here is my request:

SELECT ST_Union(ST_MakeValid(ST_SnapToGrid(wkb_geometry, 0.0001)))  ,
ST_Union(ST_MakeValid(ST_SnapToGrid(wkb_geometry, 0.0001)))::json as format_json
from public.land group by id

here is a screenshot of what i get enter image description here


Solution

  • You can use ST_CollectionExtract(geom, 3) to extract only the polygons from your geometry collection.