11

Is there a convenient way to split a polygon into n parts, more-or-less equal in size in PostGIS?

Adam Matan
  • 6,838
  • 7
  • 37
  • 50

2 Answers2

9

This is an old problem with no simple solution. The only approach that I came across is to make a function that you give a heading and the number of parts and the computer make trials until it gets equal areas. There is a LISP function with that in AutoCAD. In postgis it works the same, here is an excerpt of PostGIS in Action from Manning, this code splits a polygon in two equal parts:

WITH RECURSIVE
ref(the_geom, env) AS (
SELECT the_geom,
ST_Envelope(the_geom) As env,
ST_Area(The_geom)/2 As targ_area,
1000 As nit
FROM us.states
WHERE state = 'Idaho'
),

T(n,overlap) AS (
VALUES (CAST(0 As Float),CAST(0 As Float))
UNION ALL
SELECT n + nit, ST_Area(ST_Intersection(the_geom, ST_Translate(env, n+nit, 0)))
FROM T CROSS JOIN ref
WHERE ST_Area(ST_Intersection(the_geom, ST_Translate(env, n+nit, 0)))> ref.targ_area
) ,  

bi(n) AS
(SELECT n
FROM T
ORDER BY n DESC LIMIT 1)  

SELECT bi.n,
ST_Difference(the_geom, ST_Translate(ref.env, n,0)) As geom_part1,
ST_Intersection(the_geom, ST_Translate(ref.env, n,0)) As geom_part2
FROM bi CROSS JOIN ref;
Adam Matan
  • 6,838
  • 7
  • 37
  • 50
Pablo
  • 9,827
  • 6
  • 43
  • 73
2

One approach might be to split the polygon completely into triangles, each with a given area. Then it would be a matter of trying to group those (adjacent) triangles back into polygons of (more-or-less) size area/n. This would be a sort of customized version of "subset sum" or "knapsack" problem (and I wouldn't know how to start with that with PostGIS).