3

In the postgis documentation it is mentioned that ST_collect returns a MULTI* or a GEOMETRYCOLLECTION. But how can I force the function to only return a GeometryCollection not depending on it's input argument.

For Example: I have a postgreSQL database with a column of type GeometryCollection. And it should store geometries which a user has drawn. This can by any combination of geometries of points, lines, polygons etc... If the combination is a point, a line and a polygon ST_collect returns a GeometryCollection. But if the combination is two polygons it returns a MultiPolygon. Is it possible that ST_collect returns only GeometryCollections even if the input is a single polygon?

SELECT ST_AsText(ST_Collect(ST_GeomFromGeoJSON(feat->>'geometry')))
FROM (
  SELECT json_array_elements('{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "properties":{

         },
         "geometry":{
            "type":"Polygon",
            "coordinates":[
               [
                  [
                     14.845962524414062,
                     46.96830518448586
                  ],
                  [
                     14.845962524414062,
                     47.02309964439266
                  ],
                  [
                     14.976425170898438,
                     47.02309964439266
                  ],
                  [
                     14.976425170898438,
                     46.96830518448586
                  ],
                  [
                     14.845962524414062,
                     46.96830518448586
                  ]
               ]
            ]
         }
      }
   ]
}'::json->'features') AS feat
) as f;
  • 1
    Perhaps just cast it to a geometry collection? – BradHards Dec 26 '15 at 4:12
  • Why, as a matter of interest? GeometryColllections are something of a pain to work with. – John Powell Dec 26 '15 at 8:27
  • Yes you are right John. But i only wanna know if there is a possibillity.. Only for interest :) – Manuel Dec 26 '15 at 11:47
3

Use ST_ForceCollection http://postgis.net/docs/ST_Force_Collection.html

A simple example with point geometry:

SELECT ST_AsText(ST_ForceCollection(ST_GeomFromText('POINT (1 1)')));

Result:

"GEOMETRYCOLLECTION(POINT(1 1))"
| improve this answer | |
  • omg how i couldn see that in the docs. Thanks for helping :) – Manuel Dec 28 '15 at 23:53

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.