Changes between Version 4 and Version 5 of UsersWikiBuildPolygonsWithLines


Ignore:
Timestamp:
Apr 9, 2015, 10:29:55 PM (9 years ago)
Author:
Mike Taves
Comment:

also ST_PolygonizeLinework

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiBuildPolygonsWithLines

    v4 v5  
    66#!sql
    77CREATE OR REPLACE FUNCTION ST_BuildAreaLinework(linework geometry) RETURNS geometry AS
    8 'WITH data AS (SELECT * FROM ST_Dump(ST_UnaryUnion($1)))
     8$BODY$WITH data AS (SELECT * FROM ST_Dump(ST_UnaryUnion($1)))
    99SELECT ST_SetSRID(ST_BuildArea(ST_Collect(geom)), ST_SRID($1))
    1010FROM
    11 (
     11( -- This is the noded linework, broken up where they cross each other
    1212  SELECT (ST_Dump(ST_Union(geom))).geom FROM data
    1313) t1,
    14 (
     14( -- This is a [MULTI]POINT of any crossings or closures
    1515  SELECT ST_Union(geom) AS pt
    1616  FROM (
     
    2121  ) s
    2222) t2
    23 WHERE ST_Intersects(ST_StartPoint(geom), pt) AND ST_Intersects(ST_EndPoint(geom), pt);' LANGUAGE sql IMMUTABLE;
     23WHERE ST_Intersects(ST_StartPoint(geom), pt) AND ST_Intersects(ST_EndPoint(geom), pt);$BODY$ LANGUAGE sql IMMUTABLE;
    2424}}}
    2525
     
    3030}}}
    3131
    32 Where the yellow lines are the input linework, and the yellow polygon with a hole is the result.
     32Where the blue lines are the input linework, and the yellow polygon with a hole is the result.
    3333
    3434[[Image(BuildAreaLinework_example.png)]]
     35
     36----
     37
     38A similar function could be used with ST_Polygonize
     39{{{
     40#!sql
     41CREATE OR REPLACE FUNCTION ST_PolygonizeLinework(linework geometry) RETURNS geometry AS
     42$BODY$WITH data AS (SELECT * FROM ST_Dump(ST_UnaryUnion($1)))
     43SELECT ST_SetSRID(ST_Polygonize(geom), ST_SRID($1))
     44FROM
     45( -- This is the noded linework, broken up where they cross each other
     46  SELECT (ST_Dump(ST_Union(geom))).geom FROM data
     47) t1,
     48( -- This is a [MULTI]POINT of any crossings or closures
     49  SELECT ST_Union(geom) AS pt
     50  FROM (
     51    SELECT ST_Intersection(A.geom, B.geom) AS geom
     52    FROM data A, data B
     53    WHERE A.path[1] < B.path[1] AND ST_Intersects(A.geom, B.geom)
     54    UNION SELECT ST_StartPoint(geom) FROM data WHERE ST_IsClosed(geom)
     55  ) s
     56) t2
     57WHERE ST_Intersects(ST_StartPoint(geom), pt) AND ST_Intersects(ST_EndPoint(geom), pt);$BODY$ LANGUAGE sql IMMUTABLE;
     58}}}
     59
     60For example:
     61{{{
     62#!sql
     63SELECT ST_PolygonizeLinework('MULTILINESTRING((50 40,50 230,300 230,270 60,40 60),(150 30,130 240),(191 155,240 190,250 150,190 170))');
     64}}}
     65
     66Where the blue lines are the input linework, and the yellow polygons (`GEOMETRYCOLLECTION(POLYGON((...)))`) are the result.
     67
     68[[Image(PolygonizeLinework_example.png)]]
    3569
    3670== External links ==