PostGIS - convert multipolygon to single polygon

user3012604 picture user3012604 · Feb 12, 2014 · Viewed 38.1k times · Source

Is it possible to import a shape file containing multipolygons into single polygon in PostGIS? Whenever I try importing a shape file of a polygon, it is stored as a multipolygon (as opposed to a single polygon) in a geom column. Thus, I am unable to extract it as a single polygon value from the multipolygon.

All helpful suggestions much appreciated

Answer

Tyler picture Tyler · Aug 6, 2015

I used ST_DUMP to convert a table of multipolygon geometries in PostgreSQL to a new table with polygon geometries and other columns of data.

CREATE TABLE poly AS                       --poly will be the new polygon table
WITH dump AS (
    SELECT id, test,                       --columns from your multipolygon table 
      (ST_DUMP(geometry)).geom AS geometry 
    FROM multi                             --the name of your multipolygon table
) 
SELECT id, test, 
  geometry::geometry(Polygon,4326)         --type cast using SRID from multipolygon
FROM dump;

Update: I think this could be accomplished much easier with this query.

CREATE TABLE polygon_table AS 
    SELECT id, example_column, (ST_DUMP(geom)).geom::geometry(Polygon,4326) AS geom FROM multipolygon_table