Changes between Initial Version and Version 1 of UsersWikiSplitPolygonWithLineString


Ignore:
Timestamp:
Apr 14, 2009, 9:23:23 AM (15 years ago)
Author:
pierre
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiSplitPolygonWithLineString

    v1 v1  
     1= Split Polygon With Line String =
     2
     3
     4This example demonstrates how to Split a Polygon into multiple polygons using a Line.
     5
     6'''[http://postgis.refractions.net/pipermail/postgis-users/2008-May/019750.html James asks]:'''
     7
     8I think the steps below will work but I have one question. I am creating
     9an application that allows a user to split a polygon by drawing a
     10linestring across the polygon they wish to change. The crossing
     11linestring has parts that are outside the polygon and I want to remove
     12that before I geomunion. What's the easiest way to do that?
     13
     14- extract the lines that make up the polygon
     15- add to this your crossing linestring
     16- geomunion the lines together.
     17- polygonize the union-ed set
     18
     19Thanks,
     20
     21Jim
     22
     23--THE BELOW solution may not work with holes ---
     24
     25'''[http://postgis.refractions.net/pipermail/postgis-users/2008-May/019902.html Kevin answers]:'''
     26
     27Here's an example splitting a polygon with a line.  Note that the
     28dangling cutlines outside the polygon are quietly dropped.
     29
     30
     31{{{
     32
     33SELECT astext(geom )
     34FROM dump ((
     35
     36SELECT polygonize(geomunion(boundary(poly), line)) AS mpoly
     37FROM
     38   (SELECT 'POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1 ))'::geometry AS poly) AS a,
     39   (SELECT 'LINESTRING (0 2, 4 2)'::geometry AS line) AS b
     40
     41));
     42
     43}}}
     44
     45
     46Result:
     47
     48
     49{{{
     50              astext
     51--------------------------------
     52  POLYGON((1 1,1 2,3 2,3 1,1 1))
     53  POLYGON((1 2,1 3,3 3,3 2,1 2))
     54(2 rows)
     55
     56}}}
     57
     58
     59Cheers,
     60Kevin
     61
     62'''[http://postgis.refractions.net/pipermail/postgis-users/2008-May/019903.html  Regina asks]:'''
     63
     64This doesn't work with holes does it?
     65
     66'''[http://postgis.refractions.net/pipermail/postgis-users/2008-May/019904.html Kevin answers]'''
     67
     68Things would get a little bit more complicated when holes are involved,
     69but I don't think overly so.
     70
     71The polygonizer builds polygons from all linework - this includes
     72building polygons for what should be holes.   What you would need to do
     73as a post-process is remove all polygons that don't belong.  (In the
     74following example, I use contains and pointonsurface to determine which
     75polygons I should exclude from the final result.
     76
     77Here is another sample polygon splitting, this time the polygon has two
     78holes where one is intersected by the splitting line.
     79
     80
     81{{{
     82CREATE TABLE poly AS
     83   SELECT 'POLYGON (( 1 1, 1 7, 7 7, 7 1, 1 1 ),
     84                    ( 2 3, 4 3, 4 5, 2 5, 2 3 ),
     85                    ( 5 5, 6 5, 6 6, 5 6, 5 5 ))'::geometry AS geom;
     86
     87CREATE TABLE line AS
     88   SELECT 'LINESTRING (0 4, 8 4)'::geometry AS geom;
     89
     90CREATE TABLE split_polys AS
     91SELECT geom FROM dump ((
     92   SELECT polygonize(geomunion(boundary(poly.geom), line.geom))
     93   FROM poly, line
     94));
     95
     96DELETE FROM split_polys a
     97USING poly b
     98WHERE NOT contains(b.geom, pointonsurface(a.geom));
     99
     100SELECT astext(geom) FROM split_polys;
     101
     102}}}
     103
     104Result:
     105
     106{{{
     107                                 astext
     108----------------------------------------------------------------------
     109  POLYGON((1 1,1 4,2 4,2 3,4 3,4 4,7 4,7 1,1 1))
     110  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))
     111(2 rows)
     112
     113----------------------------------------------------------------------
     114
     115}}}
     116
     117You can also make mosaic from polygons from one table.
     118When you have for example this table
     119
     120
     121{{{
     122CREATE TABLE geoms
     123(
     124  id serial NOT NULL,
     125  geom geometry,
     126  CONSTRAINT geoms_pkey PRIMARY KEY (id),
     127  CONSTRAINT enforce_dims_geom CHECK (ndims(geom) = 2),
     128  CONSTRAINT enforce_srid_geom CHECK (srid(geom) = (-1))
     129)
     130WITH (OIDS=FALSE);
     131
     132}}}
     133
     134This is the query that returns mosaic:
     135
     136
     137{{{
     138SELECT geom FROM (SELECT (ST_Dump(g)).geom as geom FROM (
     139        SELECT polygonize(g) as g FROM (
     140                SELECT geomunion(boundary(geom)) as g FROM geoms
     141        ) r
     142)t) a WHERE EXISTS (SELECT geom FROM geoms b WHERE contains(b.geom, pointonsurface(a.geom)));
     143
     144}}}
     145
     146Yo!Zik