3

I have a table in PostGIS (2.1.7) containing adjacent multipolygons, and I'd like to create a view containing polygons which are merged versions of the multipolygons based on some field value.

I tried the following query:

CREATE VIEW results AS
SELECT somefield, ST_Union(geom)::geometry(Polygon, 2056) AS geom FROM somelayer GROUP BY somefield;

and I got the following error:

Geometry type (MultiPolygon) does not match column type (Polygon)

I thus tried to change the result type to multipolygon:

CREATE VIEW results AS 
SELECT somefield, ST_Union(geom)::geometry(MultiPolygon, 2056) AS geom FROM somelayer GROUP BY somefield;

But it didn't help since I got the following error:

Geometry type (Polygon) does not match column type (MultiPolygon)

The following query finally worked but it is not optimal since I'd like to have polygons instead of multipolygons (it should be possible since the resulting polygons are all singlepart):

CREATE VIEW results AS 
SELECT somefield, ST_Multi(ST_Union(geom))::geometry(MultiPolygon, 2056) AS geom FROM somelayer GROUP BY somefield;

How can I get the desired results?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked May 22, 2015 at 9:12

1 Answer 1

3
CREATE VIEW results AS ( 
SELECT somefield, (ST_Dump(ST_Union(geom))).geom AS geom FROM somelayer GROUP BY somefield);
wiltomap
2,7402 gold badges23 silver badges52 bronze badges
answered May 22, 2015 at 9:23
1
  • I have the same problem using your suggestion @MichalZimmermann... The query runs OK but the geom column geometry is geometry, not geometry(MultiPolygon). This causes display issues in QGIS with DB Manager... Commented Sep 22, 2016 at 12:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.