[postgis-users] How to trim a GeometryCollection to get a MultiPolygon?

Michael Smedberg Michael.Smedberg at redfin.com
Sun Jun 22 10:14:42 PDT 2008


I have some SQL that intersects a number of multipolygons, and then
attempts to test whether a point is in the result.  In most cases it
works fine.  However, some intersections return a GeometryCollection,
which results in an error like:

ERROR:  Relate Operation called with a LWGEOMCOLLECTION type.  This is
unsupported

Is there a simple and generic way to simplify the collection to trim out
linestrings, etc.?  Through experimentation, I've found that doing an
ST_Buffer(XXX, 0.0) will clean up the GeometryCollection, but I don't
know if that's a safe thing to rely on.

Here's an example of a problematic SQL statement:

SELECT ST_Contains
(
	ST_Intersection
	(
		GeomFromText('MULTIPOLYGON(((0 0,0 2,2 2,2 0,0 0)),((0
4,2 4,2 6,0 6,0 4)))', 4326),
		GeomFromText('POLYGON((0 1,2 1,2 4,0 4,0 1))', 4326)
	),
	GeomFromText('POINT(1 1.5)', 4326)
)

And here's an example of a "fixed" SQL statement:

SELECT ST_Contains
(
	ST_Buffer
	(
		ST_Intersection
		(
			GeomFromText('MULTIPOLYGON(((0 0,0 2,2 2,2 0,0
0)),((0 4,2 4,2 6,0 6,0 4)))', 4326),
			GeomFromText('POLYGON((0 1,2 1,2 4,0 4,0 1))',
4326)
		),
		0.0
	),
	GeomFromText('POINT(1 1.5)', 4326)
)

Is it OK to use ST_Buffer to clean up a GeometryCollection?  Or is there
a better/safer way to do this?

Thanks so much!




More information about the postgis-users mailing list