-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path12_generalization_25k.sql
More file actions
605 lines (555 loc) · 11.8 KB
/
12_generalization_25k.sql
File metadata and controls
605 lines (555 loc) · 11.8 KB
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
-- Forest
------------------------
TRUNCATE map_25k.forest;
INSERT INTO
map_25k.forest (geom) WITH expand AS (
SELECT
ST_Buffer(geom, 10) AS geom
FROM
osm.forest
),
clustering AS (
SELECT
ST_ClusterDBSCAN(geom, 0, 1) OVER() AS cluster_id,
geom
FROM
expand
),
dissolve AS (
SELECT
ST_Union(geom) AS geom
FROM
clustering
GROUP BY
cluster_id
),
shrink AS (
SELECT
ST_Buffer(
ST_Buffer(geom, -20),
10
) AS geom
FROM
dissolve
),
single_feature AS (
SELECT
(ST_Dump(geom)).geom AS geom
FROM
shrink
),
min_area AS (
SELECT
*
FROM
single_feature
WHERE
ST_Area(geom) > 306
),
sieve AS (
SELECT
Sieve(geom, 156) AS geom
FROM
min_area
),
simplify AS (
SELECT
ST_Simplify(geom, 5) AS geom
FROM
sieve
)
SELECT
ST_Multi(geom)
FROM
simplify;
-- Water
------------------------------
TRUNCATE map_25k.water;
INSERT INTO
map_25k.water (geom) WITH expand AS (
SELECT
ST_Buffer(geom, 10) AS geom
FROM
osm.water
),
clustering AS (
SELECT
ST_ClusterDBSCAN(geom, 0, 1) OVER() AS cluster_id,
geom
FROM
expand
),
dissolve AS (
SELECT
ST_Union(geom) AS geom
FROM
clustering
GROUP BY
cluster_id
),
shrink AS (
SELECT
ST_Buffer(
ST_Buffer(geom, -20),
10
) AS geom
FROM
dissolve
),
single_feature AS (
SELECT
(ST_Dump(geom)).geom AS geom
FROM
shrink
),
min_area AS (
SELECT
*
FROM
single_feature
WHERE
ST_Area(geom) > 306
),
sieve AS (
SELECT
Sieve(geom, 156) AS geom
FROM
min_area
),
simplify AS (
SELECT
ST_Simplify(geom, 5) AS geom
FROM
sieve
)
SELECT
ST_Multi(geom)
FROM
simplify;
---- Grass
------------------
TRUNCATE map_25k.grass;
INSERT INTO
map_25k.grass (geom) WITH expand AS (
SELECT
ST_Buffer(geom, 10) AS geom
FROM
osm.grass
),
clustering AS (
SELECT
ST_ClusterDBSCAN(geom, 0, 1) OVER() AS cluster_id,
geom
FROM
expand
),
dissolve AS (
SELECT
ST_Union(geom) AS geom
FROM
clustering
GROUP BY
cluster_id
),
shrink AS (
SELECT
ST_Buffer(
ST_Buffer(geom, -20),
10
) AS geom
FROM
dissolve
),
single_feature AS (
SELECT
(ST_Dump(geom)).geom AS geom
FROM
shrink
),
min_area AS (
SELECT
*
FROM
single_feature
WHERE
ST_Area(geom) > 306
),
sieve AS (
SELECT
Sieve(geom, 156) AS geom
FROM
min_area
),
simplify AS (
SELECT
ST_Simplify(geom, 5) AS geom
FROM
sieve
)
SELECT
ST_Multi(geom)
FROM
simplify;
---- Buildings
-------------------
TRUNCATE map_25k.building;
INSERT INTO
map_25k.building (geom) WITH clustering AS (
-- find connected areas
SELECT
ST_ClusterDBSCAN(geom, 0, 1) OVER() AS cluster_id,
geom
FROM
osm.building
),
dissolve AS (
SELECT
ROW_NUMBER() OVER() AS fid,
ST_Union(geom) AS geom
FROM
clustering
GROUP BY
cluster_id
),
buffer_simplify AS (
SELECT
fid,
ST_Buffer(
ST_Buffer(
ST_Buffer(
ST_Buffer(geom, -3, 'endcap=flat join=mitre'),
1,
'endcap=flat join=mitre'
),
5,
'endcap=flat join=mitre'
),
-3,
'endcap=flat join=mitre'
) AS geom
FROM
dissolve
),
simplify AS (
-- Redue number of points
SELECT
fid,
ST_SimplifyVW(geom, 3) AS geom
FROM
buffer_simplify
),
make_valid AS (
SELECT
fid,
geom
FROM
simplify
WHERE
ST_IsEmpty(geom) = FALSE -- Remove collapsed geometries
),
filter1 AS (
-- Add big narrow buildings again
SELECT
fid,
ST_SimplifyVW(geom, 3) AS geom
FROM
dissolve b
WHERE
NOT EXISTS (
SELECT
1
FROM
make_valid bs
WHERE
b.fid = bs.fid
)
AND ST_Area(geom) >= 100
),
filter2 AS (
-- Show too small buildings by an abstract rotated square
SELECT
fid,
ST_Rotate(
ST_Buffer(ST_Centroid(geom), 4, 'endcap=square'),
radians(0 - feature_orientation(geom)),
ST_Centroid(geom)
) AS geom
FROM
dissolve b
WHERE
NOT EXISTS (
SELECT
1
FROM
make_valid bs
WHERE
b.fid = bs.fid
)
AND ST_Area(geom) < 100
AND ST_Area(geom) > 50
)
SELECT -- Merge all kinds togehter
ST_Multi(geom)
FROM
make_valid
UNION
ALL
SELECT
ST_Multi(geom)
FROM
filter1
UNION
ALL
SELECT
ST_Multi(geom)
FROM
filter2;
----
-- Peaks
TRUNCATE map_25k.elevation_point;
INSERT INTO map_25k.elevation_point (name, "type", elevation, geom)
SELECT name, "type", round(ele), geom
FROM osm.elevation_point ep
WHERE "type" IN ('peak', 'vulcano') AND ele IS NOT NULL;
UPDATE map_25k.elevation_point
SET discrete_isolation = discrete_isolation('map_25k.elevation_point', 'geom', 'elevation', geom, elevation);
----
-- Buildup area
TRUNCATE map_25k.build_up_area;
INSERT INTO map_25k.build_up_area (geom)
WITH
buffering AS (
SELECT fid, ST_Buffer(geom, 40, 'endcap=flat join=mitre') AS geom
FROM osm.building
),
clustering AS (
SELECT geom, ST_ClusterDBSCAN(geom, 0, 1) OVER() AS cid
FROM buffering
),
merging AS (
SELECT ST_Union(geom) AS geom
FROM clustering
GROUP BY cid
),
simplifying AS (
SELECT ST_SimplifyVW(ST_Buffer(ST_Buffer(ST_SimplifyVW(ST_Buffer(geom, 20, 'endcap=flat join=mitre'), 400), -1, 'endcap=flat join=mitre'), -39, 'endcap=flat join=mitre'), 400) AS geom
FROM merging
),
single_feature AS (
SELECT (ST_Dump(geom)).geom AS geom
FROM simplifying
)
SELECT geom
FROM single_feature
WHERE ST_Area(geom) > 8000;
---- POI
TRUNCATE map_25k.poi;
-- Cluster nearby guidepost by mering them into the first one
INSERT INTO map_25k.poi (name, geom, "type")
WITH
clustering AS (
SELECT name, geom, ST_ClusterDBScan(geom, 10, 1) OVER() AS cid
FROM osm.poi p
WHERE information = 'guidepost' AND tags ->> 'tourism' = 'information'
),
chose_first AS ( -- ALWAYS chose first element in cluster
SELECT cid, (array_agg(name))[1] AS name, ST_NumGeometries(ST_Collect(geom)) AS gp_count, ST_GeometryN(ST_Collect(geom), 1) AS geom
FROM clustering
GROUP BY cid
)
SELECT name, geom, 'guidepost'
FROM chose_first;
INSERT INTO map_25k.poi (name, geom, "type")
SELECT name, geom, 'picnic_site'
FROM osm.poi p
WHERE tags ->> 'tourism' = 'picnic_site';
INSERT INTO map_25k.poi (name, geom, "type")
SELECT name, geom, 'shelter'
FROM osm.poi p
WHERE amenity = 'shelter';
INSERT INTO map_25k.poi (name, geom, "type")
SELECT name, geom, 'bench'
FROM osm.poi p
WHERE amenity = 'bench';
INSERT INTO map_25k.poi (name, geom, "type")
SELECT name, geom, 'bus_stop'
FROM osm.poi p
WHERE highway = 'bus_stop';
INSERT INTO map_25k.poi (name, geom, "type")
SELECT name, geom, railway
FROM osm.poi p
WHERE railway IN ('station', 'halt');
--- Admin bounary
INSERT INTO map_25k.admin_boundary_line (name, admin_level, geom)
SELECT name, admin_level, ST_SimplifyPreserveTopology(geom, 10) AS geom
FROM osm.admin_boundary_line abl;
--- Places
TRUNCATE map_25k.place;
INSERT INTO map_25k.place (name, place, population, geom)
SELECT name, place, population, geom
FROM osm.place p;
UPDATE map_25k.place
SET discrete_isolation = discrete_isolation('map_25k.place', 'geom', 'population', geom, population);
--- Traffic
TRUNCATE tmp.interesection_points;
INSERT INTO tmp.interesection_points (geom)
SELECT DISTINCT ST_Multi(ST_Intersection(t1.geom, t2.geom)) AS geom
FROM osm.traffic t1 JOIN osm.traffic t2 ON
ST_Crosses(t1.geom, t2.geom) OR ST_Touches(t1.geom, t2.geom)
AND t1.fid != t2.fid AND t1.layer = t2.layer
AND NOT t1.bridge AND NOT t2.tunnel AND NOT t2.bridge AND NOT t1.tunnel;
INSERT INTO tmp.interesection_points (geom)
WITH points AS (
SELECT fid, ST_StartPoint(geom) AS geom
FROM osm.traffic
WHERE ST_IsSimple(geom) = FALSE
UNION
SELECT fid, ST_EndPoint(geom) AS geom
FROM osm.traffic
WHERE ST_IsSimple(geom) = FALSE
),
distinct_points AS (
SELECT DISTINCT fid, geom
FROM points
)
SELECT ST_Multi(geom)
FROM distinct_points;
TRUNCATE map_25k.traffic_edges;
INSERT INTO map_25k.traffic_edges ("name", "ref", highway, railway, oneway, bridge, tunnel, layer, geom)
-- Split
SELECT "name", "ref", highway, railway, oneway, bridge, tunnel, layer,
(ST_Dump(ST_CollectionExtract(ST_Split(geom,
(SELECT ST_Union(p.geom) FROM tmp.interesection_points AS p WHERE ST_Intersects(p.geom, t.geom))
), 2))).geom AS geom
FROM osm.traffic AS t
UNION ALL
-- Not to split
SELECT "name", "ref", highway, railway, oneway, bridge, tunnel, layer, geom
FROM osm.traffic AS t
WHERE NOT EXISTS (SELECT 1 FROM tmp.interesection_points AS p WHERE ST_Intersects(p.geom, t.geom));
TRUNCATE map_25k.traffic_nodes;
-- Nodes from start and end points
INSERT INTO map_25k.traffic_nodes (geom)
WITH
points AS (
SELECT ST_StartPoint(geom) AS geom
FROM map_25k.traffic_edges
UNION
SELECT ST_EndPoint(geom) AS geom
FROM map_25k.traffic_edges
),
distinct_points AS (
SELECT DISTINCT geom
FROM points
)
SELECT geom
FROM points;
-- Set start node
UPDATE map_25k.traffic_edges l
SET start_node = p.fid
FROM map_25k.traffic_nodes p
WHERE ST_Intersects(ST_StartPoint(l.geom), p.geom);
-- Set end node
UPDATE map_25k.traffic_edges l
SET end_node = p.fid
FROM map_25k.traffic_nodes p
WHERE ST_Intersects(ST_EndPoint(l.geom), p.geom);
-- Count edges at node
WITH collect AS (
SELECT start_node AS node
FROM map_25k.traffic_edges
UNION ALL
SELECT end_node AS node
FROM map_25k.traffic_edges
), agg AS (
SELECT node, count(*) AS cnt
FROM collect
GROUP BY node
)
UPDATE map_25k.traffic_nodes p
SET edges_count = cnt
FROM agg
WHERE p.fid = node;
-- find death ends and calculate length
SELECT find_death_ends('traffic_edges', 'traffic_nodes', 'map_25k', 5);
-- Selection citeria of death ends: death_end > 0 and death_end_length < 100
---- Waterways
TRUNCATE tmp.waterway_interesection_points;
INSERT INTO tmp.waterway_interesection_points (geom)
SELECT DISTINCT ST_Multi(ST_Intersection(t1.geom, t2.geom)) AS geom
FROM osm.waterway t1 JOIN osm.waterway t2 ON
ST_Crosses(t1.geom, t2.geom) OR ST_Touches(t1.geom, t2.geom)
AND t1.fid != t2.fid AND t1.layer = t2.layer;
INSERT INTO tmp.waterway_interesection_points (geom)
WITH points AS (
SELECT fid, ST_StartPoint(geom) AS geom
FROM osm.waterway
WHERE ST_IsSimple(geom) = FALSE
UNION
SELECT fid, ST_EndPoint(geom) AS geom
FROM osm.waterway
WHERE ST_IsSimple(geom) = FALSE
),
distinct_points AS (
SELECT DISTINCT fid, geom
FROM points
)
SELECT ST_Multi(geom)
FROM distinct_points;
TRUNCATE map_25k.waterway_edges;
INSERT INTO map_25k.waterway_edges ("name", "type", tunnel, layer, geom)
-- Split
SELECT "name", "waterway", tunnel, layer,
(ST_Dump(ST_CollectionExtract(ST_Split(geom,
(SELECT ST_Union(p.geom) FROM tmp.waterway_interesection_points AS p WHERE ST_Intersects(p.geom, t.geom))
), 2))).geom AS geom
FROM osm.waterway AS t
UNION ALL
-- Not to split
SELECT "name", "waterway", tunnel, layer, geom
FROM osm.waterway AS t
WHERE NOT EXISTS (SELECT 1 FROM tmp.waterway_interesection_points AS p WHERE ST_Intersects(p.geom, t.geom));
TRUNCATE map_25k.waterway_nodes;
-- Nodes from start and end points
INSERT INTO map_25k.waterway_nodes (geom)
WITH
points AS (
SELECT ST_StartPoint(geom) AS geom
FROM map_25k.waterway_edges
UNION
SELECT ST_EndPoint(geom) AS geom
FROM map_25k.waterway_edges
),
distinct_points AS (
SELECT DISTINCT geom
FROM points
)
SELECT geom
FROM points;
-- Set start node
UPDATE map_25k.waterway_edges l
SET start_node = p.fid
FROM map_25k.waterway_nodes p
WHERE ST_Intersects(ST_StartPoint(l.geom), p.geom);
-- Set end node
UPDATE map_25k.waterway_edges l
SET end_node = p.fid
FROM map_25k.waterway_nodes p
WHERE ST_Intersects(ST_EndPoint(l.geom), p.geom);
-- Count edges at node
WITH collect AS (
SELECT start_node AS node
FROM map_25k.waterway_edges
UNION ALL
SELECT end_node AS node
FROM map_25k.waterway_edges
), agg AS (
SELECT node, count(*) AS cnt
FROM collect
GROUP BY node
)
UPDATE map_25k.waterway_nodes p
SET edges_count = cnt
FROM agg
WHERE p.fid = node;
SELECT find_death_ends('waterway_edges', 'waterway_nodes', 'map_25k', 5);