-
Notifications
You must be signed in to change notification settings - Fork 2
/
split_polygon.sql
50 lines (48 loc) · 1.25 KB
/
split_polygon.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
drop table if exists split_parkings;
create table split_parkings as (
with
input as (
select
ref as poly_id,
geom as geom
from
juno_areas
where type = 'airport_parking_lot'
),
unlabeled_cloud as (
select
poly_id,
geom,
(ST_Dump(ST_GeneratePoints(geom, 1000))).geom pt,
(ST_Area(geom) / 1000) :: int k
from input
group by 1, 2
order by geom
),
labeled_cloud as (
select
poly_id,
pt,
ST_ClusterKMeans(pt, k)
over (
partition by poly_id
) cluster_id
from
unlabeled_cloud
),
labeled_centers as (
select
poly_id,
cluster_id,
ST_Centroid(ST_Collect(pt)) center
from labeled_cloud
group by poly_id, cluster_id),
voronoi_poly as (
select
poly_id,
(ST_Dump(ST_VoronoiPolygons(ST_Collect(center)))).geom
from labeled_centers
group by poly_id
)
select v.poly_id, ST_Intersection(v.geom, i.geom)
from voronoi_poly v join input i on i.poly_id = v.poly_id);