# [postgis-users] Splitting a Polygon with a Linestring

James Beaudoin jmbeaudoin at wisc.edu
Wed May 28 13:16:13 PDT 2008

```Thanks Kevin and Regina. I ended up using Kevin's example and it works
great.

Jim

Kevin Neufeld wrote:
> Things would get a little bit more complicated when holes are
> involved, but I don't think overly so.
>
> The polygonizer builds polygons from all linework - this includes
> building polygons for what should be holes.   What you would need to
> do as a post-process is remove all polygons that don't belong.  (In
> the following example, I use contains and pointonsurface to determine
> which polygons I should exclude from the final result.
>
> Here is another sample polygon splitting, this time the polygon has
> two holes where one is intersected by the splitting line.
>
> CREATE TABLE poly AS
>   SELECT 'POLYGON (( 1 1, 1 7, 7 7, 7 1, 1 1 ),
>                    ( 2 3, 4 3, 4 5, 2 5, 2 3 ),
>                    ( 5 5, 6 5, 6 6, 5 6, 5 5 ))'::geometry AS geom;
>
> CREATE TABLE line AS
>   SELECT 'LINESTRING (0 4, 8 4)'::geometry AS geom;
>
> CREATE TABLE split_polys AS
> SELECT geom FROM dump ((
>   SELECT polygonize(geomunion(boundary(poly.geom), line.geom))
>   FROM poly, line
> ));
>
> DELETE FROM split_polys a
> USING poly b
> WHERE NOT contains(b.geom, pointonsurface(a.geom));
>
> SELECT astext(geom) FROM split_polys;
>
>                                 astext
> ----------------------------------------------------------------------
>  POLYGON((1 1,1 4,2 4,2 3,4 3,4 4,7 4,7 1,1 1))
>  POLYGON((1 4,1 7,7 7,7 4,4 4,4 5,2 5,2 4,1 4),(5 5,6 5,6 6,5 6,5 5))
> (2 rows)
>
>
> Cheers,
> Kevin
>
>
> Obe, Regina wrote:
>>
>> Kevin,
>>
>> that looks neat.  I'll have to remember that one.
>>
>> I presume if you have a geometry with holes, this would not work.
>> How would you tackle,   if say you wanted to cut a donut with a line.
>>
>> Thanks,
>> Regina
>>
>> *From:* postgis-users-bounces at postgis.refractions.net on behalf of
>> Kevin Neufeld
>> *Sent:* Wed 5/28/2008 1:47 PM
>> *To:* PostGIS Users Discussion
>> *Subject:* Re: [postgis-users] Splitting a Polygon with a Linestring
>>
>> Right.
>>
>> Here's an example splitting a polygon with a line.  Note that the
>> dangling cutlines outside the polygon are quietly dropped.
>>
>>
>> SELECT astext(geom )
>> FROM dump ((
>>
>> SELECT polygonize(geomunion(boundary(poly), line)) AS mpoly
>> FROM
>>    (SELECT 'POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1 ))'::geometry AS poly)
>> AS a,
>>    (SELECT 'LINESTRING (0 2, 4 2)'::geometry AS line) AS b
>>
>> ));
>>
>>
>>               astext
>> --------------------------------
>>   POLYGON((1 1,1 2,3 2,3 1,1 1))
>>   POLYGON((1 2,1 3,3 3,3 2,1 2))
>> (2 rows)
>>
>> Cheers,
>> Kevin
>>
>>
>> James Beaudoin wrote:
>>  > I think the steps below will work but I have one question. I am
>> creating
>>  > an application that allows a user to split a polygon by drawing a
>>  > linestring across the polygon they wish to change. The crossing
>>  > linestring has parts that are outside the polygon and I want to
>> remove
>>  > that before I geomunion. What's the easiest way to do that?
>>  >
>>  > - extract the lines that make up the polygon
>>  > - geomunion the lines together.
>>  > - polygonize the union-ed set
>>  >
>>  > Thanks,
>>  >
>>  > Jim
>>  > _______________________________________________
>>  > postgis-users mailing list
>>  > postgis-users at postgis.refractions.net
>>  > http://postgis.refractions.net/mailman/listinfo/postgis-users
>> _______________________________________________
>> postgis-users mailing list
>> postgis-users at postgis.refractions.net
>> http://postgis.refractions.net/mailman/listinfo/postgis-users
>>
>> * The substance of this message, including any attachments, may be
>> confidential, legally privileged and/or exempt from disclosure
>> pursuant to Massachusetts law. It is intended solely for the
>> and delete the material from any computer. *
>>
>> * Help make the earth a greener place. If at all possible resist
>>
>> * *
>>
>> * *
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> postgis-users mailing list
>> postgis-users at postgis.refractions.net
>> http://postgis.refractions.net/mailman/listinfo/postgis-users
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
>

```