forked from CartoDB/CartoDB-basemaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal_functions.sql
587 lines (551 loc) · 22.6 KB
/
global_functions.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
-- The functions used to take a schema name, but no longer do. This is
-- why many drop two functions
SET client_min_messages TO WARNING;
BEGIN;
DROP FUNCTION IF EXISTS false_background_zoomed(text,box3d);
CREATE OR REPLACE FUNCTION false_background_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(the_geom_webmercator geometry) AS
$$
SELECT bbox::geometry
WHERE zoom(scaleDenominator::numeric) >= 9;
$$
LANGUAGE SQL;
DROP FUNCTION IF EXISTS continents_zoomed(text,box3d);
CREATE OR REPLACE FUNCTION continents_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id integer, name text, the_geom_webmercator geometry) AS
$$
SELECT
cartodb_id,
name::text,
the_geom_webmercator
FROM continents_900913
WHERE the_geom_webmercator && bbox
-- hack hack. scaledenominators seem to be inaccurate at zooms 0-2
AND scaleDenominator::numeric > 139000000;
$$
LANGUAGE SQL;
DROP FUNCTION IF EXISTS land_positive_zoomed(text,box3d);
CREATE OR REPLACE FUNCTION land_positive_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id integer, the_geom_webmercator geometry) AS
$$
DECLARE
zoom NUMERIC;
BEGIN
zoom := zoom(scaleDenominator::numeric);
IF zoom <= 1 THEN
RETURN QUERY EXECUTE format(
'SELECT cartodb_id, the_geom_webmercator
FROM ne_50m_land
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSIF zoom <= 3 THEN
RETURN QUERY EXECUTE format(
'SELECT cartodb_id, the_geom_webmercator
FROM ne_50m_admin_0_countries_lakes
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSIF zoom <= 7 THEN
RETURN QUERY EXECUTE format(
'SELECT cartodb_id, the_geom_webmercator
FROM ne_10m_admin_0_countries_lakes
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSIF zoom <= 8 THEN
RETURN QUERY EXECUTE format(
'SELECT cartodb_id, the_geom_webmercator
FROM simplified_land_polygons
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSE
RETURN;
END IF;
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS land_negative_zoomed(text,box3d);
CREATE OR REPLACE FUNCTION land_negative_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id bigint, the_geom_webmercator geometry) AS
$$
SELECT
cartodb_id::bigint,
the_geom_webmercator
FROM water_polygons
WHERE the_geom_webmercator && bbox
AND zoom(scaleDenominator::numeric) >= 9;
$$
LANGUAGE SQL;
DROP FUNCTION IF EXISTS ne_marine_zoomed(text,box3d);
CREATE OR REPLACE FUNCTION ne_marine_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id bigint, the_geom_webmercator geometry, name text, namealt text, featurecla text, scalerank integer) AS
/* Some tables have cartodb_id bigint, some integer, so cast them all to bigint */
$$
BEGIN
IF zoom(scaleDenominator::numeric) <= 3 AND zoom(scaleDenominator::numeric) >= 2 THEN
RETURN QUERY EXECUTE format(
'SELECT cartodb_id::bigint, the_geom_webmercator, name::text, namealt::text, featurecla::text, scalerank::integer
FROM ne_110m_geography_marine_polys
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 4 AND zoom(scaleDenominator::numeric) <= 5 THEN
RETURN QUERY EXECUTE format(
'SELECT cartodb_id::bigint, the_geom_webmercator, name::text, namealt::text, featurecla::text, scalerank::integer
FROM ne_50m_geography_marine_polys
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 6 AND zoom(scaleDenominator::numeric) <= 8 THEN
RETURN QUERY EXECUTE format(
'SELECT cartodb_id::bigint, the_geom_webmercator, name::text, namealt::text, featurecla::text, scalerank::integer
FROM ne_10m_geography_marine_polys
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSE
RETURN;
END IF;
END
$$
LANGUAGE 'plpgsql';
-- Rivers go below lakes, so rivers are drawn first (river = 0)
DROP FUNCTION IF EXISTS water_zoomed(text,box3d);
DROP FUNCTION IF EXISTS water_zoomed(text,text,box3d);
CREATE OR REPLACE FUNCTION water_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(id bigint, the_geom_webmercator geometry, name text, type text, is_lake integer, ne_scalerank integer, area bigint) AS
$$
BEGIN
IF zoom(scaleDenominator::numeric) >= 3 AND zoom(scaleDenominator::numeric) <= 4 THEN
RETURN QUERY EXECUTE format(
'(select cartodb_id::bigint AS id, the_geom_webmercator, name::text, ''river'' AS type, 0 AS is_lake, scalerank::integer, ST_Area(the_geom_webmercator)::bigint
from ne_50m_rivers_lake_centerlines
where the_geom_webmercator && $1)
UNION
(SELECT cartodb_id::bigint AS id, the_geom_webmercator, name::text, ''lake'' AS type, 1 AS is_lake, scalerank::integer, ST_Area(the_geom_webmercator)::bigint
FROM ne_50m_lakes
WHERE the_geom_webmercator && $1)
ORDER BY is_lake ASC'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 5 AND zoom(scaleDenominator::numeric) <= 7 THEN
RETURN QUERY EXECUTE format(
'(SELECT cartodb_id::bigint AS id, the_geom_webmercator, name::text, ''river'' AS type, 0 AS is_lake, scalerank::integer, ST_Area(the_geom_webmercator)::bigint
FROM ne_10m_rivers_lake_centerlines
WHERE the_geom_webmercator && $1)
UNION
(SELECT cartodb_id::bigint AS id, the_geom_webmercator, name, ''lake'' AS type, 1 AS is_lake, scalerank::integer, ST_Area(the_geom_webmercator)::bigint
FROM ne_10m_lakes
WHERE the_geom_webmercator && $1)
ORDER BY is_lake ASC'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 8 AND zoom(scaleDenominator::numeric) <= 10 THEN
RETURN QUERY EXECUTE format(
'SELECT id::bigint AS id, the_geom_webmercator, name::text, type::text,
(CASE WHEN type IN (''water'',''bay'',''riverbank'',''reservoir'')
AND ST_GeometryType(the_geom_webmercator) IN (''ST_Polygon'',''ST_MultiPolygon'') THEN 1 ELSE 0 END) as is_lake,
0 as ne_scalerank, area::bigint
FROM water_areas_z10
WHERE the_geom_webmercator && $1
ORDER BY is_lake ASC'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 11 AND zoom(scaleDenominator::numeric) <= 13 THEN
RETURN QUERY EXECUTE format(
'SELECT id::bigint AS id, the_geom_webmercator, name::text, type::text,
(CASE WHEN type IN (''water'',''bay'',''riverbank'',''reservoir'')
AND ST_GeometryType(the_geom_webmercator) IN (''ST_Polygon'',''ST_MultiPolygon'') THEN 1 ELSE 0 END) as is_lake,
0 AS ne_scalerank, area::bigint
FROM water_areas_z13
WHERE the_geom_webmercator && $1
ORDER BY is_lake ASC'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 14 THEN
RETURN QUERY EXECUTE format(
'SELECT id::bigint AS id, the_geom_webmercator, name::text, type::text,
(CASE WHEN type IN (''water'',''bay'',''riverbank'',''reservoir'')
AND ST_GeometryType(the_geom_webmercator) IN (''ST_Polygon'',''ST_MultiPolygon'') THEN 1 ELSE 0 END) as is_lake,
0 as ne_scalerank, area::bigint
FROM water_areas_z14plus
WHERE the_geom_webmercator && $1
ORDER BY is_lake ASC'
) USING bbox;
ELSE
RETURN;
END IF;
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS urban_areas_zoomed(text,box3d);
CREATE OR REPLACE FUNCTION urban_areas_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id bigint, scalerank integer, the_geom_webmercator geometry) AS
$$
SELECT cartodb_id::bigint, scalerank::integer, the_geom_webmercator
FROM ne_50m_urban_areas
WHERE the_geom_webmercator && bbox
AND zoom(scaleDenominator::numeric) < 5
UNION ALL
SELECT cartodb_id::bigint, scalerank::integer, the_geom_webmercator
FROM ne_10m_urban_areas
WHERE the_geom_webmercator && bbox
AND zoom(scaleDenominator::numeric) >= 5 AND zoom(scaleDenominator::numeric) < 9;
$$
LANGUAGE SQL;
DROP FUNCTION IF EXISTS country_city_labels_zoomed(text,box3d);
DROP FUNCTION IF EXISTS country_city_labels_zoomed(text,text,box3d);
CREATE OR REPLACE FUNCTION country_city_labels_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id bigint, name text, country_city text, the_geom_webmercator geometry, scalerank integer, place text, pop_est numeric, is_capital bool) AS
$$
BEGIN
IF zoom(scaleDenominator::numeric) <= 3 AND zoom(scaleDenominator::numeric) >= 1 THEN
RETURN QUERY EXECUTE format(
'SELECT cartodb_id::bigint, admin::text AS name, ''country''::text, the_geom_webmercator, scalerank::integer, ''''::text, pop_est::numeric, false
FROM ne_50m_admin_0_countries_lakes
WHERE the_geom_webmercator && $1
ORDER BY pop_est DESC'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) <= 5 AND zoom(scaleDenominator::numeric) >= 4 THEN
RETURN QUERY EXECUTE format(
'(SELECT cartodb_id::bigint, admin::text AS name, ''country''::text as country_city, the_geom_webmercator, scalerank::integer, ''''::text, pop_est::numeric, false as is_capital
FROM ne_50m_admin_0_countries_lakes
WHERE the_geom_webmercator && $1)
UNION
(select cartodb_id::bigint, name::text, ''city''::text as country_city, the_geom_webmercator, zoom::integer as scalerank, ''''::text, population::numeric as pop_est, capital = ''yes'' as is_capital
from z4to10
where the_geom_webmercator && $1 and zoom <= 5
order by scalerank asc, population desc nulls last)'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 6 AND zoom(scaleDenominator::numeric) <= 7 THEN
RETURN QUERY EXECUTE format(
'select cartodb_id::bigint, name::text, ''city''::text, the_geom_webmercator, zoom::integer as scalerank, ''''::text, population::numeric as pop_est, capital = ''yes'' as is_capital
from z4to10
where the_geom_webmercator && $1 and zoom <= 7
order by scalerank asc, population desc nulls last'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 8 AND zoom(scaleDenominator::numeric) <= 12 THEN
RETURN QUERY EXECUTE format(
'SELECT cartodb_id::bigint, name::text, ''city''::text, the_geom_webmercator, zoom::integer as scalerank, ''''::text, population::numeric as pop_est, capital = ''yes'' as is_capital
FROM z4to10
WHERE the_geom_webmercator && $1
ORDER BY scalerank ASC, population DESC NULLS LAST'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 13 THEN
RETURN QUERY EXECUTE format(
'SELECT id::bigint AS cartodb_id, name::text, ''city''::text, the_geom_webmercator, 99 as scalerank, place::text, numeric_or_zero(population) as pop_est, false as is_capital
FROM places
WHERE the_geom_webmercator && $1
ORDER BY population DESC NULLS LAST'
) USING bbox;
ELSE
RETURN;
END IF;
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS ne_10m_roads_zoomed(text,box3d);
CREATE OR REPLACE FUNCTION ne_10m_roads_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id integer, the_geom_webmercator geometry, type text, scalerank numeric) AS
$$
SELECT
cartodb_id,
the_geom_webmercator,
type::text,
scalerank::numeric
FROM ne_10m_roads
WHERE type NOT IN ('Ferry, seasonal', 'Ferry Route')
AND the_geom_webmercator && bbox
AND zoom(scaleDenominator::numeric) >= 6 AND zoom(scaleDenominator::numeric) < 9
ORDER BY scalerank DESC
$$
LANGUAGE SQL;
DROP FUNCTION IF EXISTS admin0boundaries_zoomed(text,box3d);
DROP FUNCTION IF EXISTS admin0boundaries_zoomed(text,text,box3d);
CREATE OR REPLACE FUNCTION admin0boundaries_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id bigint, the_geom_webmercator geometry, unit boolean) AS
$$
BEGIN
IF zoom(scaleDenominator::numeric) <= 9 AND zoom(scaleDenominator::numeric) >= 3 THEN
RETURN QUERY EXECUTE format(
'(SELECT cartodb_id::bigint, the_geom_webmercator, false
FROM ne_10m_admin_0_boundary_lines_land
WHERE fid_ne_10m NOT IN (372,374)
AND the_geom_webmercator && $1)
union
(SELECT cartodb_id::bigint, the_geom_webmercator, true
FROM ne_10m_admin_0_boundary_lines_map_units
WHERE the_geom_webmercator && $1)'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 10 THEN
RETURN QUERY EXECUTE format(
'SELECT id::bigint, the_geom_webmercator, false
FROM administrative
WHERE admin_level = ''2''
AND the_geom_webmercator && $1'
) USING bbox;
ELSE
RETURN;
END IF;
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS admin1boundaries_zoomed(text, box3d);
DROP FUNCTION IF EXISTS admin1boundaries_zoomed(text, text, box3d);
CREATE OR REPLACE FUNCTION admin1boundaries_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id bigint, name text, scalerank integer, the_geom_webmercator geometry, geomtype text) AS
$$
BEGIN
IF scaleDenominator::numeric > 139000000 THEN
RETURN QUERY EXECUTE format(
'select cartodb_id::bigint, ''''::text as name, scalerank::integer, the_geom_webmercator, ST_GeometryType(the_geom_webmercator) AS geomtype
from ne_50m_admin_1_states_provinces_lines_shp
where the_geom_webmercator && $1'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 3 AND zoom(scaleDenominator::numeric) <= 9 THEN
RETURN QUERY EXECUTE format(
'select cartodb_id::bigint, ''''::text as name, scalerank::integer, the_geom_webmercator, ST_GeometryType(the_geom_webmercator) AS geomtype
from ne_10m_admin_1_states_provinces_lines_shp
where the_geom_webmercator && $1'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 10 THEN
RETURN QUERY EXECUTE format(
'SELECT id::bigint, tags::hstore -> ''name'' as name, 0, the_geom_webmercator, ST_GeometryType(the_geom_webmercator) AS geomtype
FROM administrative
WHERE admin_level = ''4''
AND the_geom_webmercator && $1'
) USING bbox;
ELSE
RETURN;
END IF;
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS admin1_polygons_zoomed(text,box3d);
CREATE OR REPLACE FUNCTION admin1_polygons_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id integer, name text, scalerank integer, the_geom_webmercator geometry) AS
$$
SELECT
cartodb_id,
name::text,
scalerank::integer,
the_geom_webmercator
FROM ne_10m_admin_1_states_provinces
WHERE adm0_a3 IN ('USA','CAN','AUS')
AND the_geom_webmercator && bbox
AND zoom(scaleDenominator::numeric) >= 3 AND zoom(scaleDenominator::numeric) < 8;
$$
LANGUAGE SQL;
DROP FUNCTION IF EXISTS high_road(scaleDenominator text, bbox box3d);
DROP FUNCTION IF EXISTS high_road(schema text, scaleDenominator text, bbox box3d);
CREATE OR REPLACE FUNCTION high_road(scaleDenominator text, bbox box3d)
RETURNS TABLE(name text, ref text, the_geom_webmercator geometry, highway text, railway text, kind text, is_link text, is_tunnel text, is_bridge text, z_order integer) AS
$$
DECLARE
conditions TEXT;
BEGIN
-- TODO use zoom()
CASE
WHEN zoom(scaleDenominator::numeric) = 13 THEN
conditions := 'is_bridge=''no''';
WHEN zoom(scaleDenominator::numeric) >= 14 THEN
conditions := 'is_bridge=''no'' AND is_tunnel=''no''';
ELSE
conditions := 'true';
END CASE;
RETURN QUERY SELECT * FROM high_road(scaleDenominator, bbox, conditions);
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS high_road(scaleDenominator text, bbox box3d, conditions text);
DROP FUNCTION IF EXISTS high_road(schema text, scaleDenominator text, bbox box3d, conditions text);
CREATE OR REPLACE FUNCTION high_road(scaleDenominator text, bbox box3d, conditions text)
RETURNS TABLE(name text, ref text, the_geom_webmercator geometry, highway text, railway text, kind text, is_link text, is_tunnel text, is_bridge text, z_order integer) AS
$$
DECLARE
tablename TEXT;
BEGIN
CASE
WHEN zoom(scaleDenominator::numeric) >= 9 AND zoom(scaleDenominator::numeric) <= 10 THEN
tablename := 'highroad_z10';
WHEN zoom(scaleDenominator::numeric) = 11 THEN
tablename := 'highroad_z11';
WHEN zoom(scaleDenominator::numeric) = 12 THEN
tablename := 'highroad_z12';
WHEN zoom(scaleDenominator::numeric) = 13 THEN
tablename := 'highroad_z13';
WHEN zoom(scaleDenominator::numeric) = 14 THEN
tablename := 'highroad_z14';
WHEN zoom(scaleDenominator::numeric) >= 15 THEN
tablename := 'highroad_z15plus';
ELSE
RETURN;
END CASE;
RETURN QUERY EXECUTE format(
'SELECT name, ref, the_geom_webmercator, highway::text, railway::text, kind::text, is_link::text, is_tunnel::text, is_bridge::text, z_order
FROM %I
WHERE the_geom_webmercator && $1
AND %s ORDER BY z_order ASC', tablename, conditions
) USING bbox;
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS high_road_labels(scaleDenominator text, bbox box3d);
DROP FUNCTION IF EXISTS high_road_labels(schema text, scaleDenominator text, bbox box3d);
CREATE OR REPLACE FUNCTION high_road_labels(scaleDenominator text, bbox box3d)
RETURNS TABLE(name text, ref text, the_geom_webmercator geometry, highway text, railway text, kind text, is_link text, is_tunnel text, is_bridge text, z_order integer) AS
$$
DECLARE
tablename TEXT;
BEGIN
CASE
WHEN zoom(scaleDenominator::numeric) = 12 THEN
tablename := 'highroad_z12';
WHEN zoom(scaleDenominator::numeric) = 13 THEN
tablename := 'highroad_z13';
WHEN zoom(scaleDenominator::numeric) = 14 THEN
tablename := 'highroad_z14';
WHEN zoom(scaleDenominator::numeric) >= 15 THEN
tablename := 'highroad_z15plus';
ELSE
RETURN;
END CASE;
RETURN QUERY EXECUTE format(
'SELECT name, ref, the_geom_webmercator, highway::text, railway::text, kind::text, is_link::text, is_tunnel::text, is_bridge::text, z_order
FROM %I
WHERE the_geom_webmercator && $1
ORDER BY z_order ASC', tablename
) USING bbox;
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS tunnels(scaleDenominator text, bbox box3d);
DROP FUNCTION IF EXISTS tunnels(schema text, scaleDenominator text, bbox box3d);
CREATE OR REPLACE FUNCTION tunnels(scaleDenominator text, bbox box3d)
RETURNS TABLE(the_geom_webmercator geometry, highway text, railway text, kind text, is_link text, is_tunnel text, is_bridge text) AS
$$
DECLARE
tablename TEXT;
BEGIN
CASE
WHEN zoom(scaleDenominator::numeric) = 13 THEN
tablename := 'highroad_z13';
WHEN zoom(scaleDenominator::numeric) = 14 THEN
tablename := 'highroad_z14';
WHEN zoom(scaleDenominator::numeric) >= 15 THEN
tablename := 'highroad_z15plus';
ELSE
RETURN;
END CASE;
RETURN QUERY EXECUTE format(
'SELECT the_geom_webmercator, highway::text, railway::text, kind::text, is_link::text, is_tunnel::text, is_bridge::text
FROM %I
WHERE the_geom_webmercator && $1
AND is_tunnel = ''yes'' ORDER BY z_order ASC', tablename
) USING bbox;
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS bridges(scaleDenominator text, bbox box3d);
DROP FUNCTION IF EXISTS bridges(schema text, scaleDenominator text, bbox box3d);
CREATE OR REPLACE FUNCTION bridges(scaleDenominator text, bbox box3d)
RETURNS TABLE(the_geom_webmercator geometry, highway text, railway text, kind text, is_link text, is_tunnel text, is_bridge text) AS
$$
DECLARE
tablename TEXT;
BEGIN
CASE
WHEN zoom(scaleDenominator::numeric) = 13 THEN
tablename := 'highroad_z13';
WHEN zoom(scaleDenominator::numeric) = 14 THEN
tablename := 'highroad_z14';
WHEN zoom(scaleDenominator::numeric) >= 15 THEN
tablename := 'highroad_z15plus';
ELSE
RETURN;
END CASE;
RETURN QUERY EXECUTE format(
'SELECT the_geom_webmercator, highway::text, railway::text, kind::text, is_link::text, is_tunnel::text, is_bridge::text
FROM %I
WHERE the_geom_webmercator && $1
AND is_bridge = ''yes'' ORDER BY z_order ASC', tablename
) USING bbox;
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS buildings_zoomed(text,box3d);
DROP FUNCTION IF EXISTS buildings_zoomed(text,text,box3d);
CREATE OR REPLACE FUNCTION buildings_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id bigint, osm_id bigint, area bigint, the_geom_webmercator geometry) AS
$$
BEGIN
IF zoom(scaleDenominator::numeric) >= 12 AND zoom(scaleDenominator::numeric) <= 13 THEN
RETURN QUERY EXECUTE format(
'SELECT id::bigint AS cartodb_id, osm_id::bigint, area::bigint, the_geom_webmercator
FROM buildings_z13
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 14 THEN
RETURN QUERY EXECUTE format(
'SELECT id::bigint AS cartodb_id, osm_id::bigint, area::bigint, the_geom_webmercator
FROM buildings_z14plus
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSE
RETURN;
END IF;
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS green_areas_zoomed(text,box3d);
DROP FUNCTION IF EXISTS green_areas_zoomed(text,text,box3d);
CREATE OR REPLACE FUNCTION green_areas_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id bigint, name text, area bigint, the_geom_webmercator geometry) AS
$$
BEGIN
IF zoom(scaleDenominator::numeric) >= 9 AND zoom(scaleDenominator::numeric) <= 10 THEN
RETURN QUERY EXECUTE format(
'SELECT id::bigint AS cartodb_id, name, area::bigint, the_geom_webmercator
FROM green_areas_z10
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 11 AND zoom(scaleDenominator::numeric) <= 13 THEN
RETURN QUERY EXECUTE format(
'SELECT id::bigint AS cartodb_id, name, area::bigint, the_geom_webmercator
FROM green_areas_z13
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSIF zoom(scaleDenominator::numeric) >= 14 THEN
RETURN QUERY EXECUTE format(
'SELECT id::bigint AS cartodb_id, name, area::bigint, the_geom_webmercator
FROM green_areas_z14plus
WHERE the_geom_webmercator && $1'
) USING bbox;
ELSE
RETURN;
END IF;
END
$$
LANGUAGE 'plpgsql';
DROP FUNCTION IF EXISTS aeroways_zoomed(text,box3d);
DROP FUNCTION IF EXISTS aeroways_zoomed(text,text,box3d);
CREATE OR REPLACE FUNCTION aeroways_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id bigint, type text, the_geom_webmercator geometry) AS
$$
SELECT
id::bigint AS cartodb_id,
type::text,
the_geom_webmercator
FROM aeroways
WHERE the_geom_webmercator && bbox
AND zoom(scaleDenominator::numeric) >= 12;
$$
LANGUAGE SQL;
DROP FUNCTION IF EXISTS osm_admin_zoomed(text,box3d);
DROP FUNCTION IF EXISTS osm_admin_zoomed(text,text,box3d);
CREATE OR REPLACE FUNCTION osm_admin_zoomed(scaleDenominator text, bbox box3d)
RETURNS TABLE(cartodb_id bigint, admin_level text, the_geom_webmercator geometry) AS
$$
SELECT
id::bigint AS cartodb_id,
admin_level::text,
the_geom_webmercator
FROM administrative
WHERE the_geom_webmercator && bbox
AND zoom(scaleDenominator::numeric) >= 9;
$$
LANGUAGE SQL;
COMMIT;
-- Not needed unless another file is being used in the same session
RESET client_min_messages;