[GIS] PostGIS – ST_Collect but only include POLYGONs not POINTs

optimizationpostgisunion

In PostGSI, is there anyway to turn a GEOMETRYCOLLECTION into a MULTIPOLYGON by dropping all non-POLYGONs/non-MULTIPOLYGONs?

I'm doing a query that cuts a bunch of geometries (using ST_Intersection) and I want to group them together. I'm using ST_Collect to turn this into a GEOMETRYCOLLECTION. I could use ST_Union, but that's slower. Is there an alternative approach where I can drop all the non-POLYGONs/non-MULTIPOLYGONs from a GEOMETRY collection and just return a MULTIPOLYGON?

I know would be less accurate than ST_Union, but it should be faster. For what I'm doing a small hit in accuracy is worth it if it speeds it up.

Best Answer

ST_CollectionExtract is what you are looking for:

http://postgis.net/docs/manual-2.0/ST_CollectionExtract.html