[postgis-users] Function to convert Polygons and MultiPolygons to LineStrings - struggles with path in St_Dumps
Ruth Simm
goldfish_and_muskie at yahoo.com
Sun Jun 14 12:35:14 PDT 2015
I am trying to script a function to return just the Linestring/MLinestring geom.
My function will be able to iterate through the geom of polygons and mpolys and return the geom output (only) of st_dump(st_boundary(the_geom)).
Problem is I am struggling with iterating through the 'path' array....and I don't seem to have a good sandbox way of capturing my errors (is there anything -- similar to SQLFIDDLE -- that works with the postgis ext?).
EX 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);
Function:
CREATE OR REPLACE FUNCTION LinesFromPolygon2(polygon geometry) RETURNS SETOF geometry_dump AS $BODY$DECLARE m integer; g geometry; p geometry_dump%ROWTYPE; BEGINIF GeometryType($1) LIKE 'MULTI%' THEN FOR m IN SELECT generate_series(1, ST_NumGeometries($1)) LOOP p.path[1] := m; -- use to store Multipolygon number g := ST_Dump(ST_Boundary($1, m)); RETURN NEXT p; END LOOP;ELSE -- It is not a MULTI- geometry g := ST_Dump(ST_Boundary($1)); END IF; RETURN; END;$BODY$LANGUAGE plpgsql ;
ERROR:
SELECT id, name, LinesFromPolygon2(the_geom) FROM poly_and_multipoly;
ERROR: parse error - invalid geometryHINT: "({" <-- parse error at position 2 within geometryCONTEXT: PL/pgSQL function linesfrompolygon2(geometry) line 13 at assignment
********** Error **********
ERROR: parse error - invalid geometrySQL state: XX000Hint: "({" <-- parse error at position 2 within geometryContext: PL/pgSQL function linesfrompolygon2(geometry) line 13 at assignment
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20150614/4eee9bff/attachment.html>
More information about the postgis-users
mailing list