2

This code splits into NS direction but I need to split with WE direction (horizontally):

WITH foo AS (
SELECT id,name,geom, blade
FROM admin4
CROSS JOIN LATERAL ( SELECT ST_xMin(geom) + (ST_xMax(geom) - ST_xMin(geom)) / 2 ) AS axis(x)
CROSS JOIN LATERAL ST_MakeLine(
ST_MakePoint(axis.x, ST_yMin(geom)),
ST_MakePoint(axis.x, ST_yMax(geom))
) AS blade where id<5
)
SELECT id,name,geom,    blade,   ST_Split(geom, st_Setsrid(blade,4326)) FROM foo;
1
  • 2
    The assumption that midpoint division will result in equal areas is not generally applicable. Even if you alter the line orientation, you might not achieve your goal
    – Vince
    Dec 20 '21 at 13:05
2

There is no area involved in your query. As it is, it finds the mid point of the geometry extent and draws a vertical line.

Here it is for an horizontal line:

WITH foo AS (
 SELECT id,name,geom, blade
 FROM admin4
  CROSS JOIN LATERAL ( SELECT ST_yMin(geom) + (ST_yMax(geom) - ST_yMin(geom)) / 2 ) AS axis(y)
  CROSS JOIN LATERAL ST_MakeLine(
    ST_MakePoint(ST_xMin(geom),axis.y),
    ST_MakePoint(ST_xMax(geom),axis.y)
  ) AS blade where id<5
)
SELECT id,name,geom,    blade,   ST_Split(geom, st_Setsrid(blade,4326)) FROM foo;

The output, as in the OP, will be for each row a geometry collection containing the two 1/2 polygons. Here is an example:

WITH admin4(id,name,geom) as (
    values (1,'one',st_Setsrid('polygon((0 0,0 1,1 1,1 0,0 0))'::geometry,4326)),
     (2,'two',st_Setsrid('polygon((10 10,10 11,11 11,11 10,10 10))'::geometry,4326))
    ),
foo AS (
 SELECT id,name,geom, blade
 FROM admin4
  CROSS JOIN LATERAL ( SELECT ST_yMin(geom) + (ST_yMax(geom) - ST_yMin(geom)) / 2 ) AS axis(y)
  CROSS JOIN LATERAL ST_MakeLine(
    ST_MakePoint(ST_xMin(geom),axis.y),
    ST_MakePoint(ST_xMax(geom),axis.y)
  ) AS blade where id<5
)
SELECT id,name,--geom,    blade,   
    st_asText(ST_Split(geom, st_Setsrid(blade,4326)) )
FROM foo;
 id | name |                                                    st_astext                                               
----+------+-----------------------------------------------------------------------------------------------------------------
  1 | one  | GEOMETRYCOLLECTION(POLYGON((0 0,0 0.5,1 0.5,1 0,0 0)),POLYGON((0 0.5,0 1,1 1,1 0.5,0 0.5)))
  2 | two  | GEOMETRYCOLLECTION(POLYGON((10 10,10 10.5,11 10.5,11 10,10 10)),POLYGON((10 10.5,10 11,11 11,11 10.5,10 10.5)))
(2 rows)
7
  • Are you sure that by repeating the failed OP design, the result posted in your response should work correctly? Dec 21 '21 at 19:13
  • 1
    @CyrilMikhalchenko The title and the content of the question are different. This answer addresses the question as stated in OP content, doesn't it?
    – JGH
    Dec 21 '21 at 19:40
  • I think the execution of the SQL script will not result in the polygon being split into two separate parts... Dec 21 '21 at 19:43
  • @CyrilMikhalchenko from a quick test it seems to work, generating for each source row a geometry collection containing the two 1/2 polygons
    – JGH
    Dec 21 '21 at 19:55
  • To be sure, try splitting the result with ST_Dump()... Dec 21 '21 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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