Changes between Version 3 and Version 4 of UsersWikiSplitPolygonWithLineString


Ignore:
Timestamp:
Apr 6, 2015 5:27:32 PM (5 years ago)
Author:
Mike Taves
Comment:

handle holes better, and better example

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiSplitPolygonWithLineString

    v3 v4  
    148148Yo!Zik
    149149
    150 == Function: ST_SplitPolygon ==
    151 For versions of PostGIS before 2.0, a custom function can be used to split polygons using a linestring.
     150== Function: `ST_SplitPolygon` ==
     151A custom function can be used to split a `[Multi]Polygon` with a `[Multi]LineString` blade.
    152152{{{
    153 CREATE OR REPLACE FUNCTION ST_SplitPolygon(poly geometry, blade geometry)
    154   RETURNS geometry AS
    155 'SELECT ST_Polygonize(ST_Union(ST_Boundary($1), $2))' LANGUAGE sql IMMUTABLE STRICT COST 100;
     153CREATE OR REPLACE FUNCTION ST_SplitPolygon(poly geometry, blade geometry) RETURNS geometry AS
     154'SELECT ST_Collect((d).geom)
     155FROM (SELECT ST_Dump(ST_Polygonize(ST_Union(ST_Boundary($1), $2))) AS d) f
     156WHERE ST_Area(ST_Intersection($1, (d).geom)) / ST_Area((d).geom) >= 1 - 1e-10;' LANGUAGE sql IMMUTABLE STRICT COST 100;
    156157}}}
    157158For example:
    158159{{{
    159 SELECT ST_AsText(ST_SplitPolygon(circle, line)) result
    160 FROM (SELECT
    161     ST_MakeLine(ST_MakePoint(10, 10),ST_MakePoint(190, 190)) line,
    162     ST_Buffer(ST_GeomFromText('POINT(100 90)'), 50) circle) f;
     160SELECT ST_AsText(ST_SplitPolygon(poly, blade)) AS result
     161FROM (
     162  SELECT 'POLYGON ((20 30, 100 180, 160 20, 20 30), (70 70, 92 105, 110 50, 70 70))'::geometry AS poly,
     163    'MULTILINESTRING ((150 90, 90 70, 60 20), (50 140, 90 70))'::geometry AS blade
     164) f;
    163165
    164166           result
    165167-----------------------------------------
    166 GEOMETRYCOLLECTION(
    167   POLYGON((150 90,...,150 90)),
    168   POLYGON((60.1371179574584 60.1371179574584,...,60.1371179574584 60.1371179574584)))
     168MULTIPOLYGON(((20 30,64.8275862068966 114.051724137931,80.4761904761905 86.6666666666667,
     169  70 70,85.3846153846154 62.3076923076923,64.1095890410959 26.8493150684932,20 30)),
     170((64.8275862068966 114.051724137931,100 180,135.555555555556 85.1851851851852,
     171  102.131147540984 74.0437158469945,92 105,80.4761904761905 86.6666666666667,
     172  64.8275862068966 114.051724137931)),
     173((135.555555555556 85.1851851851852,160 20,64.1095890410959 26.8493150684932,
     174  85.3846153846154 62.3076923076923,110 50,102.131147540984 74.0437158469945,
     175  135.555555555556 85.1851851851852)))
    169176}}}