wiki:UsersWikiSplitPolygonWithLineString

Version 1 (modified by pierre, 15 years ago) ( diff )

Split Polygon With Line String

This example demonstrates how to Split a Polygon into multiple polygons using a Line.

James asks:

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
  • add to this your crossing linestring
  • geomunion the lines together.
  • polygonize the union-ed set

Thanks,

Jim

—THE BELOW solution may not work with holes —-

Kevin answers:

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

));

Result:

              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

Regina asks:

This doesn't work with holes does it?

Kevin answers

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;

Result:

                                 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)

----------------------------------------------------------------------

You can also make mosaic from polygons from one table. When you have for example this table

CREATE TABLE geoms
(
  id serial NOT NULL,
  geom geometry,
  CONSTRAINT geoms_pkey PRIMARY KEY (id),
  CONSTRAINT enforce_dims_geom CHECK (ndims(geom) = 2),
  CONSTRAINT enforce_srid_geom CHECK (srid(geom) = (-1))
)
WITH (OIDS=FALSE);

This is the query that returns mosaic:

SELECT geom FROM (SELECT (ST_Dump(g)).geom as geom FROM (
	SELECT polygonize(g) as g FROM (
		SELECT geomunion(boundary(geom)) as g FROM geoms
	) r
)t) a WHERE EXISTS (SELECT geom FROM geoms b WHERE contains(b.geom, pointonsurface(a.geom)));

Yo!Zik

Note: See TracWiki for help on using the wiki.