[postgis-users] Create POLYGON from Several LINESTRINGs

Avery Penniston apenniston at geo-comm.com
Tue Jun 22 12:23:37 PDT 2010


Here is a sample function to help illustrate the issue (you can perform
a simple test by calling 'Select wedgefunc(1,1,1) as test;'):

 

CREATE OR REPLACE FUNCTION wedgefunc(outerradius float, startangle
float, openingangle float)

  RETURNS text AS

$BODY$

  DECLARE

    startchord geometry;

    midchord geometry;

    endchord geometry;

    revstartchord geometry;

    outerarc text;

    outerarcline geometry;

    polyborder geometry;

    poly geometry;

 

  BEGIN

  -- Rotate a horizontal line to form chords - the 'mid' chord is to
help form the CIRCULARLINESTRING

                startchord :=
st_rotate(st_makeline(st_point(0,0),st_point(outerradius,0)),startangle)
;

                midchord :=
st_rotate(st_makeline(st_point(0,0),st_point(outerradius,0)),startangle
+ (.5 * openingangle));

                endchord :=
st_rotate(st_makeline(st_point(0,0),st_point(outerradius,0)),startangle
+  openingangle);

                

  --Make a reversal of the start chord - just in case the points need to
be in order when putting together the poly

                revstartchord := st_reverse(startchord); 

                

  --Create an arc line by passing the endpoints of the endchord,
midchord, and startchord

                outerarc :=
'CIRCULARSTRING('||st_x(st_endpoint(endchord))||'
'||st_y(st_endpoint(endchord))||','||st_x(st_endpoint(midchord))||'
'||st_y(st_endpoint(midchord))||','||st_x(st_endpoint(startchord))||'
'||st_y(st_endpoint(startchord))||')';

                outerarcline := st_curvetoline(outerarc);

 

  --Create the single LINESTRING representing the border by combining
the individual LINESTRINGS

                polyborder :=
st_union(st_union(revstartchord,endchord),outerarcline);

                --polyborder :=
st_collect(st_collect(revstartchord,endchord),outerarcline);

 

  --Debugging

                --return st_astext(startchord);

                --return st_astext(midchord);

                --return st_astext(endchord);

                --return st_astext(revstartchord);

                --return st_astext(outerarcline);

                --return st_astext(polyborder);

 

  --Create the POLYGON geometry and return it                

                poly := st_polygon(polyborder,-1);

                return st_astext(poly);

 

  END;

 

  $BODY$

  LANGUAGE 'plpgsql' IMMUTABLE;

 

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


More information about the postgis-users mailing list