-
Notifications
You must be signed in to change notification settings - Fork 2
/
cut.sql
658 lines (604 loc) · 23.5 KB
/
cut.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
-- this block will set some flags, and supress unneeded output.
-- this is a visualization that aids in development, it renders
-- a number of different shapes (roads, land areas) in SVG format.
\set QUIET 1
\a
-- turn off footer
\pset footer off
-- turn off column names
\pset tuples_only
-- turn off any more verbose output from psql
\pset pager off
SET search_path = public, plan;
\set QUIET 0
\set QUIET 1
-- this function draws all the shapes in the support, parcel and road tables
-- it differentiates between some shapes in order to better identify them in
-- the SVG output
CREATE OR REPLACE FUNCTION parcels_draw() RETURNS text AS $$
DECLARE
retval text;
BEGIN
retval := (
WITH a AS (
-- unions together all the objects
-- we want to draw
SELECT
name, way, 'parcel' AS _type
FROM parcel
UNION ALL
SELECT
name, way, 'road' AS _type
FROM road
UNION ALL
SELECT
name, way, 'support' AS _type
FROM support
), d AS (
-- get bounding box of all shapes
-- as it will be later used for the svg viewport
SELECT
ST_Envelope(ST_Collect(way)) env
FROM a
), c AS (
SELECT
ST_Centroid(env) AS w
FROM d
), e AS (
SELECT
string_agg(svg_shape,'') AS svg_shapes
FROM (
-- draw objects differently depending on their type
SELECT
(
CASE
WHEN _type = 'parcel' THEN
'<path fill="wheat" stroke="red" stroke-width="4" d="' || ST_AsSVG(way) || '"/>'
WHEN _type = 'road' THEN
'<path fill-opacity="0" stroke="blue" stroke-width="8" d="' || ST_AsSVG(way) || '"/>'
WHEN _type = 'support' AND ST_GeometryType(way) = 'ST_LineString' THEN
'<path fill-opacity="0" stroke="violet" stroke-width="13" d="' || ST_AsSVG(way) || '"/>'
WHEN _type = 'support' AND ST_GeometryType(way) = 'ST_Polygon' THEN
'<path fill-opacity="0.2" fill="green" stroke="orange" stroke-width="13" d="' || ST_AsSVG(way) || '"/>'
WHEN _type = 'support' AND ST_GeometryType(way) = 'ST_Point' THEN
'<circle fill-opacity="1" fill="steelblue" stroke="royalblue" stroke-width="10" r="20" ' || ST_AsSVG(way) || '/>'
END
) AS svg_shape
FROM (
-- unpacking (for ST_Multi* structures)
SELECT
name,
_type,
(ST_Dump(way)).geom AS way
FROM a
) b
) q
), svg AS (
SELECT
(
'<html><svg width="100%" height="100%" preserveAspectRatio="" viewBox="' ||
concat_ws(' ', ST_XMin(d.env), ST_YMax(d.env) * -1, (ST_XMax(d.env) - ST_XMin(d.env)), (ST_YMax(d.env) - ST_YMin(d.env))) || '">' ||
e.svg_shapes ||
'</svg></html>'
) AS content
FROM e, d
)
SELECT content
FROM svg
);
RETURN retval;
END;
$$ LANGUAGE plpgsql;
--
-- split a geometry horizontally and return three geometries
-- - split line
-- - upper part
-- - lower part
--
-- the split line will be tmid distance down from the highest point of poly
--
-- note: h_split and v_split will not check if the split point is out of range.
-- that's something the caller should check.
-- if any of the split line or the pieces resulted from the split were NULL
-- the function will return a NULL.
CREATE OR REPLACE FUNCTION h_split(poly geometry, tmid float, bxmin float, bxmax float, bymin float, bymax float) RETURNS geometry[] AS $$
DECLARE
baseline geometry;
splitline geometry;
result geometry[];
BEGIN
baseline := ST_SetSRID(ST_MakeLine(ST_MakePoint(bxmin,bymin), ST_MakePoint(bxmax,bymin)), 900913);
splitline := ST_Translate(baseline,0,tmid);
-- split with horizontal line, we might
-- get back more than two pieces (depending on the shape of poly).
-- so we need to collect those above, and those below the cut.
WITH parts AS (
SELECT
a.piece
FROM (
SELECT ((ST_Dump((ST_Split(poly, splitline)))).geom) AS piece
) a
), U AS (
SELECT
ST_Multi(ST_Collect(piece)) AS shape
FROM parts
WHERE ST_YMin(piece) >= bymin + tmid
), D AS (
SELECT
ST_Multi(ST_Collect(piece)) AS shape
FROM parts
WHERE ST_YMin(piece) < bymin + tmid
)
SELECT ARRAY[U.shape, D.shape] INTO result
FROM U,D;
IF splitline IS NULL OR result[1] IS NULL OR result[2] IS NULL THEN
RETURN NULL;
END IF;
RETURN (splitline || result);
END;
$$ LANGUAGE plpgsql;
--
-- split a geometry vertically and return three geometries
-- - split line
-- - left part
-- - right part
--
-- the split line will be tmid distance right from the leftmost point of poly
CREATE OR REPLACE FUNCTION v_split(poly geometry, tmid float, bxmin float, bxmax float, bymin float, bymax float) RETURNS geometry[] AS $$
DECLARE
baseline geometry;
splitline geometry;
result geometry[];
BEGIN
baseline := ST_SetSRID(ST_MakeLine(ST_MakePoint(bxmin,bymin), ST_MakePoint(bxmin,bymax)), 900913);
splitline := ST_Translate(baseline,tmid,0);
-- split with horizontal line, we might
-- get back more than two pieces (depending on the shape of poly).
-- so we need to collect those above, and those below the cut.
WITH parts AS (
SELECT
a.piece
FROM (
SELECT ((ST_Dump((ST_Split(poly, splitline)))).geom) AS piece
) a
), L AS (
SELECT
ST_Multi(ST_Collect(piece)) AS shape
FROM parts
WHERE ST_XMin(piece) < bxmin + tmid
), R AS (
SELECT
ST_Multi(ST_Collect(piece)) AS shape
FROM parts
WHERE ST_XMin(piece) >= bxmin + tmid
)
SELECT
ARRAY[L.shape, R.shape] INTO result
FROM L,R;
IF splitline IS NULL OR result[1] IS NULL OR result[2] IS NULL THEN
RETURN NULL;
END IF;
RETURN (splitline || result);
END;
$$ LANGUAGE plpgsql;
-- input:
-- receives as parameters the initial polygon, the desired area,
-- an integer indicating which part we're searching for (1 for upper and 2 for lower)
-- and the bbox coordinates for the polygon.
--
-- output:
-- returns the cut line and the upper and lower parts.
--
-- the optimal cut line is found via binary search.
CREATE OR REPLACE FUNCTION hcut_search(poly geometry, updown integer, area float, bxmin float, bxmax float, bymin float, bymax float) RETURNS geometry[] AS $$
DECLARE
-- width of bounding box;
bwidth float;
-- length of bounding box;
bheight float;
-- trial cursor will be between [0,bwidth] or between [0,bheight]
tlow float;
tmid float;
thigh float;
-- trial cut area
tarea float;
-- tsplit
tsplit geometry[];
-- iteration number
titer integer;
BEGIN
bwidth := bxmax - bxmin;
bheight := bymax - bymin;
-- setting up the sweep line bsearch.
-- we're looking for a cut between [0, bheight]
-- (analogous situation for vertical sweep-line).
-- north-south sweep line (this is a horizontal line that travels in north-south direction)
titer := 0;
tlow := 0;
thigh := bheight;
WHILE tlow < thigh LOOP
-- RAISE NOTICE 'loop';
tmid := (tlow + thigh)/2;
tsplit := h_split(poly,tmid,bxmin,bxmax,bymin,bymax);
-- compute selected area (add 1 to skip over the split line)
tarea := ST_Area(tsplit[1 + updown]);
-- re-adjust the range we're searching for the split
-- depending on overshot/undershot relatve to the target area.
IF tarea > area AND updown = 1 THEN
-- overshot the upper target area
tlow := tmid;
ELSIF tarea < area AND updown = 1 THEN
-- undershot the upper target area
thigh := tmid;
ELSIF tarea > area AND updown = 2 THEN
-- overshot the lower target area
thigh := tmid;
ELSIF tarea < area AND updown = 2 THEN
-- undershot the lower target area
tlow := tmid;
END IF;
RAISE NOTICE 'area selected by split: %', tarea;
IF ABS(tarea - area) < 0.001 THEN
RAISE NOTICE 'found split with reasonably close area';
RAISE NOTICE 'delta for split: %', ABS(tarea-area);
EXIT;
END IF;
IF titer > 70 THEN
RAISE NOTICE 'exceeded search iterations';
RETURN NULL;
END IF;
titer := titer + 1;
END LOOP;
RETURN tsplit;
END;
$$ LANGUAGE plpgsql;
-- same as hcut_search except the cut line is vertical here
CREATE OR REPLACE FUNCTION vcut_search(poly geometry, leftright integer, area float, bxmin float, bxmax float, bymin float, bymax float) RETURNS geometry[] AS $$
DECLARE
-- width of bounding box;
bwidth float;
-- length of bounding box;
bheight float;
-- trial cursor will be between [0,bwidth] or between [0,bheight]
tlow float;
tmid float;
thigh float;
-- trial cut area
tarea float;
-- tsplit
tsplit geometry[];
-- iteration number
titer integer;
BEGIN
bwidth := bxmax - bxmin;
bheight := bymax - bymin;
-- setting up the sweep line bsearch.
-- we're looking for a cut between [0, bwidth]
-- (analogous situation for vertical sweep-line).
-- north-south sweep line (this is a horizontal line that travels in north-south direction)
titer := 0;
tlow := 0;
thigh := bwidth;
WHILE tlow < thigh LOOP
-- RAISE NOTICE 'loop';
tmid := (tlow + thigh)/2;
-- compute selected area (add 1 to skip over the split line)
tsplit := v_split(poly,tmid,bxmin,bxmax,bymin,bymax);
tarea := ST_Area(tsplit[1 + leftright]);
-- re-adjust the range we're searching for the split
-- depending on overshot/undershot relatve to the target area.
IF tarea > area AND leftright = 1 THEN
-- overshot the left target area
thigh := tmid;
ELSIF tarea < area AND leftright = 1 THEN
-- undershot the left target area
tlow := tmid;
ELSIF tarea > area AND leftright = 2 THEN
-- overshot the right target area
tlow := tmid;
ELSIF tarea < area AND leftright = 2 THEN
-- undershot the right target area
thigh := tmid;
END IF;
RAISE NOTICE 'area selected by split: %', tarea;
IF ABS(tarea - area) < 0.001 THEN
RAISE NOTICE 'found split with reasonably close area';
RAISE NOTICE 'delta for split: %', ABS(tarea-area);
EXIT;
END IF;
IF titer > 70 THEN
RAISE NOTICE 'exceeded search iterations';
RETURN NULL;
END IF;
titer := titer + 1;
END LOOP;
RETURN tsplit;
END;
$$ LANGUAGE plpgsql;
--
-- finds which corner to cut
-- (this is the 2nd implementation of this functionality,
-- as the first one was too complicated)
--
-- it only uses the boundary and the roads.
-- finds which of the four centroid quadrants the closest
-- road point belongs to.
--
CREATE OR REPLACE FUNCTION find_cut_corner2(boundary geometry[], poly geometry) RETURNS text AS $$
DECLARE
-- closest point on road
r_closest geometry;
-- closest boundary point
b_closest geometry;
-- azimuth of point on road
r_azimuth float;
-- corner to cut
corner text;
BEGIN
SELECT
ST_ClosestPoint(r.way, b.way), b.way INTO r_closest, b_closest
FROM road r, unnest(boundary) b(way)
ORDER BY ST_ClosestPoint(r.way, b.way) <-> b.way
LIMIT 1;
INSERT INTO support(way) SELECT b_closest;
INSERT INTO support(way) SELECT r_closest;
-- first argument to ST_Azimuth is the center of the circle
-- 2nd argument is the point on the boundary of the circle that
-- we want to find the angle for.
r_azimuth := degrees(ST_Azimuth(ST_Centroid(poly), r_closest));
IF r_azimuth <= 90.0 THEN
-- r_closest is in NE
corner := 'NE';
ELSIF r_azimuth <= 180.0 THEN
-- r_closest is in SE
corner := 'SE';
ELSIF r_azimuth <= 270.0 THEN
-- r_closest is in SW
corner := 'SW';
ELSE
-- r_closest is in NW
corner := 'NW';
END IF;
RETURN corner;
END;
$$ LANGUAGE plpgsql;
--
-- This function will implement the corner-cut algorithm
-- the return value will be true if the parcel was found.
--
-- Return value:
-- - NULL if there was an error
-- - false if a parcel was not found
-- - true if a parcel was found
--
CREATE OR REPLACE FUNCTION pseudo_parcel(p_uid integer, target_area float) RETURNS boolean AS $$
DECLARE
-- original polygon
poly geometry;
bbox geometry;
-- extreme cardinal points on the polygon boundary
boundary geometry[];
-- width of bounding box;
bwidth float;
-- length of bounding box;
bheight float;
-- xmin,xmax,ymin,ymax for bbox
bxmin float;
bxmax float;
bymin float;
bymax float;
cut_corner text;
-- the result of the inset split
inset_split geometry[];
inset_pos float;
-- area of the input polygon
p_area float;
-- cut line
cut_result geometry[];
-- constants indicating which piece to select after the split
UPPER_PART CONSTANT integer := 1;
LOWER_PART CONSTANT integer := 2;
LEFT_PART CONSTANT integer := 1;
RIGHT_PART CONSTANT integer := 2;
BEGIN
poly := (SELECT way FROM parcel WHERE gid = p_uid);
p_area := ST_Area(poly);
IF poly IS NULL THEN
-- the input polygon was not found in the polygon table
RAISE NOTICE 'input polygon not found';
RETURN NULL;
END IF;
IF p_area < target_area THEN
-- the input polygon is too small and we won't be able to
-- to find a parcel with the required area.
RAISE NOTICE 'the polygon area is too small';
RETURN NULL;
END IF;
RAISE NOTICE 'original area: %', p_area;
bbox := (
-- get parcel boundary
SELECT ST_ExteriorRing(ST_Envelope(way)) AS way
FROM parcel
WHERE gid = p_uid
);
INSERT INTO support(way) VALUES (bbox);
-- get boundary extreme points for polygon
boundary := (
WITH points AS (
SELECT
(ST_DumpPoints(way)).geom AS p
FROM (
SELECT way
FROM parcel
WHERE gid = p_uid
) a
), west AS (
SELECT p FROM points ORDER BY ST_X(p) LIMIT 1
), east AS (
SELECT p FROM points ORDER BY ST_X(p) DESC LIMIT 1
), north AS (
SELECT p FROM points ORDER BY ST_Y(p) LIMIT 1
), south AS (
SELECT p FROM points ORDER BY ST_Y(p) DESC LIMIT 1
)
SELECT ARRAY[west.p, east.p, north.p, south.p]
FROM west,east,north,south
);
RAISE NOTICE 'boundary types %', (SELECT array_agg(ST_GeometryType(p)) FROM unnest(boundary) a(p));
-- INSERT INTO support(way) SELECT * FROM unnest(boundary);
SELECT ST_XMax(p),ST_XMin(p),ST_YMax(p),ST_YMin(p)
INTO bxmax,bxmin,bymax,bymin
FROM (
SELECT ST_Envelope(ST_Collect(w)) p
FROM unnest(boundary) a(w)
) c;
bwidth := bxmax - bxmin;
bheight := bymax - bymin;
cut_corner := find_cut_corner2(boundary, poly);
RAISE NOTICE 'cut corner: %', cut_corner;
--
-- we have four cases(one for each corner). all of the cases are very similar.
-- we make a horizontal split and if we don't have enough area we make a
-- better horizontal split with the same goal. otherwise we make a vertical split
-- to get the required area.
--
-- here's an example for the NW corner(with the two cases mentioned before):
--
-- 1) 2)
-- +-----------------+ +-----------------+
-- | target | | | target |
-- | | | | |
-- +--------+--------+ | |
-- | | +-----------------|
-- | | | |
-- | | | |
-- | | | |
-- +--------+--------+ +-----------------+
--
IF cut_corner = 'NW' THEN
IF bheight - sqrt(target_area) > 0 THEN
-- (bheight - sqrt(target_area)) is where the inset should be placed for NW corner
inset_pos := bheight - sqrt(target_area);
inset_split := h_split(poly,inset_pos,bxmin,bxmax,bymin,bymax);
IF ST_Area(inset_split[2]) < target_area THEN
-- the inset cut was insufficient so we search for a different horizontal cut
RAISE NOTICE 'hcut_search';
cut_result := hcut_search(poly,UPPER_PART,target_area,bxmin,bxmax,bymin,bymax);
-- update original parcel
UPDATE parcel SET way = cut_result[3] WHERE gid = p_uid;
-- insert the new parcel
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[2], true, p_uid);
ELSIF bheight - sqrt(target_area) > 0 THEN
-- the inset produced an upper area which is more than what we need. we need to make another cut
-- so we're now looking for a vertical cut but applied to the upper part of the inset split (not on original poly)
RAISE NOTICE 'inset split + vcut_search';
cut_result := vcut_search(inset_split[2],LEFT_PART,target_area,ST_XMin(inset_split[2]),ST_XMax(inset_split[2]),ST_YMin(inset_split[2]),ST_YMax(inset_split[2]));
INSERT INTO support(way) SELECT inset_split[1];
INSERT INTO support(way) SELECT cut_result[1];
-- update the original parcel
UPDATE parcel SET way = ST_Union(inset_split[3], cut_result[3]) WHERE gid = p_uid;
-- insert the new parcel
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[2], true, p_uid);
END IF;
ELSE
-- in this case, we have bheight - sqrt(target_area) < 0 so we do a vcut straight
-- on the original poly
cut_result := vcut_search(poly,LEFT_PART,target_area,bxmin,bxmax,bymin,bymax);
-- update the original parcel
UPDATE parcel SET way = cut_result[3] WHERE gid = p_uid;
-- insert the new parcel
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[2], true, p_uid);
END IF;
ELSIF cut_corner = 'NE' THEN
IF bheight - sqrt(target_area) > 0 THEN
inset_pos := bheight - sqrt(target_area);
inset_split := h_split(poly,inset_pos,bxmin,bxmax,bymin,bymax);
IF ST_Area(inset_split[2]) < target_area THEN
RAISE NOTICE 'hcut_search';
cut_result := hcut_search(poly,UPPER_PART,target_area,bxmin,bxmax,bymin,bymax);
UPDATE parcel SET way = cut_result[3] WHERE gid = p_uid;
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[2], true, p_uid);
ELSE
RAISE NOTICE 'inset split + vcut_search';
cut_result := vcut_search(inset_split[2],RIGHT_PART,target_area,ST_XMin(inset_split[2]),ST_XMax(inset_split[2]),ST_YMin(inset_split[2]),ST_YMax(inset_split[2]));
INSERT INTO support(way) SELECT inset_split[1];
INSERT INTO support(way) SELECT cut_result[1];
UPDATE parcel SET way = ST_Union(inset_split[3], cut_result[2]) WHERE gid = p_uid;
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[3], true, p_uid);
END IF;
ELSE
cut_result := vcut_search(poly,RIGHT_PART,target_area,bxmin,bxmax,bymin,bymax);
UPDATE parcel SET way = cut_result[2] WHERE gid = p_uid;
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[3], true, p_uid);
END IF;
ELSIF cut_corner = 'SE' THEN
IF sqrt(target_area) < bheight THEN
inset_pos := sqrt(target_area);
inset_split := h_split(poly,inset_pos,bxmin,bxmax,bymin,bymax);
IF ST_Area(inset_split[3]) < target_area THEN
RAISE NOTICE 'hcut_search';
cut_result := hcut_search(poly,LOWER_PART,target_area,bxmin,bxmax,bymin,bymax);
UPDATE parcel SET way = cut_result[2] WHERE gid = p_uid;
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[3], true, p_uid);
ELSE
RAISE NOTICE 'inset split + vcut_search';
cut_result := vcut_search(inset_split[3],RIGHT_PART,target_area,ST_XMin(inset_split[3]),ST_XMax(inset_split[3]),ST_YMin(inset_split[3]),ST_YMax(inset_split[3]));
INSERT INTO support(way) SELECT inset_split[1];
INSERT INTO support(way) SELECT cut_result[1];
UPDATE parcel SET way = ST_Union(inset_split[2], cut_result[2]) WHERE gid = p_uid;
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[3], true, p_uid);
END IF;
ELSE
cut_result := vcut_search(poly,RIGHT_PART,target_area,bxmin,bxmax,bymin,bymax);
UPDATE parcel SET way = cut_result[2] WHERE gid = p_uid;
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[3], true, p_uid);
END IF;
ELSIF cut_corner = 'SW' THEN
IF sqrt(target_area) < bheight THEN
inset_pos := sqrt(target_area);
inset_split := h_split(poly,inset_pos,bxmin,bxmax,bymin,bymax);
IF ST_Area(inset_split[3]) < target_area THEN
RAISE NOTICE 'hcut_search';
cut_result := hcut_search(poly,LOWER_PART,target_area,bxmin,bxmax,bymin,bymax);
UPDATE parcel SET way = cut_result[2] WHERE gid = p_uid;
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[3], true, p_uid);
ELSE
RAISE NOTICE 'inset split + vcut_search';
cut_result := vcut_search(inset_split[3],LEFT_PART,target_area,ST_XMin(inset_split[3]),ST_XMax(inset_split[3]),ST_YMin(inset_split[3]),ST_YMax(inset_split[3]));
INSERT INTO support(way) SELECT inset_split[1];
INSERT INTO support(way) SELECT cut_result[1];
UPDATE parcel SET way = ST_Union(inset_split[2], cut_result[3]) WHERE gid = p_uid;
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[2], true, p_uid);
END IF;
ELSE
cut_result := vcut_search(poly,LEFT_PART,target_area,bxmin,bxmax,bymin,bymax);
UPDATE parcel SET way = cut_result[3] WHERE gid = p_uid;
INSERT INTO parcel (way, pseudo, parent_id) VALUES(cut_result[2], true, p_uid);
END IF;
END IF;
RETURN true;
END;
$$ LANGUAGE plpgsql;
-- suppress output
\o /dev/null
-- cut parcels until there's no more land to cut
DO $$
DECLARE
enough_land boolean;
target_parcel_area float;
poly_to_partition integer;
BEGIN
poly_to_partition := 1;
target_parcel_area := 8000.0;
enough_land := true;
WHILE enough_land IS NOT NULL
LOOP
RAISE NOTICE '----';
enough_land := pseudo_parcel(poly_to_partition,target_parcel_area);
END LOOP;
END$$;
TRUNCATE support RESTART IDENTITY;
-- re-enable stdout output
\o
\set QUIET 0
SELECT parcels_draw();