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

Obe, Regina robe.dnd at cityofboston.gov
Wed May 28 05:16:14 PDT 2008

```James,

This is a very interesting problem.  I think the reason Polygonize
doesn't work is that it tries to formulate polygons from the individual
linestrings so since your linestrings are not truly spliced (e.g. if the
exterior ring was cut into 2 lines it would work, but it is still one
line).

Below is the way that comes to mind for splicing.  This is a way that
comes to mind for me but probably someone has a better idea.

The basic approach is as follows:

1) Make my dividing line into a polygon by buffering it ever so
slightly. This is because the difference between a polygon and a
bisecting line is still the polygon, but the difference between a
polygon and a bisecting line is 2 polygons.  - lets call this buffered
poylgon the_knife

2) Difference out the knife from my polygon - this will bisect my
polygon into 2 polygons resulting in a multipolygon since it will be
missing my knife thickness so to speak.

3) Now that I have 2 polygons, readd to each what I had taken out.
(Note my below assumes I can have up to 30 polygons - in theory you
probably only have

Below is a self-contained example that bisects a polygon along the
bounding box diagonal.

SELECT n As gid, ST_Union(ST_GeometryN(div_geom, n), the_knife) as
the_geom
FROM
(SELECT ST_Difference(sol.the_geom,ST_Buffer(sol.div_line,0.001)) As
div_geom, ST_Buffer(sol.div_line,0.001) As the_knife
FROM
(SELECT foo.the_geom,
ST_Intersection(foo.the_geom,ST_MakeLine(ST_MakePoint(ST_XMin(foo.the_ge
om), ST_YMax(foo.the_geom)), ST_MakePoint(ST_XMAX(foo.the_geom),
ST_YMIN(foo.the_geom)))) as div_line
FROM (SELECT
64A7274160A9D721D38846418031318C3EA72741A06C25281C894641802EFEE5BDA72741
A006A0851989464180D67296DCA7274180FCC089D6884641' As geometry) As
the_geom) As foo) As sol) As sol2 CROSS JOIN generate_series(1,30) n
WHERE n <= ST_NumGeometries(sol2.div_geom)

Hope that helps,
Regina

-----Original Message-----
From: postgis-users-bounces at postgis.refractions.net
[mailto:postgis-users-bounces at postgis.refractions.net] On Behalf Of
James Beaudoin
Sent: Tuesday, May 27, 2008 6:47 PM
To: PostGIS Users Discussion
Subject: Re: [postgis-users] Splitting a Polygon with a Linestring

I am having issues with splitting a polygon with a line. I do the
intersection, then extract the lines of the polygon. Then I attempt to
geomunion the LINESTRINGS together and get something like this

('MULTILINESTRING((597002.205895278 324851.044018193,597118.930687031
324409.678399375,596848.325021875 323488.467624375,596980.570809376
323238.272891265),(598023.084373772 324919.277180544,598001.418581752
... and so on.

When I try to ST_Polygonize I get only get the original polygon and not
the line. Is there something I am doing wrong. Below is a very general
overview of my steps

select
ST_AsHEXEWKB(ST_Intersection(geometryfromtext('LINESTRING(596934.6885320
31
325106.344047969, 597118.930687031 324409.678399375, 596848.325021875
323488.467624375, 597061.355013594 323085.437910313, 597061.355013594
323085.437910313)',3071), the_geom)) from mcd_redist

select ST_AsHEXEWKB(ST_ExteriorRing(ST_GeometryN(the_geom,
generate_series(1,ST_NumGeometries(the_geom))))) from mcd_redist

select ST_Astext(geomunion(" + myline + "," + origpoly + "))

select ST_asText(ST_Polygonize" + thedata + "))

Thanks,

Jim
Andy Anderson wrote:
> If you use ST_Intersection(geometry_line, geometry_polygon) to return
> "a geometry that represents the point set intersection of the
> Geometries", the user-drawn line will be truncated to just the
> portion(s) that fall within the polygon. My tests results are either a

> LINESTRING or MULTILINESTRING, though where I expect a POINT it
> returns GEOMETRYCOLLECTION EMPTY (possibly due to round-off).
>
> (P.S. you'll also need to allow for the case where the user leaves one

> end of the line inside the polygon, by extending it to the edge I
> would imagine.)
>
> -- Andy
>
> On May 21, 2008, at 2:21 PM, 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