Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Is there any way to typecast Polygons to Multipolygon type?

For example, I have created a circle using ST_Buffer() on a point. The return type of that function is of Polygon type. The problem is that on the geometry column there is a restriction limiting it to Multipolygon type. So, I cannot insert a simple polygon.

I have used ST_MPolyFromText() function, but as usual it is returning null for that and ST_Dump() is not giving the proper output.

The constraint is:

CONSTRAINT enforce_geotype_geom CHECK (geometrytype(geom) = 'MULTIPOLYGON'::text OR geom IS NULL),
share|improve this question
    
Is there a reason for adding a multipolygon constraint? How about changing the constraint to polygon? –  underdark Sep 1 '11 at 7:24
    
Isnt't this a children question of gis.stackexchange.com/questions/14198/… ? –  unicoletti Sep 1 '11 at 7:36
    
It is, but it could be rephrased to be a stand-alone question too. –  underdark Sep 1 '11 at 8:02
    
yes this is a children question,, but the problem is when you import the shapefile into the postgis which is containing the polygon layer then automatically it creates the geometry of multipolygon. then it is little bit tedious to convert the datatype from multipolygon to polygon.. because that will be done by the client . So just have to save the output into that colummn of that type created by the postgis.. –  Vicky Sep 1 '11 at 13:12
    
PS: to do the inverse, from multi-to-poly, use st_geometryn(geom, 1). –  Peter Krauss Jan 13 at 12:23

1 Answer 1

up vote 23 down vote accepted

ST_Multi - Returns the geometry as a MULTI* geometry. If the geometry is already a MULTI*, it is returned unchanged.

Example:

SELECT ST_AsText(ST_Multi(ST_GeomFromText('POLYGON((743238 2967416,743238 2967450,
        743265 2967450,743265.625 2967416,743238 2967416))')));
        st_astext
        --------------------------------------------------------------------------------------------------
        MULTIPOLYGON(((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,
        743238 2967416)))
        (1 row)
share|improve this answer
    
Thanks Underdark. You helped me a lot.. it happens sometimes that you have to act according(without changing the need of the client) like i knew that changing the geometry constraint i can solve my problem but was not the solution as per the client.. :) –  Vicky Sep 1 '11 at 13:24
    
Underdark : it finally worked, previously i was using multi() function so i was getting error. Thanks for ST_Multi() function –  Vicky Sep 1 '11 at 13:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.