Changes between Version 4 and Version 5 of UsersWikiBuildPolygonsWithLines
- Timestamp:
- 04/09/15 22:29:55 (10 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
UsersWikiBuildPolygonsWithLines
v4 v5 6 6 #!sql 7 7 CREATE 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))) 9 9 SELECT ST_SetSRID(ST_BuildArea(ST_Collect(geom)), ST_SRID($1)) 10 10 FROM 11 ( 11 ( -- This is the noded linework, broken up where they cross each other 12 12 SELECT (ST_Dump(ST_Union(geom))).geom FROM data 13 13 ) t1, 14 ( 14 ( -- This is a [MULTI]POINT of any crossings or closures 15 15 SELECT ST_Union(geom) AS pt 16 16 FROM ( … … 21 21 ) s 22 22 ) t2 23 WHERE ST_Intersects(ST_StartPoint(geom), pt) AND ST_Intersects(ST_EndPoint(geom), pt); 'LANGUAGE sql IMMUTABLE;23 WHERE ST_Intersects(ST_StartPoint(geom), pt) AND ST_Intersects(ST_EndPoint(geom), pt);$BODY$ LANGUAGE sql IMMUTABLE; 24 24 }}} 25 25 … … 30 30 }}} 31 31 32 Where the yellowlines are the input linework, and the yellow polygon with a hole is the result.32 Where the blue lines are the input linework, and the yellow polygon with a hole is the result. 33 33 34 34 [[Image(BuildAreaLinework_example.png)]] 35 36 ---- 37 38 A similar function could be used with ST_Polygonize 39 {{{ 40 #!sql 41 CREATE OR REPLACE FUNCTION ST_PolygonizeLinework(linework geometry) RETURNS geometry AS 42 $BODY$WITH data AS (SELECT * FROM ST_Dump(ST_UnaryUnion($1))) 43 SELECT ST_SetSRID(ST_Polygonize(geom), ST_SRID($1)) 44 FROM 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 57 WHERE ST_Intersects(ST_StartPoint(geom), pt) AND ST_Intersects(ST_EndPoint(geom), pt);$BODY$ LANGUAGE sql IMMUTABLE; 58 }}} 59 60 For example: 61 {{{ 62 #!sql 63 SELECT 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 66 Where the blue lines are the input linework, and the yellow polygons (`GEOMETRYCOLLECTION(POLYGON((...)))`) are the result. 67 68 [[Image(PolygonizeLinework_example.png)]] 35 69 36 70 == External links ==