<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_1_1434151953700_2328">Hello,</div><div id="yui_3_16_0_1_1434151953700_2328"><br></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr">I have a table full of Polygons and Multipolygons, and I would like to learn how to break these objects into Linestrings (or to be more literal - rings). There seems to be a host of options for going from LineString to Polygon (ST_MakePolygon) - but not much for going the other way.</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr"><br></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr">I found some examples [a][b] but they are old and a bit over my head. Not to mention that example [a] throws a bunch of syntax errors.</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr"><br></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr">I recognize that ST_ExteriorRing will only work on polygons...but is there a way to alter this query a bit so that it can work on Multipolygons as well as Polygons?</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr"><br></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">SELECT a.id, a.name, ST_AsEWKT(st_exteriorring(a.the_geom))</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">FROM poly_and_multipoly AS a</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">ORDER BY a.name;</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style=""><br></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">Test data:</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style=""><br></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">CREATE TABLE poly_and_multipoly (</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">  "id" SERIAL NOT NULL PRIMARY KEY,</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">  "name" char(1) NOT NULL,</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">  "the_geom" geometry NOT NULL</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">);</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">-- add data, A is a polygon, B is a multipolygon</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">INSERT INTO poly_and_multipoly (name, the_geom) VALUES (</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">    'A', 'POLYGON((7.7 3.8,7.7 5.8,9.0 5.8,7.7 3.8))'::geometry</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">    ), (</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">    'B',</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">    'MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))'::geometry</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">);</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style=""><br></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">Outcome I am aiming for:</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style=""><br></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">id;name;st_asewkt</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">1;"A";"LINESTRING(7.7 3.8,7.7 5.8,9 5.8,7.7 3.8)"<br></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">2;"B";"LINESTRING(0 0,4 0,4 4,0 4,0 0)"</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">2;"B";"LINESTRING(1 1,2 1,2 2,1 2,1 1)"</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">2;"B";"LINESTRING(-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)"</div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style=""><br></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr"><br></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr">a: <a href="http://spatialdbadvisor.com/postgis_tips_tricks/128/exploding-a-linestring-or-polygon-into-individual-vectors-in-postgis" id="yui_3_16_0_1_1434151953700_2554">http://spatialdbadvisor.com/postgis_tips_tricks/128/exploding-a-linestring-or-polygon-into-individual-vectors-in-postgis</a> </div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style="">b: <a href="http://gis.stackexchange.com/questions/58534/get-the-lines-between-all-points-of-a-polygon-in-postgis-avoid-nested-loop" id="yui_3_16_0_1_1434151953700_2760">Get the lines between all points of a polygon in PostGis : avoid nested loop?</a></div><div style="width:450px; font-family: 'Georgia', 'Times', 'Times New Roman', 'serif';margin-top:5px; margin-bottom: 5px; background-color: #ffffff;" contenteditable="false" id="enhancrCard_0" class="link-enhancr-attachment link-enhancr-element "><table class="link-enhancr-element" cellspacing="0" cellpadding="0" border="0" style="width:450px; height:170px; position: relative; display: block;" id="yui_3_16_0_1_1434151953700_2839"><tbody id="yui_3_16_0_1_1434151953700_2838"><tr class="link-enhancr-element"><td class="link-enhancr-element" colspan="8" style="height: 1px; background-color: #e5e5e5; font-size: 1px; border-collapse: collapse;"><div class="link-enhancr-element" style="height: 1px; background-color: #e5e5e5; font-size: 1px; line-height:0px;"> </div></td></tr><tr class="link-enhancr-element" id="yui_3_16_0_1_1434151953700_2842"><td rowspan="5" class="link-enhancr-element" style="width: 1px; background-color: #e5e5e5; font-size: 1pt; border-collapse: collapse;"><div class="link-enhancr-element" style="width: 1px; background-color: #e5e5e5; font-size: 1pt;"> </div></td><td class="link-enhancr-element link-enhancr-image-wrap" rowspan="5" style="vertical-align: middle; width: 168px; height: 168px; background-color: #000000;border-collapse: collapse;" id="yui_3_16_0_1_1434151953700_2846"><div class="link-enhancr-element" style="width: 168px;" align="center" id="yui_3_16_0_1_1434151953700_2845"><a href="http://gis.stackexchange.com/questions/58534/get-the-lines-between-all-points-of-a-polygon-in-postgis-avoid-nested-loop" class="link-enhancr-card-urlWrapper link-enhancr-element" style="text-decoration: none !important; color: #000000 !important;" id="yui_3_16_0_1_1434151953700_2844"><img alt="image" src="http://cdn.sstatic.net/gis/img/apple-touch-icon@2.png?v=54e3ab1edcf3&a" class="link-enhancr-thumbnail-image link-enhancr-element yahoo-ignore-inline-image" width="168" height="168" style="display: block; margin: auto;" data-id="f821ae71-1ed1-93cc-5222-bf45243ebb43" id="yui_3_16_0_1_1434151953700_2843"></a></div></td><td rowspan="5" class="link-enhancr-element" style="width: 1px; background-color: #e5e5e5; font-size: 0pt; border-collapse: collapse;"><div class="link-enhancr-element" style="width: 1px; background-color: #e5e5e5; font-size: 1pt;"> </div></td><td rowspan="5" class="link-enhancr-element" style="width: 14px; background-color: #ffffff; font-size: 0pt; border-collapse: collapse;" id="yui_3_16_0_1_1434151953700_2841"><div class="link-enhancr-element" style="width: 14px; background-color: #ffffff; font-size: 14pt;"> </div></td><td colspan="2" class="link-enhancr-element" style="height: 6px; background-color: #ffffff; font-size: 0pt; border-collapse: collapse;"><div class="link-enhancr-element" style="height: 6px; background-color: #ffffff; font-size: 6pt;"> </div></td><td rowspan="5" class="link-enhancr-element" style="width: 20px; background-color: #ffffff; font-size: 0pt; border-collapse: collapse;"><div class="link-enhancr-element" style="width: 20px; background-color: #ffffff; font-size: 20pt;"> </div></td><td class="link-enhancr-element" rowspan="5" width="1" style="width: 1px; background-color: #e5e5e5; font-size: 1pt; border-collapse: collapse;"><div class="link-enhancr-element" style="width: 1px; background-color: #e5e5e5; font-size: 1pt;"> </div></td></tr><tr id="yui_3_16_0_1_1434151953700_2837"><td class="link-enhancr-element" colspan="2" style="width: 100%; vertical-align: middle; font-family: 'Georgia', 'Times', 'Times New Roman', 'serif';" id="yui_3_16_0_1_1434151953700_2836"><div class="link-enhancr-text-part link-enhancr-element" style="line-height:16.5px; background-color: #ffffff; height: 135px; width: 245px;" id="yui_3_16_0_1_1434151953700_2835"><div class="link-enhancr-element" style="word-wrap: break-word; word-break: break-all;" id="yui_3_16_0_1_1434151953700_2834"><span class="link-enhancr-element icon  icon-shrink link-enhancr-toggle"></span><span class="link-enhancr-element icon icon-close link-enhancr-delete"></span><a href="http://gis.stackexchange.com/questions/58534/get-the-lines-between-all-points-of-a-polygon-in-postgis-avoid-nested-loop" class="link-enhancr-card-urlWrapper link-enhancr-element" style="text-decoration: none !important; color: #000000 !important; line-height: 100%; font-size: 18px; display: block;" id="yui_3_16_0_1_1434151953700_2833"><span class="link-enhancr-element link-enhancr-card-title" style="margin: 0; font-weight: normal;margin-bottom: 3px; font-size: 18px; line-height: 21px; max-height: 43px; color: #000000; overflow: hidden !important; display: inline-block;" id="yui_3_16_0_1_1434151953700_2832">Get the lines between all points of a polygon in PostGis...</span></a><div style="font-size: 13px; line-height: 20px; color: #999999; max-height: 81px; font-family: 'Georgia', 'Times', 'Times New Roman', 'serif';overflow: hidden;" class="link-enhancr-card-description link-enhancr-element" id="yui_3_16_0_1_1434151953700_2840">I am trying to get all the lines between the points of a polygon, by writing PostGreSQL functions. I have written the two following functions that do the job fine :...</div></div></div></td></tr><tr><td colspan="2" class="link-enhancr-element" style="height: 4px; background-color: #ffffff; font-size: 0pt; border-collapse: collapse;"><div class="link-enhancr-element" style="height: 4px; background-color: #ffffff; font-size: 4pt;"></div></td></tr><tr><td class="link-enhancr-element" style="vertical-align: middle; font-family: 'Arial', 'Helvetica Neue', 'Helvetica', 'sans-serif';"><div class="link-enhancr-element" style="font-size: 0pt;"><a href="http://gis.stackexchange.com/questions/58534/get-the-lines-between-all-points-of-a-polygon-in-postgis-avoid-nested-loop" class="link-enhancr-card-url link-enhancr-element" style="color: black; text-decoration: none !important;cursor:pointer !important;" target="_blank"><span class="link-enhancr-element link-enhancr-view-on" style="display: inline-block; line-height: 11px; max-width: 145px; min-width: 85px; overflow: hidden; max-height: 13px; word-break: break-all;" id="yui_3_16_0_1_1434151953700_2775"><span class="link-enhancr-element link-enhancr-mobile-no-resize" style="vertical-align:middle; font-size: 9px; line-height: 11px; color: #999999; -moz-text-size-adjust: none; -ms-text-size-adjust: none; -webkit-text-size-adjust:none; text-size-adjust:none;">View on <span style="font-weight: bold" class="link-enhancr-view-on-domain">gis.stackexchange.com</span></span></span></a></div></td><td class="link-enhancr-element" style="vertical-align: middle; width: 100px; font-family: 'Arial', 'Helvetica Neue', 'Helvetica', 'sans-serif';"><div class="link-enhancr-element link-enhancr-preview-wrapper" style="max-width: 100px; min-width: 80px; overflow: hidden; text-align: right; line-height: 11px; max-height: 13px; font-size: 0pt;"><span class="link-enhancr-element link-enhancr-preview-by link-enhancr-mobile-no-resize" style="vertical-align:middle; font-size: 9px; line-height: 11px; color: #999999; -moz-text-size-adjust: none; -ms-text-size-adjust: none; -webkit-text-size-adjust:none; text-size-adjust:none;">Preview by Yahoo</span></div></td></tr><tr><td colspan="2" class="link-enhancr-element" style="height: 9px; background-color: #ffffff; font-size: 0pt; border-collapse: collapse;"><div class="link-enhancr-element" style="height: 9px; background-color: #ffffff; font-size: 9pt;"></div></td></tr><tr class="link-enhancr-element"><td class="link-enhancr-element" colspan="8" style="height: 1px; background-color: #e5e5e5; font-size: 1px; border-collapse: collapse;"><div class="link-enhancr-element" style="height: 1px; background-color: #e5e5e5; font-size: 1px; line-height:0px"> </div></td></tr></tbody></table></div><div id="yui_3_16_0_1_1434151953700_2328" dir="ltr" class="" style=""> </div></div></body></html>