3

I want to split/cut polygon into 2 polygons by line.I am using PostGIS ST_Split functionality.Below is code tried so far with reference,passing two edge point coordinates creating line, by that line spiting polygon

select ST_Multi((ST_Split(geom,ST_MakeLine(ST_SetSRID(ST_MakePoint(77.708,13.164), 4326),ST_SetSRID(ST_MakePoint(77.71,13.163), 4326))))) as _geom from itpark_plot30

But its return same polygon.As show below image there is polygon called "A" and want to split into "B" and "C" but above code returning "A" only.

enter image description here

my polygon geom is:

0106000020E610000001000000010300000001000000210000007D392969716D5340756A4B77B2522A4044CA8553716D5340CFBD342472522A40D4E093A75C6D53408611CBA572522A403CDBCF6E5C6D5340EDCA61BD72522A40D199A8365C6D5340E547180173522A405054A4FF5B6D5340F9524B7073522A40D8A04ECA5B6D53405C39F90974522A40C85828975B6D534046AA85CC74522A409142B1665B6D53402F6129B675522A40A88D60395B6D5340B0409CC476522A407DD2A40F5B6D5340C7DB48F577522A403438E6E95A6D5340A5483B4579522A406C0380C85A6D5340E04D32B17A522A408407C7AB5A6D53401FBFC1357C522A40880F01945A6D5340ECC40DCF7D522A40C82969815A6D534045873A797F522A40385C2B745A6D5340600D303081522A40FB026A6C5A6D5340853D9AEF82522A40F45E376A5A6D5340AB5947B384522A40EACF2D7B5A6D534078AE0027B7522A40CC8385905A6D53405AD915A1F6522A40CB8385905A6D53405AD915A1F6522A4026F1DBA55A6D5340C320B21636532A408361F8BA5A6D534052752DDF74532A4078C105BB5A6D5340EBA7F30675532A400CDAF4D25A6D534004CBC433BC532A40D4283BCC5E6D5340A004DA1ABC532A4079B65FC2716D53406855F4A3BB532A4065E58FA9716D534092EB21E571532A4066E58FA9716D534092EB21E571532A4083A0DD93716D53406C49066831532A401F47757E716D534085A8F1C5F1522A407D392969716D5340756A4B77B2522A40

Result: with line start and end point are 77.7085,13.1633 and 77.7102,13.1633

enter image description here

0

2 Answers 2

5

Make sure your blade is precise, that is, its boundary (the start/end vertices) has to be at least exactly on the boundary of the polygon, or outside.

PostGIS stores floating point precision coordinates (up to 15 decimal digits), and its functions are sensitive to that precision up to the last digit; the point coordinates you provide are way less precise, and might just be inside the actual polygon by a fraction of a milimeter.


ST_Split always returns a GEOMETRYCOLLECTION, and your ST_Multi construct will not work; use ST_ColectionHomogenize to extract a Multi geometry from a (homogeneous) collection:

WITH
 poly AS (
  SELECT 'POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))'::GEOMETRY AS geom
 ),
 line AS (
  SELECT 'LINESTRING(0 1, 2 1)'::GEOMETRY AS geom
 )
SELECT ST_AsText( ST_CollectionHomogenize( ST_Split(a.geom, b.geom) ) ) AS geom
FROM   poly AS a,
       line AS b
;
                             geom                              
---------------------------------------------------------------
 MULTIPOLYGON(((0 0,0 1,2 1,2 0,0 0)),((0 1,0 2,2 2,2 1,0 1)))
(1 row)

If you want to have singleton polygons extracted from the GEOMETRYCOLLECTION, simply dump them:

WITH                                                        
 poly AS (
  SELECT 'POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))'::GEOMETRY AS geom
 ),
 line AS (
  SELECT 'LINESTRING(0 1, 2 1)'::GEOMETRY AS geom
 )
SELECT ST_AsText( (ST_Dump( ST_Split(a.geom, b.geom) )).geom ) AS geom
FROM   poly AS a,
       line AS b
;
              geom              
--------------------------------
 POLYGON((0 0,0 1,2 1,2 0,0 0))
 POLYGON((0 1,0 2,2 2,2 1,0 1))
(2 rows)

UPDATE:

These are your geometries:

enter image description here

To cut that polygon, the line needs to fully cross it!

3
  • 3
    Great work! I was pretty sure the blade line is incorrect...
    – Jane
    Jun 19, 2019 at 9:55
  • @Jane yeah it just had to be...sry for spamming your answer ,)
    – geozelot
    Jun 19, 2019 at 9:58
  • 1
    An excellent example of the case when the OP is not able to accept the correct answer !!! Jun 19, 2019 at 11:46
3

I have tested your query:

Have a look at this

my poly

Then, I get split geometries:

select ST_AsText((ST_Dump(ST_Multi((ST_Split(geom,ST_MakeLine(ST_SetSRID(ST_MakePoint(104.222,53.33), 4326),ST_SetSRID(ST_MakePoint(104.455,51.15), 4326))))))).geom) as _geom from surf;

The result is:

result image

So, maybe the thing is that your initial poly is not the WGS84 clearly defined?and they are not spatially intersected. My polygon (select st_asewkt(geom) from surf) is

SRID=4326;POLYGON((103.783338 53.15,105.216672 52.95,105.333338 52.3,105.416671 52.033334,105.161671 51.576667,104.603338 51.451667,103.683338 51.716667,103.266672 52.2,103.200005 52.433334,103.215005 52.628334,103.900005 52.483334,103.863338 52.613334,103.666672 53.15,103.783338 53.15))

Secondly, is your blade correct? I mean perhaps polygon is not covered by your blade line

Your query is ok. Check your geometries

9
  • SRID is 4326 still not getting two polygons Jun 19, 2019 at 9:15
  • I want split polygons to be insert as two separate rows in table Jun 19, 2019 at 9:19
  • 1
    @veenahosur that's a slightly different requirement than your query suggests, you might want to edit your question to insert that information.
    – geozelot
    Jun 19, 2019 at 9:21
  • insertion is ok first i have to get two polygons that is only not working Jun 19, 2019 at 9:23
  • I tried with your given example in your case its working.but what is issue in my case not getting Jun 19, 2019 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.