- Solution by PostGIS developer Darafei Praliaskouski.
- http://blog.cleverelephant.ca/2018/06/polygon-splitting.html
- https://thadk.carto.com/builder/392fb7e5-7a95-4b42-92cc-32e4c29594ae/embed (for Ghana's Mion district)
CREATE TABLE peru AS
SELECT * FROM countries
WHERE name LIKE 'peru'
CREATE TABLE peru_pts AS
SELECT
ST_SetSRID((ST_Dump(ST_GeneratePoints(the_geom, 500))).geom,4326) AS the_geom,
ST_Transform(ST_Buffer(the_geom,0.001), 3857) as the_geom_webmercator
FROM peru
WHERE name LIKE 'peru'
select cdb_cartodbfytable('peru_pts')
Read more about this at: https://github.com/CartoDB/cartodb/wiki/creating-tables-though-the-SQL-API
CREATE TABLE peru_pts_clustered AS
SELECT
the_geom,
the_geom_webmercator,
ST_ClusterKmeans(the_geom, 10) over () AS cluster
FROM peru_pts;
CREATE TABLE peru_centers AS
SELECT cluster,
ST_Centroid(ST_collect(the_geom)) AS the_geom FROM peru_pts_clustered
GROUP BY cluster
select cdb_cartodbfytable('peru_centers')
CREATE TABLE peru_voronoi AS
SELECT (ST_Dump(ST_VoronoiPolygons(ST_collect(the_geom)))).geom AS the_geom
FROM peru_centers;
select cdb_cartodbfytable('peru_voronoi')
CREATE TABLE peru_divided AS
SELECT ST_Intersection(a.the_geom, b.the_geom) AS the_geom
FROM peru a
CROSS JOIN peru_voronoi b;
select cdb_cartodbfytable('peru_divided')