# [postgis-users] Convert MULTIPOINT to POLYGON

Ralf yukyuk at gmx.com
Mon Jul 14 11:33:48 PDT 2008

```Thanks Regina, it totally works!

--Ralf

Obe, Regina wrote:
> Ralf,
>
> Have you tried
>
>  ST_MakePolygon(ST_LineFromMultiPoint(multipoint))
>
> Hope that helps,
> Regina
>
>
> -----Original Message-----
> From: postgis-users-bounces at postgis.refractions.net
> [mailto:postgis-users-bounces at postgis.refractions.net] On Behalf Of Ralf
> Sent: Monday, July 14, 2008 12:29 PM
> To: postgis-users at postgis.refractions.net
> Subject: [postgis-users] Convert MULTIPOINT to POLYGON
>
> Hey! Is there a simple way to create a polygon out of a multipoint? I
> have a table with points (nodes) and another table ways. Every single
> way is defined through two or more nodes. The two tables are connected
> through way_nodes (it's an import from openstreetmap.org using
> osmosis!). I figured out how to get all ways that are closed:
>
> SELECT ways.id FROM ways WHERE ST_IsClosed( (SELECT
> ST_LineFromMultiPoint( Collect(n.geom) ) FROM nodes n LEFT JOIN
> way_nodes wn ON n.id=wn.node_id WHERE ways.id=wn.way_id) )
>
> Now I am trying to store these closed ways as a polygon into another,
> new table. While doing that, I have to convert the MULTIPOINT that is
> returned by the Collect()-function into a POLYGON:
>
> INSERT INTO way_geometry SELECT ways.id,
>   ( SELECT 'GeomFromText(\'' || replace( astext( Collect(nodes.geom) ),
> 'MULTIPOINT', 'POLYGON(') || ')\', 4326)' FROM nodes
>   LEFT JOIN way_nodes ON nodes.id=way_nodes.node_id WHERE
> way_nodes.way_id=ways.id
>   )
> FROM ways
> WHERE ST_IsClosed( (SELECT ST_LineFromMultiPoint( Collect(n.geom) ) FROM
> nodes n LEFT JOIN way_nodes wn ON n.id=wn.node_id WHERE
> ways.id=wn.way_id) )
> AND ST_NumPoints( (SELECT ST_LineFromMultiPoint( Collect(n.geom) ) FROM
> nodes n LEFT JOIN way_nodes wn ON n.id=wn.node_id WHERE
> ways.id=wn.way_id) ) >= 3
> ;
>
> This always responds the error "parse error - invalid geometry". When I
> INSERT one of these lines by hand everything works fine. Is there
> another function to convert a MULTIPOINT into a POLYGON, maybe without
> using AsText()? Or does anyone have another idea to deal with this?
>
> Thanks a lot, Ralf
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
> -----------------------------------------
> The substance of this message, including any attachments, may be
> confidential, legally privileged and/or exempt from disclosure
> pursuant to Massachusetts law. It is intended