File: project.mml

package info (click to toggle)
openstreetmap-carto 5.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,448 kB
  • sloc: python: 650; sh: 297; sql: 70; makefile: 7
file content (2331 lines) | stat: -rw-r--r-- 95,784 bytes parent folder | download
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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
# Various parts to be included later on
_parts:
  srid: &srid "3857"
  srs: &srs "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over"
  world: &world
    - -180
    - -85.05112877980659
    - 180
    - 85.05112877980659
  # Extents are used for tilemill, and don't actually make it to the generated XML
  extents: &extents
    extent: *world
    srs-name: *srid
    srs: *srs
  osm2pgsql: &osm2pgsql
    type: "postgis"
    dbname: "gis"
    key_field: ""
    geometry_field: "way"
    srid: *srid
    extent: "-20037508,-20037508,20037508,20037508"

scale: 1
metatile: 2
name: OpenStreetMap Carto
description: A general-purpose OpenStreetMap mapnik style, in CartoCSS
bounds: *world
center:
  - 0
  - 0
  - 4
format: png
interactivity: false
minzoom: 0
maxzoom: 22
srs: *srs

Stylesheet:
  - style/style.mss
  - style/fonts.mss
  - style/shapefiles.mss
  - style/landcover.mss
  - style/water.mss
  - style/water-features.mss
  - style/road-colors-generated.mss
  - style/roads.mss
  - style/power.mss
  - style/placenames.mss
  - style/buildings.mss
  - style/stations.mss
  - style/amenity-points.mss
  - style/ferry-routes.mss
  - style/aerialways.mss
  - style/admin.mss
  - style/addressing.mss
  - style/golf.mss
  - style/tourism.mss
Layer:
  - id: landcover-low-zoom
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way, way_pixels,
            COALESCE(wetland, landuse, "natural") AS feature
          FROM (SELECT
              way,
              ('landuse_' || (CASE WHEN landuse IN ('forest', 'farmland', 'residential', 'commercial', 'retail', 'industrial',
                                                    'meadow', 'grass', 'village_green', 'vineyard', 'orchard') THEN landuse END)) AS landuse,
              ('natural_' || (CASE WHEN "natural" IN ('wood', 'sand', 'scree', 'shingle', 'bare_rock', 'heath', 'grassland', 'scrub') THEN "natural" END)) AS "natural",
              ('wetland_' || (CASE WHEN "natural" IN ('wetland', 'mud') THEN (CASE WHEN "natural" IN ('mud') THEN "natural" ELSE tags->'wetland' END) END)) AS wetland,
              way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels,
              way_area
            FROM planet_osm_polygon
            WHERE (landuse IN ('forest', 'farmland', 'residential', 'commercial', 'retail', 'industrial', 'meadow', 'grass', 'village_green', 'vineyard', 'orchard')
              OR "natural" IN ('wood', 'wetland', 'mud', 'sand', 'scree', 'shingle', 'bare_rock', 'heath', 'grassland', 'scrub'))
              AND way_area > 0.01*!pixel_width!::real*!pixel_height!::real
              AND building IS NULL
          ) AS features
          ORDER BY way_area DESC, feature
        ) AS landcover_low_zoom
    properties:
      cache-features: true
      minzoom: 5
      maxzoom: 9
  - id: landcover
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way, name, religion, way_pixels, is_building,
            COALESCE(aeroway, golf, amenity, wetland, power, landuse, leisure, man_made, "natural", shop, tourism, highway, railway) AS feature
          FROM (SELECT
              way, COALESCE(name, '') AS name,
              ('aeroway_' || (CASE WHEN aeroway IN ('apron', 'aerodrome') THEN aeroway END)) AS aeroway,
              ('golf_' || (CASE WHEN (tags->'golf') IN ('rough', 'fairway', 'driving_range', 'water_hazard', 'green', 'bunker', 'tee') THEN tags->'golf' ELSE NULL END)) AS golf,
              ('amenity_' || (CASE WHEN amenity IN ('bicycle_parking', 'motorcycle_parking', 'university', 'college', 'school', 'taxi',
                                                    'hospital', 'kindergarten', 'grave_yard', 'prison', 'place_of_worship', 'clinic', 'ferry_terminal',
                                                    'marketplace', 'community_centre', 'social_facility', 'arts_centre', 'parking_space', 'bus_station',
                                                    'fire_station', 'police')
                              OR amenity IN ('parking') AND (tags->'parking' NOT IN ('underground') OR (tags->'parking') IS NULL) THEN amenity END)) AS amenity,
              ('landuse_' || (CASE WHEN landuse IN ('quarry', 'vineyard', 'orchard', 'cemetery', 'residential', 'garages', 'meadow', 'grass',
                                                    'allotments', 'forest', 'farmyard', 'farmland', 'greenhouse_horticulture',
                                                    'recreation_ground', 'village_green', 'retail', 'industrial', 'railway', 'commercial',
                                                    'brownfield', 'landfill', 'salt_pond', 'construction', 'plant_nursery', 'religious', 'flowerbed') THEN landuse END)) AS landuse,
              ('shop_' || (CASE WHEN shop IN ('mall') AND (tags->'location' NOT IN ('underground') OR (tags->'location') IS NULL) THEN shop END)) AS shop,
              ('leisure_' || (CASE WHEN leisure IN ('swimming_pool', 'playground', 'park', 'garden',
                                                    'golf_course', 'miniature_golf', 'sports_centre', 'stadium', 'pitch', 'ice_rink',
                                                    'track', 'dog_park', 'fitness_station', 'water_park') THEN leisure END)) AS leisure,
              ('man_made_' || (CASE WHEN man_made IN ('works', 'wastewater_plant', 'water_works') THEN man_made END)) AS man_made,
              ('natural_' || (CASE WHEN "natural" IN ('beach', 'shoal', 'heath', 'grassland', 'wood', 'sand', 'scree', 'shingle', 'bare_rock', 'scrub') THEN "natural" END)) AS "natural",
              ('wetland_' || (CASE WHEN "natural" IN ('wetland', 'mud') THEN (CASE WHEN "natural" = 'mud' THEN "natural" ELSE tags->'wetland' END) END)) AS wetland,
              ('power_' || (CASE WHEN power IN ('plant', 'substation', 'generator') THEN power END)) AS power,
              ('tourism_' || (CASE WHEN tourism IN ('camp_site', 'caravan_site', 'picnic_site') THEN tourism END)) AS tourism,
              ('highway_' || (CASE WHEN highway IN ('services', 'rest_area') THEN highway END)) AS highway,
              ('railway_' || (CASE WHEN railway = 'station' THEN railway END)) AS railway,
              CASE WHEN religion IN ('christian', 'jewish', 'muslim') THEN religion ELSE 'INT-generic'::text END AS religion,
              way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels,
              CASE WHEN building = 'no' OR building IS NULL THEN 'no' ELSE 'yes' END AS is_building,
              way_area
            FROM planet_osm_polygon
            WHERE (landuse IS NOT NULL
              OR leisure IS NOT NULL
              OR aeroway IN ('apron', 'aerodrome')
              OR (tags->'golf') IS NOT NULL
              OR amenity IN ('parking', 'bicycle_parking', 'motorcycle_parking', 'taxi', 'university', 'college', 'school', 'hospital', 'kindergarten',
                             'grave_yard', 'place_of_worship', 'prison', 'clinic', 'ferry_terminal', 'marketplace', 'community_centre', 'social_facility',
                             'arts_centre', 'parking_space', 'bus_station', 'fire_station', 'police')
              OR man_made IN ('works', 'wastewater_plant','water_works')
              OR "natural" IN ('beach', 'shoal', 'heath', 'mud', 'wetland', 'grassland', 'wood', 'sand', 'scree', 'shingle', 'bare_rock', 'scrub')
              OR power IN ('plant', 'substation', 'generator')
              OR shop IN ('mall')
              OR tourism IN ('camp_site', 'caravan_site', 'picnic_site')
              OR highway IN ('services', 'rest_area')
              OR railway = 'station')
              AND way_area > 1*!pixel_width!::real*!pixel_height!::real
          ) AS landcover
          ORDER BY way_area DESC, feature
        ) AS features
    properties:
      cache-features: true
      minzoom: 10
  - id: landcover-line
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way
          FROM planet_osm_line
          WHERE man_made = 'cutline'
        ) AS landcover_line
    properties:
      minzoom: 14
  - id: icesheet-poly
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way
          FROM icesheet_polygons
        ) AS icesheet_polygons
    properties:
      minzoom: 5
  - id: water-lines-low-zoom
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            waterway,
            CASE WHEN tags->'intermittent' IN ('yes')
              OR tags->'seasonal' IN ('yes', 'spring', 'summer', 'autumn', 'winter', 'wet_season', 'dry_season')
              THEN 'yes' ELSE 'no' END AS int_intermittent
          FROM planet_osm_line
          WHERE waterway = 'river'
        ) AS water_lines_low_zoom
    properties:
      minzoom: 8
      maxzoom: 11
  - id: water-lines
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            waterway,
            name,
            CASE WHEN tags->'intermittent' IN ('yes')
              OR tags->'seasonal' IN ('yes', 'spring', 'summer', 'autumn', 'winter', 'wet_season', 'dry_season')
              THEN 'yes' ELSE 'no' END AS int_intermittent,
            CASE WHEN tunnel IN ('yes', 'culvert') 
              OR waterway = 'canal' AND tunnel = 'flooded'
              THEN 'yes' ELSE 'no' END AS int_tunnel,
            'no' AS bridge
          FROM planet_osm_line
          WHERE waterway IN ('river', 'canal', 'stream', 'drain', 'ditch')
            AND (bridge IS NULL OR bridge NOT IN ('yes', 'aqueduct'))
          ORDER BY COALESCE(layer,0)
        ) AS water_lines
    properties:
      minzoom: 12
  - id: water-areas
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            "natural",
            waterway,
            landuse,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels,
            CASE WHEN tags->'intermittent' IN ('yes')
              OR tags->'seasonal' IN ('yes', 'spring', 'summer', 'autumn', 'winter', 'wet_season', 'dry_season')
              OR tags->'basin' IN ('detention', 'infiltration')
              THEN 'yes' ELSE 'no' END AS int_intermittent
          FROM planet_osm_polygon
          WHERE
            (waterway IN ('dock', 'riverbank')
              OR landuse IN ('reservoir', 'basin')
              OR "natural" IN ('water', 'glacier'))
            AND building IS NULL
            AND way_area > 1*!pixel_width!::real*!pixel_height!::real
          ORDER BY COALESCE(layer,0), way_area DESC
        ) AS water_areas
    properties:
      cache-features: true
      minzoom: 0
  - id: ocean-lz
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way
          FROM simplified_water_polygons
        ) AS ocean_lz
    properties:
      maxzoom: 9
  - id: ocean
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way
          FROM water_polygons
        ) AS ocean
    properties:
      minzoom: 10
  - id: landcover-area-symbols
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way, surface,
            COALESCE(CASE WHEN landuse = 'forest' THEN 'wood' END, "natural") AS "natural",
            CASE WHEN "natural" = 'mud'
                THEN "natural"
                ELSE CASE WHEN ("natural" = 'wetland' AND NOT tags ? 'wetland')
                  THEN 'wetland'
                  ELSE CASE WHEN ("natural" = 'wetland')
                    THEN tags->'wetland'
                    END
                END
              END AS int_wetland,
            landuse,
            tags->'leaf_type' AS leaf_type,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels
          FROM planet_osm_polygon
          WHERE ("natural" IN ('mud', 'wetland', 'wood', 'beach', 'shoal', 'reef', 'scrub') OR landuse IN ('forest', 'salt_pond'))
            AND building IS NULL
            AND way_area > 1*!pixel_width!::real*!pixel_height!::real
          ORDER BY COALESCE(layer,0), way_area DESC
        ) AS landcover_area_symbols
    properties:
      cache-features: true
      minzoom: 9
  - id: icesheet-outlines
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            ice_edge
          FROM icesheet_outlines
        ) AS icesheet_outlines
    properties:
      minzoom: 5
  - id: marinas-area
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way
          FROM planet_osm_polygon
          WHERE leisure = 'marina'
        ) AS marinas_area
    properties:
      minzoom: 14
  - id: water-barriers-line
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            waterway
          FROM planet_osm_line
          WHERE waterway IN ('dam', 'weir', 'lock_gate')
        ) AS water_barriers_line
    properties:
      minzoom: 13
  - id: water-barriers-poly
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            waterway
          FROM planet_osm_polygon
          WHERE waterway IN ('dam', 'weir', 'lock_gate')
        ) AS water_barriers_poly
    properties:
      minzoom: 13
  - id: piers-poly
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way, man_made
          FROM planet_osm_polygon
          WHERE man_made IN ('pier', 'breakwater', 'groyne')
          ORDER BY CASE
              WHEN man_made = 'pier' THEN 3
              WHEN man_made = 'groyne' THEN 2
              WHEN man_made = 'breakwater' THEN 1
              ELSE 0
            END ASC
        ) AS piers_poly
    properties:
      minzoom: 12
  - id: piers-line
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way, man_made
          FROM planet_osm_line
          WHERE man_made IN ('pier', 'breakwater', 'groyne')
          ORDER BY CASE
              WHEN man_made = 'pier' THEN 3
              WHEN man_made = 'groyne' THEN 2
              WHEN man_made = 'breakwater' THEN 1
              ELSE 0
            END ASC
        ) AS piers_line
    properties:
      minzoom: 12
  - id: water-barriers-point
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way, waterway
          FROM planet_osm_point
          WHERE waterway IN ('dam', 'weir', 'lock_gate')
        ) AS water_barriers_points
    properties:
      minzoom: 17
  - id: bridge
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            man_made
          FROM planet_osm_polygon
          WHERE man_made = 'bridge'
        ) AS bridge
    properties:
      minzoom: 12
  - id: buildings
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            building,
            amenity,
            aeroway,
            aerialway,
            tags->'public_transport' as public_transport
          FROM planet_osm_polygon
          WHERE building IS NOT NULL
            AND building != 'no'
            AND way_area > 1*!pixel_width!::real*!pixel_height!::real
          ORDER BY COALESCE(layer,0), way_area DESC
        ) AS buildings
    properties:
      minzoom: 14
  - id: tunnels
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      # This query is quite large, having to deal with both roads, railways. To
      # allow for ways that are both railways and roads, a UNION ALL is present.
      table: |-
        (SELECT
            way,
            (CASE WHEN feature IN ('highway_motorway_link', 'highway_trunk_link', 'highway_primary_link', 'highway_secondary_link', 'highway_tertiary_link') THEN substr(feature, 0, length(feature)-4) ELSE feature END) AS feature,
            tracktype,
            int_surface,
            int_access,
            construction,
            service,
            link,
            preserved,
            layernotnull
          FROM ( -- subselect that contains both roads and rail
            SELECT
                way,
                'highway_' || (CASE WHEN highway = 'path' THEN carto_path_type(bicycle, horse) ELSE highway END) AS feature,
                tracktype,
                CASE WHEN surface IN ('unpaved', 'compacted', 'dirt', 'earth', 'fine_gravel', 'grass', 'grass_paver', 'gravel', 'ground',
                                      'mud', 'pebblestone', 'salt', 'sand', 'woodchips', 'clay', 'ice', 'snow') THEN 'unpaved'
                  WHEN surface IN ('paved', 'asphalt', 'cobblestone', 'cobblestone:flattened', 'sett', 'concrete', 'concrete:lanes',
                                      'concrete:plates', 'paving_stones', 'metal', 'wood', 'unhewn_cobblestone') THEN 'paved'
                END AS int_surface,
                carto_highway_int_access(highway, access, foot, bicycle, horse, tags->'motorcar', tags->'motor_vehicle', tags->'vehicle') AS int_access,
                construction,
                CASE
                  WHEN service IN ('parking_aisle', 'drive-through', 'driveway') THEN 'INT-minor'::text
                  ELSE 'INT-normal'::text
                END AS service,
                CASE
                  WHEN highway IN ('motorway_link', 'trunk_link', 'primary_link', 'secondary_link', 'tertiary_link') THEN 'yes'
                  ELSE 'no'
                END AS link,
                'no' as preserved,
                COALESCE(layer,0) AS layernotnull,
                z_order
              FROM planet_osm_line
              WHERE (tunnel = 'yes' OR tunnel = 'building_passage' OR covered = 'yes')
                AND highway IS NOT NULL -- end of road select
            UNION ALL
            SELECT
                way,
                'railway_' || (CASE
                                 WHEN (railway = 'rail' AND service IN ('spur', 'siding', 'yard')) THEN 'INT-spur-siding-yard'
                                 WHEN (railway = 'tram' AND service IN ('spur', 'siding', 'yard')) THEN 'tram-service'
                                 ELSE railway END) AS feature,
                tracktype,
                'null',
                NULL,
                construction,
                CASE WHEN service IN ('parking_aisle', 'drive-through', 'driveway') THEN 'INT-minor'::text ELSE 'INT-normal'::text END AS service,
                'no' AS link,
                (CASE
                  WHEN tags->'railway:preserved' = 'yes' THEN 'yes'
                  ELSE 'no'
                END) AS preserved,
                COALESCE(layer,0) AS layernotnull,
                z_order
              FROM planet_osm_line
              WHERE (tunnel = 'yes' OR tunnel = 'building_passage' OR covered = 'yes')
                AND (railway NOT IN ('platform') AND railway IS NOT NULL) -- end of rail select
            ) AS features
          ORDER BY
            layernotnull,
            z_order,
            CASE WHEN substring(feature for 8) = 'railway_' THEN 2 ELSE 1 END,
            CASE WHEN feature IN ('railway_INT-spur-siding-yard', 'railway_tram-service') THEN 0 ELSE 1 END,
            CASE int_access WHEN 'no' THEN 0 WHEN 'restricted' THEN 1 ELSE 2 END,
            CASE WHEN int_surface IN ('unpaved') THEN 0 ELSE 1 END
        ) AS tunnels
    properties:
      cache-features: true
      group-by: layernotnull
      minzoom: 10
  - id: landuse-overlay
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            landuse,
            military,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels
          FROM planet_osm_polygon
          WHERE (landuse = 'military'
            OR military = 'danger_area')
            AND building IS NULL
        ) AS landuse_overlay
    properties:
      minzoom: 8
  - id: barriers
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way, COALESCE(historic, barrier) AS feature
          FROM
            (SELECT way,
              ('barrier_' || (CASE WHEN barrier IN ('chain', 'city_wall', 'ditch', 'fence', 'guard_rail',
                    'handrail', 'hedge', 'retaining_wall', 'wall') THEN barrier END)) AS barrier,
              ('historic_' || (CASE WHEN historic = 'citywalls' THEN historic END)) AS historic
              FROM
                (SELECT
                    way,
                    historic,
                    barrier,
                    waterway
                  FROM planet_osm_polygon
                  WHERE way && !bbox!
                UNION ALL
                SELECT
                    way,
                    historic,
                    barrier,
                    waterway
                  FROM planet_osm_line
                  WHERE way && !bbox!
                ) _
              WHERE barrier IN ('chain', 'city_wall', 'ditch', 'fence', 'guard_rail',
                  'handrail', 'hedge', 'retaining_wall', 'wall', 'jersey_barrier')
              OR historic = 'citywalls'
              AND (waterway IS NULL OR waterway NOT IN ('river', 'canal', 'stream', 'drain', 'ditch'))
          ) AS features
        ) AS line_barriers
    properties:
      minzoom: 15
  - id: cliffs
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way, "natural", man_made
          FROM planet_osm_line
          WHERE "natural" IN ('arete', 'cliff', 'ridge') OR man_made = 'embankment'
        ) AS cliffs
    properties:
      cache-features: true
      minzoom: 13
  - id: ferry-routes
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way
          FROM planet_osm_line
          WHERE route = 'ferry'
            AND osm_id > 0
        ) AS ferry_routes
    properties:
      minzoom: 8
  - id: turning-circle-casing
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: &turning-circle_sql |-
        (SELECT DISTINCT ON (p.way)
            p.way AS way,
            p.highway AS type,
            l.highway AS int_tc_type,
            CASE WHEN l.service IN ('parking_aisle', 'drive-through', 'driveway')
              THEN 'INT-minor'::text
              ELSE 'INT-normal'::text
            END AS int_tc_service
          FROM planet_osm_point p
            JOIN planet_osm_line l
              ON ST_DWithin(p.way, l.way, 0.1) -- Assumes Mercator
            JOIN (VALUES
                ('trunk', 1),
                ('primary', 2),
                ('secondary', 3),
                ('tertiary', 4),
                ('unclassified', 5),
                ('residential', 6),
                ('living_street', 7),
                ('service', 8),
                ('track', 9)
              ) AS v (highway, prio)
              ON v.highway = l.highway
          WHERE p.highway IN (
            'turning_circle',
            'turning_loop',
            'mini_roundabout')
            AND l.way && !bbox!
            AND p.way && !bbox! -- Both conditions are necessary for good index usage, even with the DWithin above
          ORDER BY p.way, v.prio
        ) AS turning_circle_sql
    properties:
      minzoom: 15
  - id: highway-area-casing
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            COALESCE((
              'highway_' || (CASE WHEN highway IN ('pedestrian', 'footway', 'service', 'platform') THEN highway END)),
              ('railway_' || (CASE WHEN (railway IN ('platform')
                              AND (tags->'location' NOT IN ('underground') OR (tags->'location') IS NULL)
                              AND (tunnel NOT IN ('yes', 'building_passage') OR tunnel IS NULL))
                              THEN railway END))
            ) AS feature
          FROM planet_osm_polygon
          WHERE highway IN ('pedestrian', 'footway', 'service', 'platform')
            OR (railway IN ('platform')
                AND (tags->'location' NOT IN ('underground') OR (tags->'location') IS NULL)
                AND (tunnel NOT IN ('yes', 'building_passage') OR tunnel IS NULL))
          ORDER BY COALESCE(layer,0), way_area DESC
        ) AS highway_area_casing
    properties:
      minzoom: 14
  - id: roads-casing
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      # This is one of the most complex layers, so it bears explaining in some detail
      # It is necessary to
      # - Have roads and railways in the same layer to get ordering right
      # - Return two linestrings for ways which are both a road and railway
      table: &roads_sql |-
        (SELECT
            way,
            (CASE WHEN feature IN ('highway_motorway_link', 'highway_trunk_link', 'highway_primary_link', 'highway_secondary_link', 'highway_tertiary_link') THEN substr(feature, 0, length(feature)-4) ELSE feature END) AS feature,
            tracktype,
            int_surface,
            int_access,
            construction,
            service,
            link,
            preserved,
            layernotnull
          FROM ( -- subselect that contains both roads and rail/aero
            SELECT
                way,
                'highway_' || (CASE WHEN highway = 'path' THEN carto_path_type(bicycle, horse) ELSE highway END) AS feature,
                tracktype,
                CASE WHEN surface IN ('unpaved', 'compacted', 'dirt', 'earth', 'fine_gravel', 'grass', 'grass_paver', 'gravel', 'ground',
                                      'mud', 'pebblestone', 'salt', 'sand', 'woodchips', 'clay', 'ice', 'snow') THEN 'unpaved'
                  WHEN surface IN ('paved', 'asphalt', 'cobblestone', 'cobblestone:flattened', 'sett', 'concrete', 'concrete:lanes',
                                      'concrete:plates', 'paving_stones', 'metal', 'wood', 'unhewn_cobblestone') THEN 'paved'
                END AS int_surface,
                carto_highway_int_access(highway, access, foot, bicycle, horse, tags->'motorcar', tags->'motor_vehicle', tags->'vehicle') AS int_access,
                construction,
                CASE
                  WHEN service IN ('parking_aisle', 'drive-through', 'driveway') OR leisure IN ('slipway') THEN 'INT-minor'::text
                  ELSE 'INT-normal'::text
                END AS service,
                CASE
                  WHEN highway IN ('motorway_link', 'trunk_link', 'primary_link', 'secondary_link', 'tertiary_link') THEN 'yes'
                  ELSE 'no'
                END AS link,
                'no' AS preserved,
                COALESCE(layer,0) AS layernotnull,
                osm_id,
                z_order
              FROM planet_osm_line
              WHERE (tunnel IS NULL OR NOT tunnel IN ('yes', 'building_passage'))
                AND (covered IS NULL OR NOT covered = 'yes')
                AND (bridge IS NULL OR NOT bridge IN ('yes', 'boardwalk', 'cantilever', 'covered', 'low_water_crossing', 'movable', 'trestle', 'viaduct'))
                AND highway IS NOT NULL -- end of road select
            UNION ALL
            SELECT
                way,
                'railway_' || (CASE
                                 WHEN (railway = 'rail' AND service IN ('spur', 'siding', 'yard')) THEN 'INT-spur-siding-yard'
                                 WHEN (railway = 'tram' AND service IN ('spur', 'siding', 'yard')) THEN 'tram-service'
                                 ELSE railway END) AS feature,
                tracktype,
                CASE WHEN surface IN ('unpaved', 'compacted', 'dirt', 'earth', 'fine_gravel', 'grass', 'grass_paver', 'gravel', 'ground',
                                      'mud', 'pebblestone', 'salt', 'sand', 'woodchips', 'clay', 'ice', 'snow') THEN 'unpaved'
                  WHEN surface IN ('paved', 'asphalt', 'cobblestone', 'cobblestone:flattened', 'sett', 'concrete', 'concrete:lanes',
                                      'concrete:plates', 'paving_stones', 'metal', 'wood', 'unhewn_cobblestone') THEN 'paved'
                  ELSE NULL
                END AS int_surface,
                NULL,
                construction,
                CASE WHEN service IN ('parking_aisle', 'drive-through', 'driveway') OR leisure IN ('slipway') THEN 'INT-minor'::text ELSE 'INT-normal'::text END AS service,
                'no' AS link,
                (CASE
                  WHEN tags->'railway:preserved' = 'yes' THEN 'yes'
                  ELSE 'no'
                END) AS preserved,
                COALESCE(layer,0) AS layernotnull,
                osm_id,
                z_order
              FROM planet_osm_line
              WHERE (tunnel IS NULL OR NOT tunnel IN ('yes', 'building_passage'))
                AND (covered IS NULL OR NOT covered = 'yes' OR railway IN ('platform'))
                AND (bridge IS NULL OR NOT bridge IN ('yes', 'boardwalk', 'cantilever', 'covered', 'low_water_crossing', 'movable', 'trestle', 'viaduct'))
                AND (railway NOT IN ('platform') OR (tags->'location' NOT IN ('underground')) OR (tags->'location') IS NULL)
                AND railway IS NOT NULL -- end of rail select
            ) AS features
          ORDER BY
            layernotnull,
            z_order,
            CASE WHEN substring(feature for 8) = 'railway_' THEN 2 ELSE 1 END,
            CASE WHEN feature IN ('railway_INT-spur-siding-yard', 'railway_tram-service') THEN 0 ELSE 1 END,
            CASE int_access WHEN 'no' THEN 0 WHEN 'restricted' THEN 1 ELSE 2 END,
            CASE WHEN int_surface IN ('unpaved') THEN 0 ELSE 1 END,
            osm_id
        ) AS roads_sql
    properties:
      cache-features: true
      minzoom: 10
  - id: highway-area-fill
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            COALESCE(
              ('highway_' || (CASE WHEN highway IN ('pedestrian', 'footway', 'service', 'living_street',
                                                    'platform', 'services') THEN highway END)),
              ('railway_' || (CASE WHEN (railway IN ('platform')
                              AND (tags->'location' NOT IN ('underground') OR (tags->'location') IS NULL)
                              AND (tunnel NOT IN ('yes', 'building_passage') OR tunnel IS NULL))
                              THEN railway END)),
              (('aeroway_' || CASE WHEN aeroway IN ('runway', 'taxiway', 'helipad') THEN aeroway ELSE NULL END))
            ) AS feature,
            CASE WHEN surface IN ('unpaved', 'compacted', 'dirt', 'earth', 'fine_gravel', 'grass', 'grass_paver', 'gravel', 'ground',
                                  'mud', 'pebblestone', 'salt', 'sand', 'woodchips', 'clay', 'ice', 'snow') THEN 'unpaved'
              WHEN surface IN ('paved', 'asphalt', 'cobblestone', 'cobblestone:flattened', 'sett', 'concrete', 'concrete:lanes',
                                  'concrete:plates', 'paving_stones', 'metal', 'wood', 'unhewn_cobblestone') THEN 'paved'
              ELSE NULL
            END AS int_surface
          FROM planet_osm_polygon
          WHERE highway IN ('pedestrian', 'footway', 'service', 'living_street', 'platform', 'services')
            OR (railway IN ('platform')
                AND (tags->'location' NOT IN ('underground') OR (tags->'location') IS NULL)
                AND (tunnel NOT IN ('yes', 'building_passage') OR tunnel IS NULL))
            OR aeroway IN ('runway', 'taxiway', 'helipad')
          ORDER BY COALESCE(layer,0), way_area desc
        ) AS highway_area_fill
    properties:
      minzoom: 14
  - id: roads-fill
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: *roads_sql
    properties:
      cache-features: true
      minzoom: 10
  - id: turning-circle-fill
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: *turning-circle_sql
    properties:
      minzoom: 15
  - id: aerialways
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            aerialway,
            man_made,
            tags->'substance' AS substance
          FROM planet_osm_line
          WHERE aerialway IS NOT NULL
            OR (man_made = 'pipeline'
                AND tags-> 'location' IN ('overground', 'overhead', 'surface', 'outdoor')
                OR bridge IN ('yes', 'aqueduct', 'cantilever', 'covered', 'trestle', 'viaduct'))
            OR (man_made = 'goods_conveyor'
                AND (tags->'location' NOT IN ('underground') OR (tags->'location') IS NULL)
                AND (tunnel NOT IN ('yes') OR tunnel IS NULL))
          ORDER BY
            CASE
              WHEN man_made IN ('goods_conveyor', 'pipeline') THEN 1
              WHEN tags-> 'location' = 'overhead' THEN 2
              WHEN bridge IS NOT NULL THEN 3
              WHEN aerialway IS NOT NULL THEN 4
            END
        ) AS aerialways
    properties:
      minzoom: 12
  - id: roads-low-zoom
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            COALESCE(
              ('highway_' || (CASE WHEN highway IN ('motorway_link', 'trunk_link', 'primary_link', 'secondary_link', 'tertiary_link')
                                     THEN substr(highway, 0, length(highway)-4) ELSE highway end)),
              ('railway_' || railway)
            ) AS feature,
            CASE WHEN tunnel = 'yes' OR tunnel = 'building_passage' OR covered = 'yes' THEN 'yes' ELSE 'no' END AS int_tunnel,
            CASE WHEN highway IN ('motorway_link', 'trunk_link', 'primary_link', 'secondary_link', 'tertiary_link') THEN 'yes' ELSE 'no' END AS link,
            CASE WHEN surface IN ('unpaved', 'compacted', 'dirt', 'earth', 'fine_gravel', 'grass', 'grass_paver', 'gravel', 'ground',
                                  'mud', 'pebblestone', 'salt', 'sand', 'woodchips', 'clay', 'ice', 'snow') THEN 'unpaved'
              WHEN surface IN ('paved', 'asphalt', 'cobblestone', 'cobblestone:flattened', 'sett', 'concrete', 'concrete:lanes',
                                  'concrete:plates', 'paving_stones', 'metal', 'wood', 'unhewn_cobblestone') THEN 'paved'
            END AS int_surface
          FROM planet_osm_roads
          WHERE highway IS NOT NULL
            OR (railway IN ('rail', 'light_rail', 'funicular', 'narrow_gauge')
              AND (tags->'railway:preserved' IS NULL OR tags->'railway:preserved' != 'yes')
              AND (service IS NULL OR service NOT IN ('spur', 'siding', 'yard')))
          ORDER BY
            z_order
        ) AS roads_low_zoom
    properties:
      cache-features: true
      minzoom: 6
      maxzoom: 9
  - id: waterway-bridges
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            waterway,
            CASE WHEN tags->'intermittent' IN ('yes')
              OR tags->'seasonal' IN ('yes', 'spring', 'summer', 'autumn', 'winter', 'wet_season', 'dry_season')
              THEN 'yes' ELSE 'no' END AS int_intermittent,
            CASE WHEN tunnel IN ('yes', 'culvert') 
              OR waterway = 'canal' AND tunnel = 'flooded'
              THEN 'yes' ELSE 'no' END AS int_tunnel,
            'yes' AS bridge
          FROM planet_osm_line
          WHERE waterway IN ('river', 'canal', 'stream', 'drain', 'ditch')
            AND bridge IN ('yes', 'aqueduct')
          ORDER BY COALESCE(layer,0)
        ) AS waterway_bridges
    properties:
      minzoom: 12
  - id: bridges
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            (CASE WHEN feature IN ('highway_motorway_link', 'highway_trunk_link', 'highway_primary_link', 'highway_secondary_link', 'highway_tertiary_link') THEN substr(feature, 0, length(feature)-4) ELSE feature END) AS feature,
            tracktype,
            int_surface,
            int_access,
            construction,
            service,
            link,
            preserved,
            layernotnull
          FROM ( -- subselect that contains both roads and rail/aero
            SELECT
                way,
                'highway_' || (CASE WHEN highway = 'path' THEN carto_path_type(bicycle, horse) ELSE highway END) AS feature,
                tracktype,
                CASE WHEN surface IN ('unpaved', 'compacted', 'dirt', 'earth', 'fine_gravel', 'grass', 'grass_paver', 'gravel', 'ground',
                                      'mud', 'pebblestone', 'salt', 'sand', 'woodchips', 'clay', 'ice', 'snow') THEN 'unpaved'
                  WHEN surface IN ('paved', 'asphalt', 'cobblestone', 'cobblestone:flattened', 'sett', 'concrete', 'concrete:lanes',
                                      'concrete:plates', 'paving_stones', 'metal', 'wood', 'unhewn_cobblestone') THEN 'paved'
                END AS int_surface,
                carto_highway_int_access(highway, access, foot, bicycle, horse, tags->'motorcar', tags->'motor_vehicle', tags->'vehicle') AS int_access,
                construction,
                CASE
                  WHEN service IN ('parking_aisle', 'drive-through', 'driveway') THEN 'INT-minor'::text
                  ELSE 'INT-normal'::text
                END AS service,
                CASE
                  WHEN highway IN ('motorway_link', 'trunk_link', 'primary_link', 'secondary_link', 'tertiary_link') THEN 'yes'
                  ELSE 'no'
                END AS link,
                'no' AS preserved,
                COALESCE(layer,0) AS layernotnull,
                z_order
              FROM planet_osm_line
              WHERE bridge IN ('yes', 'boardwalk', 'cantilever', 'covered', 'low_water_crossing', 'movable', 'trestle', 'viaduct')
                AND highway IS NOT NULL -- end of road select
            UNION ALL
            SELECT
                way,
                'railway_' || (CASE
                                 WHEN (railway = 'rail' AND service IN ('spur', 'siding', 'yard')) THEN 'INT-spur-siding-yard'
                                 WHEN (railway = 'tram' AND service IN ('spur', 'siding', 'yard')) THEN 'tram-service'
                                 ELSE railway END) AS feature,
                tracktype,
                'null',
                NULL,
                construction,
                CASE WHEN service IN ('parking_aisle', 'drive-through', 'driveway') THEN 'INT-minor'::text ELSE 'INT-normal'::text END AS service,
                'no' AS link,
                (CASE
                  WHEN tags->'railway:preserved' = 'yes' THEN 'yes'
                  ELSE 'no'
                END) AS preserved,
                COALESCE(layer,0) AS layernotnull,
                z_order
              FROM planet_osm_line
              WHERE bridge IN ('yes', 'boardwalk', 'cantilever', 'covered', 'low_water_crossing', 'movable', 'trestle', 'viaduct')
                AND railway IS NOT NULL -- end of rail select
            ) AS features
          ORDER BY
            layernotnull,
            z_order,
            CASE WHEN substring(feature for 8) = 'railway_' THEN 2 ELSE 1 END,
            CASE WHEN feature IN ('railway_INT-spur-siding-yard', 'railway_tram-service') THEN 0 ELSE 1 END,
            CASE int_access WHEN 'no' THEN 0 WHEN 'restricted' THEN 1 ELSE 2 END,
            CASE WHEN int_surface IN ('unpaved') THEN 0 ELSE 1 END
        ) AS bridges
    properties:
      cache-features: true
      group-by: layernotnull
      minzoom: 10
  - id: guideways
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way
          FROM planet_osm_line
          WHERE highway = 'bus_guideway'
        ) AS guideways
    properties:
      minzoom: 11
  - id: roller-coaster-gap-fill
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            CASE WHEN (tunnel = 'yes' OR tunnel = 'building_passage' OR covered = 'yes' OR tags->'indoor' = 'yes') THEN 'yes' ELSE 'no' END AS tunnel,
            CASE WHEN (bridge = 'yes' OR bridge = 'covered' OR bridge = 'viaduct') THEN 'yes' ELSE 'no' END AS bridge
          FROM planet_osm_line
          WHERE tags @> 'roller_coaster=>track' AND railway IS NULL
        ) AS roller_coaster
    properties:
      minzoom: 15
  - id: roller-coaster
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            COALESCE(layer,0) AS layernotnull,
            CASE WHEN (tunnel = 'yes' OR tunnel = 'building_passage' OR covered = 'yes' OR tags->'indoor' = 'yes') THEN 'yes' ELSE 'no' END AS tunnel,
            CASE WHEN (bridge = 'yes' OR bridge = 'covered' OR bridge = 'viaduct') THEN 'yes' ELSE 'no' END AS bridge
          FROM planet_osm_line
          WHERE tags @> 'roller_coaster=>track' AND railway IS NULL
          ORDER BY
            layernotnull -- put bottom layered track first
        ) AS roller_coaster
    properties:
      group-by: layernotnull
      minzoom: 15
  - id: entrances
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            tags->'entrance' AS entrance,
            access
          FROM planet_osm_point
          WHERE (tags->'entrance') IS NOT NULL AND
            (tags->'indoor' = 'no'
            OR (tags->'indoor') IS NULL))
        AS entrances
    properties:
      minzoom: 18
  - id: aeroways
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            aeroway,
            bridge IN ('yes', 'boardwalk', 'cantilever', 'covered', 'low_water_crossing', 'movable', 'trestle', 'viaduct') AS bridge,
            CASE WHEN surface IN ('unpaved', 'compacted', 'dirt', 'earth', 'fine_gravel', 'grass', 'grass_paver', 'gravel', 'ground',
                                  'mud', 'pebblestone', 'salt', 'sand', 'woodchips', 'clay', 'ice', 'snow') THEN 'unpaved'
              WHEN surface IN ('paved', 'asphalt', 'cobblestone', 'cobblestone:flattened', 'sett', 'concrete', 'concrete:lanes',
                                  'concrete:plates', 'paving_stones', 'metal', 'wood', 'unhewn_cobblestone') THEN 'paved'
              ELSE NULL
            END AS int_surface
          FROM planet_osm_line
          WHERE aeroway IN ('runway', 'taxiway')
          ORDER BY
            bridge NULLS FIRST,
            CASE WHEN aeroway = 'runway' THEN 1 ELSE 0 END,
            CASE WHEN surface IN ('unpaved', 'compacted', 'dirt', 'earth', 'fine_gravel', 'grass', 'grass_paver', 'gravel', 'ground',
                                  'mud', 'pebblestone', 'salt', 'sand', 'woodchips', 'clay', 'ice', 'snow') THEN 0 ELSE 1 END
        ) AS aeroways
    properties:
      cache-features: true
      minzoom: 11
  - id: golf-line
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way, tags->'golf' AS golf
          FROM planet_osm_line
          WHERE tags @> 'golf=>hole'
        ) AS golf_line
    properties:
      minzoom: 16
  - id: necountries
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way
          FROM ne_110m_admin_0_boundary_lines_land
        ) AS necountries
    properties:
      minzoom: 1
      maxzoom: 3
  - id: admin-low-zoom
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            admin_level
          FROM planet_osm_roads
          WHERE boundary = 'administrative'
            AND admin_level IN ('0', '1', '2', '3', '4')
            AND osm_id < 0
          ORDER BY admin_level DESC
        ) AS admin_low_zoom
    properties:
      minzoom: 4
      maxzoom: 7
  - id: admin-mid-zoom
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            admin_level
          FROM planet_osm_roads
          WHERE boundary = 'administrative'
            AND admin_level IN ('0', '1', '2', '3', '4', '5', '6', '7', '8')
            AND osm_id < 0
          ORDER BY admin_level DESC
        ) AS admin_mid_zoom
    properties:
      minzoom: 8
      maxzoom: 12
  - id: admin-high-zoom
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            admin_level
          FROM planet_osm_roads
          WHERE boundary = 'administrative'
            AND admin_level IN ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10')
            AND osm_id < 0
          ORDER BY admin_level::integer DESC -- With 10 as a valid value, we need to do a numeric ordering, not a text ordering
        ) AS admin_high_zoom
    properties:
      minzoom: 13
  - id: power-minorline
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way
          FROM planet_osm_line
          WHERE power = 'minor_line'
        ) AS power_minorline
    properties:
      minzoom: 16
  - id: power-line
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way
          FROM planet_osm_line
          WHERE power = 'line'
        ) AS power_line
    properties:
      minzoom: 14
  - id: tourism-boundary
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels,
            tourism
          FROM planet_osm_polygon
          WHERE tourism = 'theme_park'
            OR tourism = 'zoo'
        ) AS tourism_boundary
    properties:
      minzoom: 10
  - id: protected-areas
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            boundary,
            tags->'protect_class' AS protect_class,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels
          FROM planet_osm_polygon
          WHERE (boundary IN ('aboriginal_lands', 'national_park')
                 OR leisure = 'nature_reserve'
                 OR (boundary = 'protected_area' AND tags->'protect_class' IN ('1','1a','1b','2','3','4','5','6')))
            AND building IS NULL
            AND way_area > 1*!pixel_width!::real*!pixel_height!::real
        ) AS protected_areas
    properties:
      cache-features: true
      minzoom: 8
  - id: trees
    geometry: polygon
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way, "natural"
          FROM planet_osm_point
          WHERE "natural" = 'tree'
        UNION ALL
        SELECT
            way, "natural"
          FROM planet_osm_line
          WHERE "natural" = 'tree_row'
        ) AS trees
    properties:
      cache-features: true
      minzoom: 16
  - id: country-names
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      # By splitting MPs into polygons, finding the largest in real area, and
      # finding a label point in 4326 we get better labeling points. 4326 does
      # distort the placement, but it does so in a way that results in better
      # results for most countries.
      table: |-
        (SELECT
            (SELECT ST_Transform(ST_PointOnSurface(geom),3857)
              FROM ST_Dump(ST_Transform(way,4326))
              ORDER BY ST_Area(geom::geography) DESC
              LIMIT 1) AS way,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels,
            name
          FROM planet_osm_polygon
          WHERE way && !bbox!
            AND name IS NOT NULL
            AND boundary = 'administrative'
            AND admin_level = '2'
            AND way_area > 100*POW(!scale_denominator!*0.001*0.28,2)
            AND way_area < 4000000*POW(!scale_denominator!*0.001*0.28,2)
            AND osm_id < 0
          ORDER BY way_area DESC
        ) AS country_names
    properties:
      minzoom: 2
  - id: capital-names
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            name,
            CASE
              WHEN (tags->'population' ~ '^[0-9]{1,8}$') THEN (tags->'population')::INTEGER ELSE 0
            END as population,
            round(ascii(md5(osm_id::text)) / 55) AS dir -- base direction factor on geometry to be consistent across metatiles
          FROM planet_osm_point
          WHERE place IN ('city', 'town', 'village', 'hamlet')
            AND name IS NOT NULL
            AND tags @> 'capital=>yes'
          ORDER BY population DESC
        ) AS capital_names
    properties:
      minzoom: 3
      maxzoom: 15
  - id: state-names
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            ST_PointOnSurface(way) AS way,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels,
            name,
            admin_level,
            ref
          FROM planet_osm_polygon
          WHERE ST_PointOnSurface(way) && !bbox!
            AND name IS NOT NULL
            AND boundary = 'administrative'
            AND admin_level = '4'
            AND way_area > 3000*POW(!scale_denominator!*0.001*0.28,2)
            AND way_area < 4000000*POW(!scale_denominator!*0.001*0.28,2)
            AND osm_id < 0
          ORDER BY way_area DESC
        ) AS state_names
    properties:
      minzoom: 4
  - id: placenames-medium
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            name,
            score,
            CASE
              WHEN (place = 'city') THEN 1
              ELSE 2
            END as category,
            round(ascii(md5(osm_id::text)) / 55) AS dir -- base direction factor on geometry to be consistent across metatiles
          FROM
            (SELECT
                osm_id,
                way,
                place,
                name,
                (
                  (CASE
                    WHEN (tags->'population' ~ '^[0-9]{1,8}$') THEN (tags->'population')::INTEGER
                    WHEN (place = 'city') THEN 100000
                    WHEN (place = 'town') THEN 1000
                    ELSE 1
                  END)
                  *
                  (CASE
                    WHEN (tags @> 'capital=>4') THEN 2
                    ELSE 1
                  END)
                ) AS score
              FROM planet_osm_point
              WHERE place IN ('city', 'town')
                AND name IS NOT NULL
                AND NOT (tags @> 'capital=>yes')
            ) as p
          ORDER BY score DESC, length(name) DESC, name
        ) AS placenames_medium
    properties:
      cache-features: true
      minzoom: 4
      maxzoom: 15
  - id: placenames-small
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            place,
            name
          FROM planet_osm_point
          WHERE place IN ('village', 'hamlet')
             AND name IS NOT NULL
             AND NOT tags @> 'capital=>yes'
             OR (place IN ('suburb', 'quarter', 'neighbourhood', 'isolated_dwelling', 'farm')
             ) AND name IS NOT NULL
          ORDER BY CASE
              WHEN place = 'suburb' THEN 7
              WHEN place = 'village' THEN 6
              WHEN place = 'hamlet' THEN 5
              WHEN place = 'quarter' THEN 4
              WHEN place = 'neighbourhood' THEN 3
              WHEN place = 'isolated_dwelling' THEN 2
              WHEN place = 'farm' THEN 1
            END DESC,
            length(name) DESC,
            name
        ) AS placenames_small
    properties:
      cache-features: true
      minzoom: 12
  - id: stations
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            name,
            ref,
            railway,
            aerialway,
            station
          FROM
          (SELECT
              ST_PointOnSurface(way) AS way,
              name,
              ref,
              railway,
              aerialway,
              tags->'station' AS station,
              way_area
            FROM planet_osm_polygon
            WHERE way && !bbox! -- Not ST_PointOnSurface(way) because name might be NULL
              AND way_area < 768000*POW(!scale_denominator!*0.001*0.28,2)
          UNION ALL
          SELECT
              way,
              name,
              ref,
              railway,
              aerialway,
              tags->'station' AS station,
              NULL as way_area
            FROM planet_osm_point
            WHERE way && !bbox!
            ) _
          WHERE railway IN ('station', 'halt', 'tram_stop')
            OR railway = 'subway_entrance' AND way_area IS NULL
            OR aerialway = 'station'
          ORDER BY
            CASE railway
              WHEN 'station' THEN 1
              WHEN 'subway_entrance' THEN 3
              ELSE 2
            END,
            way_area DESC NULLS LAST
        ) AS stations
    properties:
      cache-features: true
      minzoom: 12
  - id: junctions
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            highway,
            junction,
            ref,
            name,
            NULL AS way_pixels
          FROM planet_osm_point
          WHERE way && !bbox!
            AND (highway = 'motorway_junction' OR highway = 'traffic_signals' OR junction = 'yes')
        UNION ALL
          SELECT
            ST_PointOnSurface(way) AS way,
            highway,
            junction,
            ref,
            name,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels
          FROM planet_osm_polygon
          WHERE way && !bbox! -- Not ST_PointOnSurface(way) because name might be NULL
            AND junction = 'yes'
            AND way_area < 768000*POW(!scale_denominator!*0.001*0.28,2)
          ORDER BY way_pixels DESC NULLS LAST
        ) AS junctions
    properties:
      minzoom: 11
  - id: bridge-text
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            ST_PointOnSurface(way) AS way,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels,
            man_made,
            name
          FROM planet_osm_polygon
          WHERE ST_PointOnSurface(way) && !bbox!
            AND name IS NOT NULL
            AND man_made = 'bridge'
            AND way_area > 125*POW(!scale_denominator!*0.001*0.28,2)
            AND way_area < 768000*POW(!scale_denominator!*0.001*0.28,2)
          ORDER BY way_area DESC
        ) AS bridge_text
    properties:
      minzoom: 11
  - id: county-names
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            ST_PointOnSurface(way) AS way,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels,
            name,
            admin_level
          FROM planet_osm_polygon
          WHERE ST_PointOnSurface(way) && !bbox!
            AND name IS NOT NULL
            AND boundary = 'administrative'
            AND admin_level IN ('5', '6')
            AND way_area > 12000*POW(!scale_denominator!*0.001*0.28,2)
            AND way_area < 196000*POW(!scale_denominator!*0.001*0.28,2)
            AND osm_id < 0
          ORDER BY
            admin_level,
            way_area DESC
        ) AS county_names
    properties:
      minzoom: 8
  - id: amenity-points
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: &amenity_points_sql |-
        (SELECT
            *
          FROM
          (SELECT -- This subselect allows filtering on the feature column
              way,
              CASE
                WHEN amenity = 'parcel_locker'
                THEN CONCAT_WS(E'\n', COALESCE(tags->'brand', tags->'operator', name), ref)
                ELSE
                CONCAT(
                  name,
                  E'\n' || CONCAT( -- by doing this with a || if both the ele and height branches are null, this entire expression is null and only name is used
                    CASE
                      WHEN (tags ? 'ele') AND tags->'ele' ~ '^-?\d{1,4}(\.\d+)?$'
                        AND ("natural" IN ('peak', 'volcano', 'saddle')
                          OR (tourism = 'information' AND tags->'information' = 'guidepost')
                          OR tourism IN ('alpine_hut', 'wilderness_hut')
                          OR (amenity = 'shelter' AND tags->'shelter_type' NOT IN ('public_transport', 'picnic_shelter'))
                          OR tags->'mountain_pass' = 'yes')
                      THEN CONCAT(REPLACE(ROUND((tags->'ele')::NUMERIC)::TEXT, '-', U&'\2212'), U&'\00A0', 'm') END,
                    CASE
                      WHEN (tags ? 'height') AND tags->'height' ~ '^\d{1,3}(\.\d+)?$'
                        AND waterway = 'waterfall'
                      THEN CONCAT(ROUND((tags->'height')::NUMERIC)::TEXT, U&'\00A0', 'm') END
                    )
                  )
                END AS name,
              tags->'parking' as "parking",
              COALESCE(
                'aeroway_' || CASE WHEN aeroway IN ('gate', 'apron', 'helipad', 'aerodrome') THEN aeroway END,
                'tourism_' || CASE WHEN tourism IN ('alpine_hut', 'apartment', 'artwork', 'camp_site', 'caravan_site', 'chalet', 'gallery', 'guest_house',
                                                    'hostel', 'hotel', 'motel', 'museum', 'picnic_site', 'theme_park', 'wilderness_hut',
                                                    'zoo') THEN tourism END,
                'amenity_' || CASE WHEN amenity IN ('arts_centre', 'atm', 'bank', 'bar', 'bbq', 'bicycle_rental',
                                                    'bicycle_repair_station','biergarten', 'boat_rental', 'bureau_de_change', 'bus_station', 'cafe',
                                                    'car_rental', 'car_wash', 'casino', 'charging_station', 'childcare', 'cinema', 'clinic', 'college',
                                                    'community_centre', 'courthouse', 'dentist', 'doctors', 'drinking_water', 'driving_school',
                                                    'fast_food', 'ferry_terminal', 'fire_station', 'food_court', 'fountain', 'fuel', 'grave_yard',
                                                    'hospital', 'hunting_stand', 'ice_cream', 'internet_cafe', 'kindergarten', 'library', 'marketplace',
                                                    'nightclub', 'nursing_home', 'pharmacy', 'place_of_worship', 'police', 'post_box',
                                                    'post_office', 'prison', 'pub', 'public_bath', 'public_bookcase', 'recycling', 'restaurant', 'school',
                                                    'shelter', 'shower', 'social_facility', 'taxi', 'telephone', 'theatre', 'toilets', 'townhall',
                                                    'university', 'vehicle_inspection', 'veterinary') THEN amenity END,
                'amenity_' || CASE WHEN amenity IN ('waste_disposal') AND way_area IS NOT NULL THEN amenity END, -- Waste disposal points are rendered in the low priority layer
                'amenity_' || CASE WHEN amenity IN ('vending_machine') AND tags->'vending' IN ('excrement_bags', 'parking_tickets', 'public_transport_tickets') THEN amenity END,
                'amenity_' || CASE WHEN amenity IN ('parcel_locker') THEN amenity END,
                'diplomatic_'|| CASE WHEN tags->'office' IN ('diplomatic') AND tags->'diplomatic' IN ('embassy', 'consulate') THEN tags->'diplomatic' ELSE NULL END,
                'advertising_' || CASE WHEN tags->'advertising' in ('column') THEN tags->'advertising' END,
                'emergency_' || CASE WHEN tags->'emergency' IN ('phone') AND way_area IS NULL THEN tags->'emergency' END,
                'shop' || CASE WHEN shop IN ('yes', 'no', 'vacant', 'closed', 'disused', 'empty') OR shop IS NULL THEN NULL ELSE '' END,
                'leisure_' || CASE WHEN leisure IN ('amusement_arcade', 'beach_resort', 'bird_hide', 'bowling_alley', 'dance', 'dog_park', 'firepit', 'fishing',
                                                    'fitness_centre', 'fitness_station', 'garden', 'golf_course', 'ice_rink', 'marina', 'miniature_golf',
                                                    'outdoor_seating', 'park', 'picnic_table', 'pitch', 'playground',
                                                    'sauna', 'slipway', 'sports_centre', 'stadium', 'swimming_area', 'swimming_pool', 'track', 'water_park') THEN leisure END,
                'power_' || CASE WHEN power IN ('plant', 'generator', 'substation') THEN power END,
                'man_made_' || CASE WHEN (man_made IN ('chimney', 'communications_tower', 'crane', 'lighthouse', 'mast', 'obelisk', 'silo', 'storage_tank',
                                                       'telescope', 'tower', 'wastewater_plant', 'water_tower', 'water_works', 'windmill', 'works')
                                            AND (tags->'location' NOT IN ('roof', 'rooftop') OR NOT (tags ? 'location'))) THEN man_made END,
                'landuse_' || CASE WHEN landuse IN ('reservoir', 'basin', 'recreation_ground', 'village_green', 'quarry', 'vineyard', 'orchard', 'cemetery',
                                                    'residential', 'garages', 'meadow', 'grass', 'allotments', 'forest', 'farmyard', 'farmland',
                                                    'greenhouse_horticulture', 'retail', 'industrial', 'railway', 'commercial', 'brownfield', 'landfill',
                                                    'construction', 'salt_pond', 'military', 'plant_nursery') THEN landuse END,
                'natural_' || CASE WHEN "natural" IN ('peak', 'volcano', 'saddle', 'cave_entrance') AND way_area IS NULL THEN "natural" END,
                'natural_' || CASE WHEN "natural" IN ('wood', 'water', 'mud', 'wetland', 'bay', 'spring', 'scree', 'shingle', 'bare_rock', 'sand', 'heath',
                                                      'grassland', 'scrub', 'beach', 'glacier', 'tree', 'strait', 'cape', 'reef')
                                                      THEN "natural" END,
                'mountain_pass' || CASE WHEN tags->'mountain_pass' IN ('yes') THEN '' END, -- after natural=saddle to give priority to that tag on the same node
                'waterway_' || CASE WHEN "waterway" IN ('waterfall') AND way_area IS NULL THEN waterway END,
                'place_' || CASE WHEN place IN ('island', 'islet', 'square') THEN place END,
                'historic_' || CASE WHEN historic IN ('memorial', 'monument', 'archaeological_site', 'fort', 'castle', 'manor', 'city_gate')
                              THEN historic END,
                'military_'|| CASE WHEN military IN ('danger_area', 'bunker') THEN military END,
                'highway_' || CASE WHEN highway IN ('services', 'rest_area', 'bus_stop', 'elevator', 'traffic_signals') THEN highway END,
                'highway_'|| CASE WHEN tags @> 'ford=>yes' OR tags @> 'ford=>stepping_stones' AND way_area IS NULL THEN 'ford' END,
                'boundary_' || CASE WHEN boundary IN ('aboriginal_lands', 'national_park')
                                          OR (boundary = 'protected_area' AND tags->'protect_class' IN ('1','1a','1b','2','3','4','5','6'))
                                          THEN boundary END,
                'leisure_' || CASE WHEN leisure IN ('nature_reserve') THEN leisure END,
                'tourism_' || CASE WHEN tourism IN ('information') AND tags->'information' IN ('audioguide', 'board', 'guidepost', 'office', 'map', 'tactile_map', 'terminal') THEN tourism END,
                'office' || CASE WHEN tags->'office' IN ('no', 'vacant', 'closed', 'disused', 'empty') OR (tags->'office') IS NULL THEN NULL ELSE '' END,
                'barrier_' || CASE WHEN barrier IN ('toll_booth') AND way_area IS NULL THEN barrier END,
                'waterway_' || CASE WHEN waterway IN ('dam', 'weir', 'dock') THEN waterway END,
                'amenity_' || CASE WHEN amenity IN ('bicycle_parking', 'motorcycle_parking') THEN amenity END,
                'amenity_' || CASE WHEN amenity IN ('parking') AND (tags->'parking' NOT IN ('underground') OR (tags->'parking') IS NULL) THEN amenity END,
                'amenity_' || CASE WHEN amenity IN ('parking_entrance')
                                        AND tags->'parking' IN ('multi-storey', 'underground')
                                        AND (access IS NULL OR access NOT IN ('private', 'no', 'customers', 'permit', 'delivery'))
                                        AND way_area IS NULL
                                   THEN amenity END,
                'tourism_' || CASE WHEN tourism IN ('viewpoint', 'attraction') THEN tourism END,
                'place_' || CASE WHEN place IN ('locality') AND way_area IS NULL THEN place END,
                'golf_' || CASE WHEN tags->'golf' IN ('pin') THEN tags->'golf' END
              ) AS feature,
              CASE WHEN access IN ('private', 'no', 'customers', 'permit', 'delivery') THEN 'restricted' ELSE 'yes' END AS int_access,
              CASE
                WHEN "natural" IN ('peak', 'volcano', 'saddle') 
                     OR tags->'mountain_pass' = 'yes' THEN
                  CASE
                    WHEN tags->'ele' ~ '^-?\d{1,4}(\.\d+)?$' THEN (tags->'ele')::NUMERIC
                  END
                WHEN "waterway" IN ('waterfall') THEN
                  CASE
                    WHEN tags->'height' ~ '^\d{1,3}(\.\d+)?( m)?$' THEN (SUBSTRING(tags->'height', '^(\d{1,3}(\.\d+)?)( m)?$'))::NUMERIC
                  END
              END AS score,
              religion,
              tags->'denomination' as denomination,
              tags->'generator:source' as "generator:source",
              CASE
                WHEN (man_made IN ('mast', 'tower', 'chimney', 'crane') AND (tags->'location' NOT IN ('roof', 'rooftop') OR (tags->'location') IS NULL))
                      OR waterway IN ('waterfall') THEN
                  CASE
                    WHEN tags->'height' ~ '^\d{1,3}(\.\d+)?( m)?$' THEN (SUBSTRING(tags->'height', '^(\d{1,3}(\.\d+)?)( m)?$'))::NUMERIC
                  END
              END AS height,
              tags->'location' as location,
              tags->'icao' as icao,
              tags->'iata' as iata,
              tags->'office' as office,
              tags->'recycling_type' as recycling_type,
              tags->'tower:construction' as "tower:construction",
              tags->'tower:type' as "tower:type",
              tags->'telescope:type' as "telescope:type",
              CASE
                WHEN man_made IN ('telescope') THEN
                  CASE
                    WHEN tags->'telescope:diameter' ~ '^-?\d{1,4}(\.\d+)?$' THEN (tags->'telescope:diameter')::NUMERIC
                  END
              END AS "telescope:diameter",
              tags->'castle_type' as castle_type,
              tags->'sport' as sport,
              tags->'information' as information,
              tags->'memorial' as memorial,
              tags->'artwork_type' as artwork_type,
              tags->'vending' as vending,
              CASE WHEN shop IN ('supermarket', 'bag', 'bakery', 'beauty', 'bed', 'bookmaker', 'books', 'butcher', 'carpet', 'clothes', 'computer',
                                'confectionery', 'fashion', 'convenience', 'department_store', 'doityourself', 'hardware', 'fabric', 'fishmonger', 'florist',
                                'garden_centre', 'hairdresser', 'hifi', 'car', 'car_repair', 'bicycle', 'mall', 'pet',
                                'photo', 'photo_studio', 'photography', 'seafood', 'shoes', 'alcohol', 'gift', 'furniture', 'kiosk',
                                'mobile_phone', 'motorcycle', 'musical_instrument', 'newsagent', 'optician', 'jewelry',
                                'electronics', 'chemist', 'toys', 'travel_agency', 'car_parts', 'greengrocer', 'farm', 'stationery',
                                'laundry', 'dry_cleaning', 'beverages', 'perfumery', 'cosmetics', 'variety_store', 'wine', 'outdoor',
                                'copyshop', 'sports', 'deli', 'tobacco', 'art', 'tea', 'coffee', 'tyres', 'pastry', 'chocolate',
                                'music', 'medical_supply', 'dairy', 'video_games', 'houseware', 'ticket', 'charity', 'second_hand',
                                'interior_decoration', 'video', 'paint', 'massage', 'trade', 'wholesale', 'hearing_aids') THEN shop
                                ELSE 'other' END AS shop,
              CASE WHEN building = 'no' OR building IS NULL THEN 'no' ELSE 'yes' END AS is_building,
              tags->'operator' AS operator,
              ref,
              way_area,
              COALESCE(way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0), 0) AS way_pixels
            FROM
              (SELECT
                  ST_PointOnSurface(way) AS way,
                  name,
                  access,
                  aeroway,
                  amenity,
                  barrier,
                  boundary,
                  building,
                  highway,
                  historic,
                  landuse,
                  leisure,
                  man_made,
                  military,
                  "natural",
                  place,
                  power,
                  ref,
                  religion,
                  shop,
                  tourism,
                  waterway,
                  tags,
                  way_area
                FROM planet_osm_polygon
                WHERE way && !bbox! -- Not ST_PointOnSurface(way) because name might be NULL
                  AND way_area < 768000*POW(!scale_denominator!*0.001*0.28,2)
              UNION ALL
              SELECT
                  way,
                  name,
                  access,
                  aeroway,
                  amenity,
                  barrier,
                  boundary,
                  building,
                  highway,
                  historic,
                  landuse,
                  leisure,
                  man_made,
                  military,
                  "natural",
                  place,
                  power,
                  ref,
                  religion,
                  shop,
                  tourism,
                  waterway,
                  tags,
                  NULL AS way_area
                FROM planet_osm_point
                WHERE way && !bbox!
              ) _
            ) AS features
          WHERE feature IS NOT NULL
          ORDER BY score DESC NULLS LAST,
            way_pixels DESC NULLS LAST
          ) AS amenity_points
    properties:
      cache-features: true
      minzoom: 10
  - id: amenity-line
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
          way,
          COALESCE(
           'highway_' || CASE WHEN tags @> 'ford=>yes' OR tags @> 'ford=>stepping_stones' THEN 'ford' END,
           'leisure_' || CASE WHEN leisure IN ('slipway', 'track') THEN leisure END,
           'attraction_' || CASE WHEN tags @> 'attraction=>water_slide' THEN 'water_slide' END
            ) AS feature
          FROM planet_osm_line
          -- The upcoming where clause is needed for performance only, as the CASE statements would end up doing the equivalent filtering
          WHERE tags @> 'ford=>yes' OR tags @> 'ford=>stepping_stones'
            OR leisure IN ('slipway', 'track')
            OR tags @> 'attraction=>water_slide'
          ORDER BY COALESCE(layer,0)
        ) AS amenity_line
    properties:
      minzoom: 16
  - id: power-towers
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            power
        FROM planet_osm_point
        WHERE power IN ('tower', 'pole')
        ORDER BY
          CASE power
            WHEN 'tower' THEN 2
            WHEN 'pole' THEN 1
          END DESC
        ) AS power_towers
    properties:
      minzoom: 14
  - id: roads-text-ref-low-zoom
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            highway,
            height,
            width,
            refs
          FROM (
            SELECT
                way,
                osm_id,
                highway,
                array_length(refs,1) AS height,
                (SELECT MAX(char_length(ref)) FROM unnest(refs) AS u(ref)) AS width,
                array_to_string(refs, E'\n') AS refs
              FROM (
                SELECT
                    way,
                    osm_id,
                    highway,
                    string_to_array(ref, ';') AS refs
                FROM planet_osm_roads
                  WHERE highway IN ('motorway', 'trunk', 'primary', 'secondary')
                  AND ref IS NOT NULL
              ) AS p) AS q
          WHERE height <= 4 AND width <= 11
          ORDER BY
            CASE
              WHEN highway = 'motorway' THEN 38
              WHEN highway = 'trunk' THEN 37
              WHEN highway = 'primary' THEN 36
              WHEN highway = 'secondary' THEN 35
            END DESC NULLS LAST,
            height DESC,
            width DESC,
            refs,
            osm_id
        ) AS roads_text_ref_low_zoom
    properties:
      minzoom: 10
      maxzoom: 12
  - id: roads-text-ref
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            highway,
            height,
            width,
            refs
          FROM (
            SELECT
                osm_id,
                way,
                highway,
                array_length(refs,1) AS height,
                (SELECT MAX(char_length(ref)) FROM unnest(refs) AS u(ref)) AS width,
                array_to_string(refs, E'\n') AS refs
              FROM (
                SELECT
                    osm_id,
                    way,
                    COALESCE(
                      CASE WHEN highway IN ('motorway', 'trunk', 'primary', 'secondary', 'tertiary') THEN highway END,
                      CASE WHEN aeroway IN ('runway', 'taxiway') THEN aeroway END
                    ) AS highway,
                    string_to_array(ref, ';') AS refs
                  FROM planet_osm_line
                  WHERE (highway IN ('motorway', 'trunk', 'primary', 'secondary', 'tertiary') OR aeroway IN ('runway', 'taxiway'))
                    AND ref IS NOT NULL
              ) AS p) AS q
          WHERE height <= 4 AND width <= 11
          ORDER BY
            CASE
              WHEN highway = 'motorway' THEN 38
              WHEN highway = 'trunk' THEN 37
              WHEN highway = 'primary' THEN 36
              WHEN highway = 'secondary' THEN 35
              WHEN highway = 'tertiary' THEN 34
              WHEN highway = 'runway' THEN 6
              WHEN highway = 'taxiway' THEN 5
            END DESC NULLS LAST,
            height DESC,
            width DESC,
            refs,
            osm_id
        ) AS roads_text_ref
    properties:
      minzoom: 13
  - id: roads-area-text-name
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            ST_PointOnSurface(way) AS way,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels,
            highway,
            name
          FROM planet_osm_polygon
          WHERE ST_PointOnSurface(way) && !bbox!
            AND name IS NOT NULL
            AND (highway IN ('pedestrian', 'footway', 'service', 'living_street', 'platform')
              OR (railway IN ('platform')
                  AND (tags->'location' NOT IN ('underground') OR (tags->'location') IS NULL)
                  AND (tunnel NOT IN ('yes', 'building_passage') OR tunnel IS NULL)
                  AND (covered NOT IN ('yes') OR covered IS NULL)))
            AND way_area > 3000*POW(!scale_denominator!*0.001*0.28,2)
            AND way_area < 768000*POW(!scale_denominator!*0.001*0.28,2)
          ORDER BY way_area DESC
        ) AS roads_area_text_name
    properties:
      minzoom: 15
  - id: roads-text-name
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            CASE WHEN substr(highway, length(highway)-4, 5) = '_link' THEN substr(highway, 0, length(highway)-4) ELSE highway END,
            CASE WHEN (tunnel = 'yes' OR tunnel = 'building_passage' OR covered = 'yes') THEN 'yes' ELSE 'no' END AS tunnel,
            construction,
            name,
            CASE
              WHEN oneway IN ('yes', '-1') THEN oneway
              WHEN junction IN ('roundabout') AND (oneway IS NULL OR NOT oneway IN ('no', 'reversible')) THEN 'yes'
            END AS oneway
          FROM planet_osm_line l
          WHERE highway IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary',
                            'tertiary_link', 'residential', 'unclassified', 'road', 'service', 'pedestrian', 'raceway', 'living_street', 'construction')
            AND (name IS NOT NULL
              OR oneway IN ('yes', '-1')
              OR junction IN ('roundabout'))
          ORDER BY
            z_order DESC, -- put important roads first
            COALESCE(layer, 0), -- put top layered roads first
            length(name) DESC, -- Try to fit big labels in first
            name DESC, -- Force a consistent ordering between differently named streets
            l.osm_id DESC -- Force an ordering for streets of the same name, e.g. dualized roads
        ) AS roads_text_name
    properties:
      cache-features: true
      minzoom: 13
  - id: paths-text-name
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            CASE WHEN highway = 'path' THEN carto_path_type(bicycle, horse) ELSE highway END AS highway,
            construction,
            name,
            CASE
              WHEN oneway IN ('yes', '-1') THEN oneway
              WHEN junction IN ('roundabout') AND (oneway IS NULL OR NOT oneway IN ('no', 'reversible')) THEN 'yes'
            END AS oneway
          FROM planet_osm_line
          WHERE highway IN ('bridleway', 'footway', 'cycleway', 'path', 'track', 'steps', 'construction')
            AND (name IS NOT NULL
              OR oneway IN ('yes', '-1')
              OR junction IN ('roundabout'))
        ) AS paths_text_name
    properties:
      cache-features: true
      minzoom: 15
  - id: railways-text-name
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            CASE
              WHEN (railway = 'rail' AND service IN ('spur', 'siding', 'yard')) THEN 'INT-spur-siding-yard'
              WHEN (railway = 'tram' AND service IN ('spur', 'siding', 'yard')) THEN 'tram-service'
              ELSE railway
            END AS railway,
            CASE WHEN (tunnel = 'yes' OR tunnel = 'building_passage' OR covered = 'yes') THEN 'yes' ELSE 'no' END AS tunnel,
            tags->'highspeed' as highspeed,
            tags->'usage' as usage,
            construction,
            name,
            (CASE
              WHEN tags->'railway:preserved' = 'yes' THEN 'yes'
              ELSE 'no'
            END) AS preserved
          FROM planet_osm_line l
          WHERE railway IN ('rail', 'subway', 'narrow_gauge', 'light_rail', 'funicular', 'monorail', 'miniature', 'tram', 'disused', 'construction')
            AND (tunnel IS NULL OR NOT tunnel IN ('yes', 'building_passage'))
            AND highway IS NULL -- Prevent duplicate rendering
            AND name IS NOT NULL
          ORDER BY
            z_order DESC, -- put important rails first
            COALESCE(layer, 0), -- put top layered rails first
            length(name) DESC, -- Try to fit big labels in first
            name DESC, -- Force a consistent ordering between differently named railways
            l.osm_id DESC -- Force an ordering for railways of the same name, e.g. dualized rails
        ) AS railways_text_name
    properties:
      minzoom: 11
  - id: roads-text-ref-minor
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            highway,
            height,
            width,
            refs
          FROM (
            SELECT
                osm_id,
                way,
                highway,
                array_length(refs,1) AS height,
                (SELECT MAX(char_length(ref)) FROM unnest(refs) AS u(ref)) AS width,
                array_to_string(refs, E'\n') AS refs
              FROM (
                SELECT
                    osm_id,
                    way,
                    CASE WHEN highway IN ('unclassified', 'residential', 'track') THEN highway END AS highway,
                    string_to_array(ref, ';') AS refs
                  FROM planet_osm_line
                  WHERE highway IN ('unclassified', 'residential', 'track')
                    AND ref IS NOT NULL
              ) AS p) AS q
          WHERE height <= 4 AND width <= 11
          ORDER BY
            CASE
              WHEN highway = 'unclassified' THEN 33
              WHEN highway = 'residential' THEN 32
              WHEN highway = 'track' THEN 30
            END DESC NULLS LAST,
            height DESC,
            width DESC,
            refs,
            osm_id
        ) AS roads_text_ref_minor
    properties:
      minzoom: 15
  - id: text-poly-low-zoom
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            ST_PointOnSurface(way) AS way,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels,
            COALESCE(
              'landuse_' || CASE WHEN landuse IN ('forest', 'military', 'farmland') THEN landuse END,
              'military_' || CASE WHEN military IN ('danger_area') THEN military END,
              'natural_' || CASE WHEN "natural" IN ('wood', 'glacier', 'sand', 'scree', 'shingle', 'bare_rock', 'water') THEN "natural" END,
              'place_' || CASE WHEN place IN ('island') THEN place END,
              'boundary_' || CASE WHEN boundary IN ('aboriginal_lands', 'national_park')
                                       OR (boundary = 'protected_area' AND tags->'protect_class' IN ('1','1a','1b','2','3','4','5','6'))
                                       THEN boundary END,
              'leisure_' || CASE WHEN leisure IN ('nature_reserve') THEN leisure END
            ) AS feature,
            name,
            CASE WHEN building = 'no' OR building IS NULL THEN 'no' ELSE 'yes' END AS is_building -- always no with the where conditions
          FROM planet_osm_polygon
          WHERE ST_PointOnSurface(way) && !bbox!
            AND name IS NOT NULL
            AND (landuse IN ('forest', 'military', 'farmland')
              OR military IN ('danger_area')
              OR "natural" IN ('wood', 'glacier', 'sand', 'scree', 'shingle', 'bare_rock', 'water')
              OR "place" IN ('island')
              OR boundary IN ('aboriginal_lands', 'national_park')
              OR (boundary = 'protected_area' AND tags->'protect_class' IN ('1','1a','1b','2','3','4','5','6'))
              OR leisure IN ('nature_reserve'))
            AND building IS NULL
            AND way_area > 100*POW(!scale_denominator!*0.001*0.28,2)
            AND way_area < 768000*POW(!scale_denominator!*0.001*0.28,2)
          ORDER BY way_area DESC
        ) AS text_poly_low_zoom
    properties:
      minzoom: 4
      maxzoom: 9
  - id: text-line
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
          way,
            NULL as way_pixels,
            COALESCE('aerialway_' || aerialway, 'attraction_' || CASE WHEN tags @> 'attraction=>water_slide' THEN 'water_slide' END, 'leisure_' || leisure, 'man_made_' || man_made, 'waterway_' || waterway, 'natural_' || "natural", 'golf_' || (tags->'golf')) AS feature,
            access,
            name,
            tags->'operator' as operator,
            ref,
            NULL AS way_area,
            CASE WHEN building = 'no' OR building IS NULL THEN 'no' ELSE 'yes' END AS is_building
          FROM planet_osm_line
          WHERE ((man_made IN ('pier', 'breakwater', 'groyne', 'embankment')
              OR (man_made = 'pipeline'
                 AND tags-> 'location' IN ('overground', 'overhead', 'surface', 'outdoor')
                 OR bridge IN ('yes', 'aqueduct', 'cantilever', 'covered', 'trestle', 'viaduct'))
              OR tags @> 'attraction=>water_slide'
              OR aerialway IN ('cable_car', 'gondola', 'mixed_lift', 'goods', 'chair_lift', 'drag_lift', 't-bar', 'j-bar', 'platter', 'rope_tow', 'zip_line')
              OR leisure IN ('slipway', 'track')
              OR waterway IN ('dam', 'weir')
              OR "natural" IN ('arete', 'cliff', 'ridge'))
            AND name IS NOT NULL)
            OR (tags @> 'golf=>hole' AND ref IS NOT NULL)
        ) AS text_line
    properties:
      minzoom: 10
  - id: text-point
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      # Include values that are rendered as icon without label to prevent mismatch between icons and labels,
      # see https://github.com/gravitystorm/openstreetmap-carto/pull/1349#issuecomment-77805678
      table: *amenity_points_sql
    properties:
      minzoom: 10
  - id: building-text
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            name,
            ST_PointOnSurface(way) AS way,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels
          FROM planet_osm_polygon
          WHERE ST_PointOnSurface(way) && !bbox!
            AND name IS NOT NULL
            AND building IS NOT NULL
            AND building NOT IN ('no')
            AND way_area < 4000000*POW(!scale_denominator!*0.001*0.28,2)
          ORDER BY way_area DESC
        ) AS building_text
    properties:
      minzoom: 14
  - id: interpolation
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way
          FROM planet_osm_line
          WHERE "addr:interpolation" IS NOT NULL
        ) AS interpolation
    properties:
      minzoom: 17
  - id: addresses
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            ST_PointOnSurface(way) AS way,
            "addr:housenumber" AS addr_housenumber,
            "addr:housename" AS addr_housename,
            tags->'addr:unit' AS addr_unit,
            tags->'addr:flats' AS addr_flats,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels
          FROM planet_osm_polygon
          WHERE way && !bbox! -- Not ST_PointOnSurface(way) because name might be NULL
            AND (("addr:housenumber" IS NOT NULL) OR ("addr:housename" IS NOT NULL) OR ((tags->'addr:unit') IS NOT NULL) OR ((tags->'addr:flats') IS NOT NULL))
            AND building IS NOT NULL
            AND way_area < 4000000*POW(!scale_denominator!*0.001*0.28,2)
        UNION ALL
        SELECT
            way,
            "addr:housenumber" AS addr_housenumber,
            "addr:housename" AS addr_housename,
            tags->'addr:unit' AS addr_unit,
            tags->'addr:flats' AS addr_flats,
            NULL AS way_pixels
          FROM planet_osm_point
          WHERE way && !bbox!
            AND (("addr:housenumber" IS NOT NULL) OR ("addr:housename" IS NOT NULL) OR ((tags->'addr:unit') IS NOT NULL) OR ((tags->'addr:flats') IS NOT NULL))
          ORDER BY way_pixels DESC NULLS LAST
        ) AS addresses
    properties:
      minzoom: 17
  - id: water-lines-text
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            waterway,
            lock,
            name,
            "natural",
            tags->'lock_name' AS lock_name,
            CASE WHEN tags->'intermittent' IN ('yes')
              OR tags->'seasonal' IN ('yes', 'spring', 'summer', 'autumn', 'winter', 'wet_season', 'dry_season')
              THEN 'yes' ELSE 'no' END AS int_intermittent,
            CASE WHEN tunnel IN ('yes', 'culvert') 
              OR waterway = 'canal' AND tunnel = 'flooded'
              THEN 'yes' ELSE 'no' END AS int_tunnel
          FROM planet_osm_line
          WHERE (waterway IN ('river', 'canal', 'stream', 'drain', 'ditch')
                 OR "natural" IN ('bay', 'strait'))
            AND (tunnel IS NULL OR tunnel != 'culvert')
            AND name IS NOT NULL
          ORDER BY COALESCE(layer,0)
        ) AS water_lines_text
    properties:
      minzoom: 13
  - id: ferry-routes-text
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            name
          FROM planet_osm_line
          WHERE route = 'ferry'
            AND osm_id > 0
            AND name IS NOT NULL
        ) AS ferry_routes_text
    properties:
      minzoom: 13
  - id: admin-text
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            name,
            admin_level,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels
          FROM planet_osm_polygon
          WHERE boundary = 'administrative'
            AND admin_level IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10')
            AND name IS NOT NULL
            AND osm_id < 0
            AND way_area > 196000*POW(!scale_denominator!*0.001*0.28,2)
          ORDER BY admin_level::integer ASC, way_area DESC
        ) AS admin_text
    properties:
      minzoom: 11
  - id: protected-areas-text
    geometry: linestring
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: |-
        (SELECT
            way,
            name,
            boundary,
            tags->'protect_class' AS protect_class,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels
          FROM planet_osm_polygon
          WHERE (boundary IN ('aboriginal_lands', 'national_park')
                 OR leisure = 'nature_reserve'
                 OR (boundary = 'protected_area' AND tags->'protect_class' IN ('1','1a','1b','2','3','4','5','6')))
            AND name IS NOT NULL
        ) AS protected_areas_text
    properties:
      minzoom: 13
  - id: amenity-low-priority
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      table: &amenity_low_priority_sql |-
        (SELECT
            way,
            name,
            COALESCE(
              'railway_' || CASE WHEN railway IN ('level_crossing', 'crossing') AND way_area IS NULL THEN railway END,
              'amenity_' || CASE WHEN amenity IN ('bench', 'waste_basket', 'waste_disposal') AND way_area IS NULL THEN amenity END,
              'historic_' || CASE WHEN historic IN ('wayside_cross', 'wayside_shrine') AND way_area IS NULL THEN historic END,
              'man_made_' || CASE WHEN man_made IN ('cross') AND way_area IS NULL THEN man_made END,
              'barrier_' || CASE WHEN barrier IN ('bollard', 'gate', 'lift_gate', 'swing_gate', 'block', 'log', 'cattle_grid', 'stile', 'motorcycle_barrier', 'cycle_barrier', 'full-height_turnstile', 'turnstile', 'kissing_gate') THEN barrier END
            )  AS feature,
            CASE WHEN access IN ('private', 'no', 'customers', 'permit', 'delivery') THEN 'restricted' ELSE 'yes' END AS int_access,
            way_area,
            way_area/NULLIF(POW(!scale_denominator!*0.001*0.28,2),0) AS way_pixels
            FROM
              (SELECT
                  ST_PointOnSurface(way) AS way,
                  name,
                  access,
                  amenity,
                  barrier,
                  highway,
                  historic,
                  man_made,
                  railway,
                  tags,
                  way_area
                FROM planet_osm_polygon
                WHERE way && !bbox! -- Not ST_PointOnSurface(way) because name might be NULL
                  AND way_area < 768000*POW(!scale_denominator!*0.001*0.28,2)
              UNION ALL
              SELECT
                  way,
                  name,
                  access,
                  amenity,
                  barrier,
                  highway,
                  historic,
                  man_made,
                  railway,
                  tags,
                  NULL AS way_area
                FROM planet_osm_point
                WHERE way && !bbox!
              ) _
            WHERE railway IN ('level_crossing', 'crossing')
               OR amenity IN ('bench', 'waste_basket', 'waste_disposal')
               OR historic IN ('wayside_cross', 'wayside_shrine')
               OR man_made IN ('cross')
               OR barrier IN ('bollard', 'gate', 'lift_gate', 'swing_gate', 'block', 'log', 'cattle_grid', 'stile', 'motorcycle_barrier', 'cycle_barrier', 'full-height_turnstile', 'turnstile', 'kissing_gate')
            ORDER BY
              CASE amenity
                WHEN 'waste_basket' THEN 1
                WHEN 'waste_disposal' THEN 1
                WHEN 'bench' THEN 2
                WHEN NULL THEN 3
              END DESC,
              way_pixels DESC NULLS LAST
          ) AS amenity_low_priority
    properties:
      cache-features: true
      minzoom: 14
  - id: text-low-priority
    geometry: point
    <<: *extents
    Datasource:
      <<: *osm2pgsql
      # Include values that are rendered as icon without label to prevent mismatch between icons and labels,
      # see https://github.com/gravitystorm/openstreetmap-carto/pull/1349#issuecomment-77805678
      table: *amenity_low_priority_sql
    properties:
      minzoom: 17