5

How can I check if a geometry is contained in a geometry collection ? Specifically , I am using ST_ClusterWithin in PostGIS to get a cluster which returns each cluster as a geometry collection. Now I want to extract the cluster_id for each geometry. For this I need to check if the geometry is within the geometry collection. How can I do it ? It is related to this question I asked on stackexchange some days ago.

Nicolas Boisteault
  • 1,039
  • 10
  • 30
Malice
  • 257
  • 1
  • 10
  • 1
    For the application you describe, it should be sufficient to figure out which geometry collection each input intersects. – dbaston May 31 '16 at 12:52

1 Answers1

3

you can use a combination of ST_CollectionExtract and ST_Dump()

and compare results to your geometry. You will need to specify the geometry type you want to extract:

1 == POINT, 2 == LINESTRING, 3 == POLYGON

WITH c AS (
SELECT (ST_Dump(ST_CollectionExtract(geom),2)).geom AS geom 
FROM my_geo_collection)

SELECT c.geom, t.geom FROM
c, my_geometry AS t

WHERE c.geom = t.geom

This might not work if your reference geometry is a multigeometry, since ST_Dump expands multi geometries into simple geometry types

Thomas
  • 1,197
  • 5
  • 11