-
Notifications
You must be signed in to change notification settings - Fork 2
/
queryprocs.sql.in
775 lines (678 loc) · 24.9 KB
/
queryprocs.sql.in
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
-- queryprocs.sql.in --
--
-- Procedures to query the OpenStreetMap database for roads that
-- should have markers placed on them on a map
--
-- This file has been placed in the Public Domain in Common Law countries,
-- and is licenced under Creative Commons Zero in all jurisdictions.
--
-----------------------------------------------------------------------------
-----------
-- USAGE --
-----------
-- This file is designed to be given to PostgreSQL as a script after
-- replacing the string, @PREFIX@, with the prefix under which OSM
-- data have been imported, e.g., 'planet_osm'. A suitable Unix
-- command to accomplish this might be:
--
-- sed s/@PREFIX@/planet_osm/g <queryprocs.sql.in | psql -d gis
--------------------
-- INITIALIZATION --
--------------------
-- Clean up any trash from a previous run
DROP FUNCTION IF EXISTS @PREFIX@_query_shields_roads_motorway(GEOMETRY, INT)
CASCADE
\g
DROP FUNCTION IF EXISTS @PREFIX@_query_shields_roads_major(GEOMETRY, INT)
CASCADE
\g
DROP FUNCTION IF EXISTS @PREFIX@_query_shields_roads_all(GEOMETRY, INT)
CASCADE
\g
DROP FUNCTION IF EXISTS @PREFIX@_query_shields_line_all(GEOMETRY, INT)
CASCADE
\g
DROP FUNCTION IF EXISTS @PREFIX@_analyze_markers(@PREFIX@_road_segment[], INT)
CASCADE
\g
DROP FUNCTION IF EXISTS @PREFIX@_make_repeat_key(@PREFIX@_gid_label[])
CASCADE
\g
DROP FUNCTION IF EXISTS @PREFIX@_gids_to_files(INT[]) -- former function
CASCADE
\g
DROP FUNCTION IF EXISTS @PREFIX@_gids_to_files(@PREFIX@_gid_label[])
CASCADE
\g
DROP TYPE IF EXISTS @PREFIX@_file_label CASCADE
\g
DROP TYPE IF EXISTS @PREFIX@_gid_label CASCADE
\g
DROP TYPE IF EXISTS @PREFIX@_road_segment CASCADE
\g
-----------
-- TYPES --
-----------
-- Type: @PREFIX@_road_segment
--
-- A 'road_segment' is a piece of a road. An array of road segments
-- will be constructed to represent the network of numbered routes
-- that are to be rendered, and passed into @PREFIX@_analyze_markers
-- to produce the data set that will drive the renderer.
CREATE TYPE
@PREFIX@_road_segment AS(
osm_id BIGINT, -- The OSM ID of the segment
way GEOMETRY, -- The geometry of the segment
"ref" TEXT, -- The 'ref' tag on the segment
highway TEXT -- The 'highway' tag on the segment
)
\g
-- Type: @PREFIX@_gid_label
--
-- A 'gid_label' is an ordered pair of a graphic file ID and
-- a textual label to attach to it.
CREATE TYPE
@PREFIX@_gid_label AS (
gid INT, -- Graphics file ID
label TEXT -- Textual label
)
\g
-- Type: @PREFIX_file_label
--
-- A 'file_label' is an ordered pair of a graphic file name and
-- a textual label to attach to it
CREATE TYPE
@PREFIX@_file_label AS (
filename TEXT, -- Graphics file name
label TEXT -- Textual label
)
\g
---------------
-- FUNCTIONS --
---------------
-- Function: @PREFIX@_gids_to_files
--
-- Converts a table with graphics IDs and labels into a table
-- with filenames and labels
--
-- Parameters:
-- gids - Array of graphics ID/label pairs
--
-- Results:
-- Returns an array with graphics IDs replaced with filenames
--
-- Since each graphics ID is a primary key in the 'osm_shield_graphics'
-- table, this function need only join the array to the table,
-- select the filenames and aggregate the result.
CREATE OR REPLACE FUNCTION
@PREFIX@_gids_to_files(gid_labels @PREFIX@_gid_label[])
RETURNS @PREFIX@_file_label[]
LANGUAGE plpgsql
AS $@PREFIX@_gids_to_files$
BEGIN
RETURN (
SELECT ARRAY_AGG((g.filename, (t.glabel).label)::@PREFIX@_file_label)
FROM (SELECT UNNEST(gid_labels) AS glabel) AS t
JOIN osm_shield_graphics AS g
ON g.id = (t.glabel).gid
);
END
$@PREFIX@_gids_to_files$
\g
-- Function: @PREFIX@_make_repeat_key --
--
-- Makes a repeat key from an array of graphic-id/label pairs
--
-- Parameters:
-- glabels - Array of gid/label pairs
--
-- Results:
-- Returns a string that may serve as a repeat key
CREATE OR REPLACE FUNCTION
@PREFIX@_make_repeat_key(glabels @PREFIX@_gid_label[])
RETURNS TEXT
LANGUAGE plpgsql
AS
$@PREFIX@_make_repeat_key$
BEGIN
RETURN (
WITH q1 AS (
SELECT x.glabel
FROM (SELECT UNNEST(glabels) AS glabel) AS x
)
SELECT ARRAY_TO_STRING(ARRAY_AGG(
(q1.glabel).gid || '~' || (q1.glabel).label), '|')
FROM q1
);
END
$@PREFIX@_make_repeat_key$
\g
-- Function: @PREFIX@_analyze_markers --
--
-- Analyzes a set of road segments, and returns a set of ways, together
-- with the data needed to support shield rendering.
--
-- Parameters:
-- segs -- Array of '@PREFIX@_road_segment' objects that represent
-- roads that might be candidates for labelling with markers.
-- size_wanted -- The desired height of the graphic elements in pixels.
-- Because of banners, odd shield proportions, and similar
-- issues, the returned graphic may not be precisely
-- the nominal size, but should harmonize with other
-- graphics of the same nominal size.
--
-- Results:
-- Returns a table giving ways that actually should be labeled with
-- markers. Each way has a repeat key that informs Mapnik what marker
-- set it belongs to (to eliminate markers that are too close together)
-- and up to eight occurrences of a file name containing the marker
-- graphic and a string (often empty) to layer atop the graphic.
--
-- The ways passed in are ordinarily not in one-to-one correspondence
-- with the ways returned. Sets of ways that share the same set of markers
-- are coalesced if possible, so the returned linestrings are typically
-- longer than the ones passed as parameters.
--
-- Example:
--
-- Ordinarily, this stored procedure, or one that wraps it, will
-- be used as input to some rendering layer with a given style.
-- The set of input ways is derived from a query on one of the OSM
-- line tables, so the 'table' parameter in the layer's datasource
-- will look something like:
--
-- (SELECT * from @PREFIX@_analyze_markers(
-- SELECT ARRAY_AGG(ROW(rd.osm_id, rd.way,
-- rd."ref", rd.highway)::@PREFIX@_road_segment)
-- FROM @PREFIX@_osm_roads
-- WHERE ST_Intersects(!bbox!, way) ))
CREATE OR REPLACE FUNCTION
@PREFIX@_analyze_markers(segs @PREFIX@_road_segment[],
size_wanted INT)
RETURNS TABLE(
way GEOMETRY, -- Geometry of a resulting linestring
repeat_key TEXT, -- Repeat key used to control marker spacing
picture_1 TEXT, -- First graphic element
label_1 TEXT, -- First label
picture_2 TEXT, -- ...
label_2 TEXT,
picture_3 TEXT,
label_3 TEXT,
picture_4 TEXT,
label_4 TEXT,
picture_5 TEXT,
label_5 TEXT,
picture_6 TEXT,
label_6 TEXT,
picture_7 TEXT,
label_7 TEXT,
picture_8 TEXT, -- Last graphic element
label_8 TEXT -- Last label
)
LANGUAGE plpgsql
AS
$@PREFIX@_analyze_markers$
BEGIN
RETURN QUERY ((
--------------------------------------------------------------
-- --
-- Pathway for places that use route relations to represent --
-- road routes --
-- --
--------------------------------------------------------------
-- The first common table expression does the following things:
-- + Unpacks the input array into a set of rows again.
-- + Finds any route relations in which any input way participates
-- + Downselects to 'road' routes
-- + Looks up graphic elements corresponding to the routes
-- + Packages tuples of (osm_id, way, highway,
-- network, ref, graphic_id)
WITH q10 AS (
SELECT ln.osm_id AS id,
ln.way AS way,
ln.highway AS highway,
rt.route AS route,
rt.network AS network,
rt."ref" AS "ref",
g.id AS gid
FROM UNNEST(segs) AS ln
JOIN @PREFIX@_shieldway sw
ON sw.wayid = ln.osm_id
JOIN @PREFIX@_shieldroute rt
ON rt.relid = sw.relid
AND rt.route = 'road'
LEFT JOIN osm_shield_graphics g
ON g.route = rt.route
AND g.network = rt.network
AND g."ref" = CASE
WHEN rt."ref" IS NULL THEN ''
ELSE rt."ref"
END
AND g."size" = size_wanted
ORDER BY network, "ref"
),
-- The graphic id in the above query may be NULL because the
-- network is unknown, or there is no graphic for the ref. Replace
-- the NULL graphic ID with an appropriate generic graphic ID, and
-- include a text label to fill in the blank box.
q15 AS (
SELECT q10.id, q10.way, q10.network, q10.ref,
( q10.gid, '' )::@PREFIX@_gid_label AS glabel
FROM q10
WHERE q10.gid IS NOT NULL
UNION ALL
SELECT q10.id, q10.way, q10.network, q10.ref,
( g.id, q10.ref )::@PREFIX@_gid_label AS glabel
FROM q10
JOIN osm_shield_graphics g
ON g.route = q10.route
AND g.network = CASE
WHEN q10.highway IN ('motorway', 'trunk',
'primary', 'secondary')
THEN 'generic-' || q10.highway
ELSE 'generic-other'
END
AND g.ref = '1x' || LENGTH(q10.ref)
WHERE q10.gid IS NULL
),
-- The next common table expression repackages the result of the
-- first so that all graphic elements for a given way appear packaged
-- together in a single row. The result is a table of
-- (osm_id, way, gid_label[])
q20 AS (
SELECT q15.id AS xid, -- OSM ID
ST_Union(q15.way) AS way, -- This union is always of a single
-- way with itself.
ARRAY_AGG(q15.glabel) AS glabels -- Graphic ID's, aggregated
FROM q15
GROUP BY xid
),
-- The next common table expression merges the ways corresponding
-- to each set of route relations being processed. The result has
-- one row per distinct set, consisting of an ordered pair of
-- the merged ways and the array of gids/labels. The ST_LineMerge
-- call serves to coalesce ways that share a common set of routes
-- into longer linestrings.
q30 AS (
SELECT ST_LineMerge(ST_Union(q20.way)) AS way,
q20.glabels AS glabels
FROM q20
GROUP BY glabels
),
-- Now that we've produced pairs of (way, gids/labels) we want to
-- bring in the graphic file names. They have not been carried up
-- to this point, because the 'GROUP BY' in the step above would
-- be inordinately slow if carried out over an arbitrary set of TEXT.
q40 AS (
SELECT q30.way as way,
@PREFIX@_make_repeat_key(q30.glabels) AS repeat_key,
@PREFIX@_gids_to_files(q30.glabels) AS flabels
FROM q30
)
-- Finally, we can package the query result. The set of graphic ID's,
-- separated by vertical bars, becomes the repeat key, and the
-- individual graphic files are in the result table. In this pipeline,
-- none of the graphics requires labeling.
SELECT
q40.way AS way,
q40.repeat_key AS repeat_key,
q40.flabels[1].filename AS picture_1,
q40.flabels[1].label AS label_1,
q40.flabels[2].filename AS picture_2,
q40.flabels[2].label AS label_2,
q40.flabels[3].filename AS picture_3,
q40.flabels[3].label AS label_3,
q40.flabels[4].filename AS picture_4,
q40.flabels[4].label AS label_4,
q40.flabels[5].filename AS picture_5,
q40.flabels[5].label AS label_5,
q40.flabels[6].filename AS picture_6,
q40.flabels[6].label AS label_6,
q40.flabels[7].filename AS picture_7,
q40.flabels[7].label AS label_7,
q40.flabels[8].filename AS picture_8,
q40.flabels[8].label AS label_8
FROM q40
----------------------------------------------------------------
-- --
-- End of the pathway that works with route relations --
-- --
----------------------------------------------------------------
) UNION ALL (
----------------------------------------------------------------
-- --
-- Pathway for places that represent road routes only with --
-- (possibly multiply-valued) 'ref' tags on the relation --
-- --
----------------------------------------------------------------
-- From all the input roads, isolate the ones that have 'ref'
-- tags, are not link roads, and do not participate in route
-- relations. Group the ones that have the same sets of refs
-- and are the same type of highway. Merge the linstrings
-- for the grouped sets. Pass on the merged ways, the
-- highway type, and an array of refs.
--
-- TODO: The deprecated ref_1, ref_2, ... may have to be
-- included here.
WITH q110 AS (
SELECT
ST_LineMerge(ST_Union(rd.way)) AS way,
STRING_TO_ARRAY(rd."ref", ';') AS refs,
rd.highway AS highway
FROM UNNEST(segs) AS rd
LEFT JOIN @PREFIX@_shieldway sw
ON sw.wayid = rd.osm_id
WHERE rd.highway NOT LIKE '%_link'
AND rd."ref" IS NOT NULL
AND sw.wayid IS NULL
GROUP BY highway, refs
),
-- Refs longer than 10 characters won't fit on a rendered shield,
-- so it's not reasonable to keep them here. Discard them.
q120 AS (
SELECT q110.way AS way,
q110.highway AS highway,
(SELECT ARRAY_AGG(x)
FROM UNNEST(q110.refs) AS x
WHERE LENGTH(x) <= 10) AS refs
FROM q110
),
-- For each set of merged roads, make a string of refs separated by
-- newlines. Calculate the width and height, in characters, of a box
-- big enough to display them
q130 AS (
SELECT q120.way AS way,
-- The linestrings that get shields
q120.highway AS highway,
-- The type of highway
ARRAY_TO_STRING(q120.refs, E'\n') AS refstack,
-- The references, stacked with newlines
(SELECT MAX(LENGTH(x))
FROM UNNEST(q120.refs) AS x
) AS w,
-- The maximum width of a reference
CARDINALITY(q120.refs) AS h
-- The height of the stack of references
FROM q120
),
-- Go find the appropriate box to surround the labels
q140 AS (
SELECT q130.way AS way,
g.id AS gid,
g.filename AS picture,
q130.refstack AS label
FROM q130
JOIN osm_shield_graphics g
ON g.route = 'road'
AND g.network = CASE
WHEN q130.highway IN ('motorway', 'primary',
'trunk', 'secondary')
THEN 'generic-' || q130.highway
ELSE 'generic-other'
END
AND g.ref = q130.h || 'x' || q130.w
)
-- Package the result. We put all the stacked refs in a single marker
SELECT q140.way AS way,
q140.gid || '~' || q140.label AS repeat_key,
q140.picture AS picture_1,
q140.label AS label_1,
NULL AS picture_2,
NULL AS label_2,
NULL AS picture_3,
NULL AS label_3,
NULL AS picture_4,
NULL AS label_4,
NULL AS picture_5,
NULL AS label_5,
NULL AS picture_6,
NULL AS label_6,
NULL AS picture_7,
NULL AS label_7,
NULL AS picture_8,
NULL AS label_8
FROM q140
));
END
$@PREFIX@_analyze_markers$
\g
-- Function: @PREFIX@_query_shields_line_all --
--
-- Calculate and return the ways on which highway shields
-- should be rendered, considering all highways of whatever
-- prominence within the bounding box.
--
-- Parameters:
-- bbox - Bounding box of the area being rendered
-- size_wanted - Nominal height of graphic elements in pixels
--
-- Results:
-- Returns a table giving ways that actually should be labeled with
-- markers. Each way has a repeat key that informs Mapnik what marker
-- set it belongs to (to eliminate markers that are too close together)
-- and up to eight occurrences of a file name containing the marker
-- graphic and a string (often empty) to layer atop the graphic.
CREATE OR REPLACE FUNCTION
@PREFIX@_query_shields_line_all(bbox GEOMETRY, size_wanted INT)
RETURNS TABLE(
way GEOMETRY, -- Geometry of a resulting linestring
repeat_key TEXT, -- Repeat key used to control marker spacing
picture_1 TEXT, -- First graphic element
label_1 TEXT, -- First label
picture_2 TEXT, -- ...
label_2 TEXT,
picture_3 TEXT,
label_3 TEXT,
picture_4 TEXT,
label_4 TEXT,
picture_5 TEXT,
label_5 TEXT,
picture_6 TEXT,
label_6 TEXT,
picture_7 TEXT,
label_7 TEXT,
picture_8 TEXT, -- Last graphic element
label_8 TEXT -- Last label
)
LANGUAGE plpgsql
AS $@PREFIX@_query_shields_line_all$
BEGIN
RETURN QUERY(
SELECT * FROM @PREFIX@_analyze_markers(
(
---------------------------------------------------------------
-- This is the base query that yields the roads that
-- might need markers. For this particular use case,
-- it returns all highways in the bounding box.
SELECT ARRAY_AGG(ROW(l.osm_id, l.way,
l."ref", l.highway)::@PREFIX@_road_segment)
FROM @PREFIX@_line l
WHERE ST_Intersects(l.way, ST_SetSRID(bbox, 3857))
AND l.highway IS NOT NULL
---------------------------------------------------------------
),
size_wanted));
END
$@PREFIX@_query_shields_line_all$
\g
-- Function: @PREFIX@_query_shields_roads_all --
--
-- Calculate and return the ways on which highway shields
-- should be rendered, considering all highways in '@PREFIX@_roads'
-- within the bounding box.
--
-- Parameters:
-- bbox - Bounding box of the area being rendered
-- size_wanted - Nominal height of graphic elements in pixels
--
-- Results:
-- Returns a table giving ways that actually should be labeled with
-- markers. Each way has a repeat key that informs Mapnik what marker
-- set it belongs to (to eliminate markers that are too close together)
-- and up to eight occurrences of a file name containing the marker
-- graphic and a string (often empty) to layer atop the graphic.
CREATE FUNCTION
@PREFIX@_query_shields_roads_all(bbox GEOMETRY, size_wanted INT)
RETURNS TABLE(
way GEOMETRY, -- Geometry of a resulting linestring
repeat_key TEXT, -- Repeat key used to control marker spacing
picture_1 TEXT, -- First graphic element
label_1 TEXT, -- First label
picture_2 TEXT, -- ...
label_2 TEXT,
picture_3 TEXT,
label_3 TEXT,
picture_4 TEXT,
label_4 TEXT,
picture_5 TEXT,
label_5 TEXT,
picture_6 TEXT,
label_6 TEXT,
picture_7 TEXT,
label_7 TEXT,
picture_8 TEXT, -- Last graphic element
label_8 TEXT -- Last label
)
LANGUAGE plpgsql
AS $@PREFIX@_query_shields_roads_all$
BEGIN
RETURN QUERY(
SELECT * FROM @PREFIX@_analyze_markers(
(
---------------------------------------------------------------
-- This is the base query that yields the roads that
-- might need markers. For this particular use case,
-- it returns all roads in the bounding box.
SELECT ARRAY_AGG(ROW(l.osm_id, l.way,
l."ref", l.highway)::@PREFIX@_road_segment)
FROM @PREFIX@_roads l
WHERE ST_Intersects(l.way, ST_SetSRID(bbox, 3857))
AND l.highway IS NOT NULL
---------------------------------------------------------------
),
size_wanted));
END
$@PREFIX@_query_shields_roads_all$
\g
-- Function: @PREFIX@_query_shields_roads_major --
--
-- Calculate and return the ways on which highway shields
-- should be rendered, considering major highways in '@PREFIX@_roads'
-- within the bounding box.
--
-- Parameters:
-- bbox - Bounding box of the area being rendered
-- size_wanted - Nominal height of graphic elements in pixels
--
-- Results:
-- Returns a table giving ways that actually should be labeled with
-- markers. Each way has a repeat key that informs Mapnik what marker
-- set it belongs to (to eliminate markers that are too close together)
-- and up to eight occurrences of a file name containing the marker
-- graphic and a string (often empty) to layer atop the graphic.
--
-- A highway is considered to be 'major' if it is a motorway, trunk
-- or primary way.
CREATE FUNCTION
@PREFIX@_query_shields_roads_major(bbox GEOMETRY, size_wanted INT)
RETURNS TABLE(
way GEOMETRY, -- Geometry of a resulting linestring
repeat_key TEXT, -- Repeat key used to control marker spacing
picture_1 TEXT, -- First graphic element
label_1 TEXT, -- First label
picture_2 TEXT, -- ...
label_2 TEXT,
picture_3 TEXT,
label_3 TEXT,
picture_4 TEXT,
label_4 TEXT,
picture_5 TEXT,
label_5 TEXT,
picture_6 TEXT,
label_6 TEXT,
picture_7 TEXT,
label_7 TEXT,
picture_8 TEXT, -- Last graphic element
label_8 TEXT -- Last label
)
LANGUAGE plpgsql
AS $@PREFIX@_query_shields_roads_major$
BEGIN
RETURN QUERY(
SELECT * FROM @PREFIX@_analyze_markers(
(
---------------------------------------------------------------
-- This is the base query that yields the roads that
-- might need markers. For this particular use case,
-- it returns all motorways, trunks and primary highways
-- in the bounding box.
SELECT ARRAY_AGG(ROW(l.osm_id, l.way,
l."ref", l.highway)::@PREFIX@_road_segment)
FROM @PREFIX@_roads l
WHERE ST_Intersects(l.way, ST_SetSRID(bbox, 3857))
AND l.highway IN ('motorway', 'trunk', 'primary')
---------------------------------------------------------------
),
size_wanted));
END
$@PREFIX@_query_shields_roads_major$
\g
-- Function: @PREFIX@_query_shields_roads_motorway --
--
-- Calculate and return the ways on which highway shields
-- should be rendered, considering only motorways in '@PREFIX@_roads'
-- within the bounding box.
--
-- Parameters:
-- bbox - Bounding box of the area being rendered
-- size_wanted - Nominal height of graphic elements in pixels
--
-- Results:
-- Returns a table giving ways that actually should be labeled with
-- markers. Each way has a repeat key that informs Mapnik what marker
-- set it belongs to (to eliminate markers that are too close together)
-- and up to eight occurrences of a file name containing the marker
-- graphic and a string (often empty) to layer atop the graphic.
CREATE FUNCTION
@PREFIX@_query_shields_roads_motorway(bbox GEOMETRY, size_wanted INT)
RETURNS TABLE(
way GEOMETRY, -- Geometry of a resulting linestring
repeat_key TEXT, -- Repeat key used to control marker spacing
picture_1 TEXT, -- First graphic element
label_1 TEXT, -- First label
picture_2 TEXT, -- ...
label_2 TEXT,
picture_3 TEXT,
label_3 TEXT,
picture_4 TEXT,
label_4 TEXT,
picture_5 TEXT,
label_5 TEXT,
picture_6 TEXT,
label_6 TEXT,
picture_7 TEXT,
label_7 TEXT,
picture_8 TEXT, -- Last graphic element
label_8 TEXT -- Last label
)
LANGUAGE plpgsql
AS $@PREFIX@_query_shields_roads_motorway$
BEGIN
RETURN QUERY(
SELECT * FROM @PREFIX@_analyze_markers(
(
---------------------------------------------------------------
-- This is the base query that yields the roads that
-- might need markers. For this particular use case,
-- it returns all motorways in the bounding box.
SELECT ARRAY_AGG(ROW(l.osm_id, l.way,
l."ref", l.highway)::@PREFIX@_road_segment)
FROM @PREFIX@_roads l
WHERE ST_Intersects(l.way, ST_SetSRID(bbox, 3857))
AND l.highway = 'motorway'
---------------------------------------------------------------
),
size_wanted));
END
$@PREFIX@_query_shields_roads_motorway$
\g