4

I have a table in PostGIS, where the data is currently stored as a MultiPolygon. Each Multipolygon has only a single Polygon in it.

How do I query and get the containing Polygon, and not MultiPolygon? I am ok ignoring the Edge case where there are Multiple Polygons in the Multi-Polygon Geometry. All I need is the first or only Polygon in that Geometry.

4

If you want to Ignore the edge cases, and get only the First (or only) Polygon, from the MultiPolygon geometry, then you need to use the ST_GeometryN() function.

A query like the following will give you the geometry as Polygons

SELECT ST_GeometryN(geom, 1)
FROM polygons;
1
  • 2
    the issue with the (ST_Dump(geom)).geom type of query, is that you get multiple rows, and need to use it in a subquery, if you want to get just the first Polygon. Jul 21 '21 at 4:26

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.