Search code examples
postgresqlpostgisgeojson

How to convert FeatureCollection to GeometryCollection or MultiPolygon?


I have many polygons that need to be drawn manually and then get geo-coordinates. I need to get the coordinates of the drawn polygons in GeoJSON format.

In this format:

"{"type":"MultiPolygon","coordinates":[[[[37.4653933,55.3959159]...}"
"{"type":"Polygon","coordinates":[[[37.475738525390625,55.41420507450017]...}"

Or in this:

"{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[-98.0419921875,39.027718840211605]...}]}"

I draw polygons at http://geojson.io/. But from this site I can only get data in the format with the FeatureCollection type. I found another site - https://rodic.fr/blog/online-conversion-between-geometric-formats/, on which I can convert to GeoJSON format, but this site can only convert type GeometryCollection.

I cannot find how to convert FeatureCollection to GeometryCollection or MultiPolygon or to Polygon.

How to solve? Many thx!


Solution

  • To get the coordinates in the geojson format, you can use the following snippet:

    WITH geojson_featurecollection AS (
        SELECT ''::json AS fc
    )
    SELECT (json_array_elements(fc->'features'))->>'geometry'
    FROM geojson_featurecollection;
    

    in which you paste your entire FeatureCollection definition (coming from the http://geojson.io website after your edits) inside of the quotes