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.

1
  • 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
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

2
  • Are you sure it works? ST_Dump returns a set. In my case I have ERROR: operator does not exist: geometry = geometry_dump error. – Nicolas Boisteault Jun 2 '17 at 8:52
  • Right, you have to use (st_dump ()).geom. I corrected my answer – Thomas Jun 2 '17 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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