[postgis-users] Learn how to create LineStrings from Polygons and Multipolygons

Ruth Simm goldfish_and_muskie at yahoo.com
Fri Jun 12 16:51:35 PDT 2015


Hello,
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.
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.
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?
SELECT a.id, a.name, ST_AsEWKT(st_exteriorring(a.the_geom))FROM poly_and_multipoly AS aORDER BY a.name;
Test data:
CREATE TABLE poly_and_multipoly (  "id" SERIAL NOT NULL PRIMARY KEY,  "name" char(1) NOT NULL,  "the_geom" geometry NOT NULL);-- add data, A is a polygon, B is a multipolygonINSERT INTO poly_and_multipoly (name, the_geom) VALUES (    'A', 'POLYGON((7.7 3.8,7.7 5.8,9.0 5.8,7.7 3.8))'::geometry    ), (    'B',    '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);
Outcome I am aiming for:
id;name;st_asewkt1;"A";"LINESTRING(7.7 3.8,7.7 5.8,9 5.8,7.7 3.8)"
2;"B";"LINESTRING(0 0,4 0,4 4,0 4,0 0)"2;"B";"LINESTRING(1 1,2 1,2 2,1 2,1 1)"2;"B";"LINESTRING(-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)"

a: http://spatialdbadvisor.com/postgis_tips_tricks/128/exploding-a-linestring-or-polygon-into-individual-vectors-in-postgis b: Get the lines between all points of a polygon in PostGis : avoid nested loop?
|   |
|   |  |   |   |   |   |   |
| Get the lines between all points of a polygon in PostGis...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 :... |
|  |
| View on gis.stackexchange.com | Preview by Yahoo |
|  |
|   |

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20150612/b948819d/attachment.html>


More information about the postgis-users mailing list