mapterm 0.1.2

A map in your terminal: vector tiles, MapLibre styles, imagery and your own data, as a ratatui widget and the mapterm viewer
Documentation
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
//! A map, MapLibre GL style: named sources (vector tiles, GeoJSON) plus an ordered list of
//! style layers that say how to draw them, with custom layers for anything else. The app
//! changes the map with `set_data`, `set_paint`, `set_filter` and friends, draws it with
//! [`Map::view`], and asks what's under the pointer with [`Map::query_rendered_features`].
//!
//! ```no_run
//! # use mapterm::{Camera, Map, MapSource, Source, StyleLayer, source::OPENFREEMAP};
//! # async fn demo(mut term: ratatui::DefaultTerminal, route_json: serde_json::Value) -> anyhow::Result<()> {
//! # let cam = Camera::new(-77.03, 38.89, 13.0);
//! let mut map = Map::new();
//! map.add_source("basemap", MapSource::vector(Source::open(OPENFREEMAP).await?))?;
//! map.add_source("route", MapSource::geojson(&route_json)?)?;
//! map.add_layer(StyleLayer::background("bg").color("#101418"))?;
//! map.add_layer(StyleLayer::line("roads", "basemap").source_layer("transportation").color("#666"))?;
//! map.add_layer(StyleLayer::line("route", "route").color("#00e5ff"))?;
//! loop {
//!     let area = term.get_frame().area();
//!     map.load_tiles(&cam, area).await?;
//!     term.draw(|frame| frame.render_widget(map.view(&cam), area))?;
//!     // What's under a spot: braille dots in the map area (2 per column, 4 per row).
//!     for f in map.query_rendered_features(&cam, area, (40.0, 40.0), Some(&["route"])) {
//!         println!("{}", f.title());
//!     }
//! #   break;
//! }
//! # Ok(())
//! # }
//! ```

use std::{
    any::Any,
    collections::{HashMap, HashSet, VecDeque},
    sync::Arc,
};

use anyhow::{Result, anyhow, bail};
use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Modifier, Style as TStyle},
    widgets::Widget,
};
use serde_json::Value as Json;
use unicode_width::UnicodeWidthStr;

use crate::{
    raster::{RasterSource, RasterStore, RasterTile},
    expr::{Ctx, Rgba, Val},
    geojson,
    layer::{CustomLayer, DrawContext, PickContext, render_with},
    mvt::GeomType,
    pick::{Pick, hit, stats},
    render::{Camera, Viewport, to_lonlat, to_world},
    source::Source,
    spec::{Kind, StyleLayer},
    store::{self, TileId},
};

/// How many decoded tiles a vector source keeps.
const TILE_CACHE: usize = 512;

/// Priority room per layer: symbols in higher layers win collisions, then lower sort keys.
const LAYER_PRIORITY: i32 = 10_000;

/// A source of features for style layers.
pub enum MapSource {
    /// Vector tiles (MVT), from PMTiles, XYZ or TileJSON.
    Vector(VectorSource),
    /// GeoJSON (or CSV, GPX, KML, FlatGeobuf turned into GeoJSON).
    GeoJson(GeoJsonSource),
    /// Image tiles for raster layers.
    Raster(RasterTiles),
}

impl MapSource {
    /// Vector tiles fetched from `src` by [`Map::load_tiles`].
    pub fn vector(src: Source) -> Self {
        let attribution = src.attribution.as_deref().map(crate::source::plain_attribution);
        MapSource::Vector(VectorSource { min_zoom: src.min_zoom, max_zoom: src.max_zoom, attribution, promote_id: None, fetch: Some(Arc::new(src)), tiles: Tiles::default() })
    }

    /// Vector tiles the app supplies itself with [`Map::insert_tile`].
    pub fn vector_tiles(min_zoom: u8, max_zoom: u8) -> Self {
        MapSource::Vector(VectorSource { min_zoom, max_zoom, attribution: None, promote_id: None, fetch: None, tiles: Tiles::default() })
    }

    /// The property used as feature id, if set (MapLibre's `promoteId`).
    pub fn promote_id(&self) -> Option<&str> {
        match self {
            MapSource::Vector(v) => v.promote_id.as_deref(),
            MapSource::GeoJson(g) => g.promote_id.as_deref(),
            MapSource::Raster(_) => None,
        }
    }

    /// Cluster a GeoJSON source's points (MapLibre's `cluster: true`, `clusterRadius`,
    /// `clusterMaxZoom`). Cluster features have `cluster`, `cluster_id`, `point_count` and
    /// `point_count_abbreviated` properties.
    pub fn with_cluster(mut self, options: crate::cluster::ClusterOptions) -> Self {
        if let MapSource::GeoJson(g) = &mut self {
            g.set_cluster(Some(options));
        }
        self
    }

    /// Use a property as the feature id (MapLibre's `promoteId`), for feature state.
    pub fn with_promote_id(mut self, prop: impl Into<String>) -> Self {
        match &mut self {
            MapSource::Vector(v) => v.promote_id = Some(prop.into()),
            MapSource::GeoJson(g) => g.promote_id = Some(prop.into()),
            MapSource::Raster(_) => {}
        }
        self
    }

    /// The source's credit as plain text, if it has one.
    pub fn attribution(&self) -> Option<&str> {
        match self {
            MapSource::Vector(v) => v.attribution.as_deref(),
            MapSource::GeoJson(g) => g.attribution.as_deref(),
            MapSource::Raster(r) => Some(r.src.attribution.as_str()),
        }
        .filter(|a| !a.trim().is_empty())
    }

    /// A GeoJSON document (FeatureCollection, Feature or geometry).
    pub fn geojson(doc: &Json) -> Result<Self> {
        Ok(MapSource::GeoJson(GeoJsonSource::parse(doc)?))
    }

    /// Image tiles (PNG, JPEG, WebP) for raster layers.
    pub fn raster(src: Arc<RasterSource>) -> Self {
        MapSource::Raster(RasterTiles { src, tiles: RasterStore::default() })
    }
}

/// A raster source and its decoded tiles.
pub struct RasterTiles {
    /// Where the tiles come from.
    pub src: Arc<RasterSource>,
    tiles: RasterStore,
}

/// How raster layers are drawn.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum RasterMode {
    /// One averaged color per cell background; works in any terminal.
    #[default]
    Cells,
    /// Real images with the kitty graphics protocol, placed under the text by
    /// [`Map::kitty_commands`]. `cell_px` is the cell size in pixels.
    Kitty {
        /// Cell width and height in pixels.
        cell_px: (f64, f64),
    },
}

/// A tile a view needs, to load in the background with [`TileRequest::load`] and hand back
/// with [`Map::insert_loaded`].
pub struct TileRequest {
    /// The source id.
    pub source: String,
    /// The tile.
    pub id: TileId,
    fetch: Fetch,
}

enum Fetch {
    Vector(Arc<Source>),
    Raster(Arc<RasterSource>),
}

/// A fetched and decoded tile.
pub struct Loaded(LoadedTile);

enum LoadedTile {
    Vector(Vec<store::Feature>),
    Raster(Option<RasterTile>),
}

impl TileRequest {
    /// Whether it is an image tile (image servers often ask for fewer connections).
    pub fn is_raster(&self) -> bool {
        matches!(self.fetch, Fetch::Raster(_))
    }

    /// Fetch and decode the tile (decoding runs on a blocking thread).
    pub async fn load(&self) -> Result<Loaded> {
        let (z, x, y) = self.id;
        Ok(Loaded(match &self.fetch {
            Fetch::Vector(src) => match src.tile(z, x, y).await? {
                Some(b) => LoadedTile::Vector(tokio::task::spawn_blocking(move || store::decode_features(&b)).await??),
                None => LoadedTile::Vector(Vec::new()),
            },
            Fetch::Raster(src) => match src.tile(z, x, y).await? {
                Some(b) => LoadedTile::Raster(Some(tokio::task::spawn_blocking(move || crate::raster::decode(&b)).await??)),
                None => LoadedTile::Raster(None),
            },
        }))
    }
}

/// The images [`Map::kitty_commands`] has sent to the terminal: their decoded size and the
/// last frame that used each.
///
/// Terminals keep a limited amount of image memory, counted in decoded RGBA bytes (herdr: 64
/// MiB per pane; Ghostty: 320 MB) and evict on their own when it runs out, which can drop a
/// tile still on screen that mapterm believes is loaded. So mapterm keeps under `budget`
/// itself, freeing the least recently used images first.
pub struct KittyState {
    sent: HashMap<u32, (usize, u64)>,
    frame: u64,
    /// Most decoded image bytes to keep in the terminal (default 48 MiB, under herdr's 64 MiB;
    /// `MAPTERM_KITTY_BUDGET_MB` overrides it in the viewer).
    pub budget: usize,
}

impl Default for KittyState {
    fn default() -> Self {
        Self { sent: HashMap::new(), frame: 0, budget: 48 << 20 }
    }
}

impl KittyState {
    /// Decoded bytes currently held by the terminal.
    pub fn bytes(&self) -> usize {
        self.sent.values().map(|(b, _)| b).sum()
    }
}

impl From<geojson::GeoJson> for MapSource {
    fn from(g: geojson::GeoJson) -> Self {
        MapSource::GeoJson(GeoJsonSource::new(g.features, g.bbox))
    }
}

/// A vector tile source and its loaded tiles.
pub struct VectorSource {
    /// Lowest zoom with tiles.
    pub min_zoom: u8,
    /// Highest zoom with tiles (higher zooms reuse these).
    pub max_zoom: u8,
    /// A property to use as the feature id (MapLibre's `promoteId`), for feature state.
    pub promote_id: Option<String>,
    /// Credit to show while the source is drawn (plain text).
    pub attribution: Option<String>,
    fetch: Option<Arc<Source>>,
    tiles: Tiles,
}

/// A GeoJSON source: features, a spatial index, and clusters when on.
pub struct GeoJsonSource {
    /// The features, with a spatial index so drawing and picking look only at those in view.
    data: crate::cluster::Level,
    /// [min_x, min_y, max_x, max_y] in world coordinates.
    pub bbox: [f64; 4],
    /// Credit to show while the source is drawn (plain text).
    pub attribution: Option<String>,
    /// A property to use as the feature id (MapLibre's `promoteId`), for feature state.
    pub promote_id: Option<String>,
    /// Point clusters by zoom, when clustering is on.
    clusters: Option<crate::cluster::Clusters>,
}

impl GeoJsonSource {
    /// A source of these features (`bbox` in world coordinates).
    pub fn new(features: Vec<geojson::Feature>, bbox: [f64; 4]) -> Self {
        Self { data: crate::cluster::Level::new(features), bbox, attribution: None, promote_id: None, clusters: None }
    }

    /// Parse a GeoJSON document.
    pub fn parse(doc: &Json) -> Result<Self> {
        let (features, bbox) = geojson::features(doc)?;
        Ok(Self::new(features, bbox))
    }

    /// The features as given.
    pub fn features(&self) -> &[geojson::Feature] {
        &self.data.features
    }

    /// Cluster nearby points (MapLibre's `cluster: true`); `None` turns it off.
    pub fn set_cluster(&mut self, options: Option<crate::cluster::ClusterOptions>) {
        self.clusters = options.map(|o| crate::cluster::Clusters::build(&self.data.features, o));
    }

    /// The clustering settings, if on.
    pub fn cluster_options(&self) -> Option<crate::cluster::ClusterOptions> {
        self.clusters.as_ref().map(|c| c.options)
    }

    /// The features to draw at `zoom` (clusters when on) and their index.
    fn at(&self, zoom: f64) -> &crate::cluster::Level {
        self.clusters.as_ref().and_then(|c| c.at(zoom)).unwrap_or(&self.data)
    }

    /// Features (of [`GeoJsonSource::at`]) whose boxes overlap `q` (world coordinates).
    fn near(&self, zoom: f64, q: crate::index::BBox) -> impl Iterator<Item = &geojson::Feature> {
        let level = self.at(zoom);
        level.index.search(q).into_iter().map(move |i| &level.features[i])
    }
}

/// What a style layer resolved to for one feature.
#[derive(Clone, Debug)]
enum Resolved {
    Fill { color: Option<Color>, outline: Option<Color> },
    /// `width` in dots (1 to 3); `dash` is on/off lengths in dots.
    Line { color: Color, width: u8, dash: Option<Vec<f64>> },
    /// A glyph at each point, with an optional label beside it.
    Point { glyph: String, style: TStyle, label: Option<(String, TStyle)>, priority: i32 },
    /// `anchors` are tried in order (`text-variable-anchor`); `offset` is `text-offset` in
    /// dots; `spacing` is `symbol-spacing` in dots, for labels repeated along lines.
    Text { text: String, style: TStyle, anchors: Vec<(i8, i8)>, offset: (f64, f64), along_line: bool, spacing: f64, priority: i32 },
}

struct Item {
    layer: u16,
    feature: u32,
    paint: Resolved,
}

/// One decoded vector tile: its features and what the style draws of them, by layer.
struct MapTile {
    features: Arc<Vec<store::Feature>>,
    /// What layers whose style ignores the zoom draw, sorted by layer.
    items: Vec<Item>,
    /// Feature indices by source-layer name.
    by_layer: HashMap<Arc<str>, Vec<u32>>,
    /// What zoom-dependent layers draw, by layer, for the zoom they were evaluated at.
    zoomed: std::sync::Mutex<ZoomedItems>,
}

/// Items of zoom-dependent layers by layer index, with the zoom they were evaluated at.
type ZoomedItems = HashMap<u16, (ZoomKey, u64, Arc<Vec<Item>>)>;

/// The zooms a zoom-dependent layer was evaluated at: filters and layout at the whole zoom
/// level, paint at the camera zoom (to 1/20 of a level).
type ZoomKey = (i64, i64);

fn zoom_key(zoom: f64) -> ZoomKey {
    (zoom.floor() as i64, (zoom * 20.0).round() as i64)
}

impl MapTile {
    fn new(features: Arc<Vec<store::Feature>>, items: Vec<Item>) -> Self {
        let mut by_layer: HashMap<Arc<str>, Vec<u32>> = HashMap::new();
        for (i, f) in features.iter().enumerate() {
            by_layer.entry(f.layer.clone()).or_default().push(i as u32);
        }
        Self { features, items, by_layer, zoomed: Default::default() }
    }

    fn items_of(&self, layer: usize) -> &[Item] {
        let lo = self.items.partition_point(|i| (i.layer as usize) < layer);
        let hi = self.items.partition_point(|i| (i.layer as usize) <= layer);
        &self.items[lo..hi]
    }

    /// What style layer `i` draws from this tile at `zoom`. Layers that ignore the zoom were
    /// evaluated when the tile loaded; the others are evaluated here, as MapLibre does:
    /// filters and layout at the whole zoom level (past the source's last tile zoom too),
    /// paint at the exact camera zoom. Kept until the zoom changes.
    /// Layers reading feature state are evaluated here too, again when the state changes.
    fn layer_items(&self, i: usize, l: &StyleLayer, zoom: f64, bg: Option<Rgba>, env: StateEnv) -> LayerItems<'_> {
        let stateful = l.uses_state();
        if !stateful && !l.uses_zoom() {
            return LayerItems::Cached(self.items_of(i));
        }
        let key = zoom_key(zoom);
        let version = if stateful { env.version } else { 0 };
        let mut cache = self.zoomed.lock().unwrap_or_else(|e| e.into_inner());
        if let Some((k, v, items)) = cache.get(&(i as u16))
            && *k == key
            && *v == version
        {
            return LayerItems::Zoomed(items.clone());
        }
        let mut items = Vec::new();
        if let Some(fs) = l.source_layer.as_deref().and_then(|s| self.by_layer.get(s)) {
            for &fi in fs {
                let f = &self.features[fi as usize];
                let c = Ctx { zoom: zoom.floor(), geom: f.geom, props: &f.props, state: env.of(&f.id, &f.props) };
                if let Some(paint) = resolve(l, &c, zoom, bg) {
                    items.push(Item { layer: i as u16, feature: fi, paint });
                }
            }
        }
        let items = Arc::new(items);
        cache.insert(i as u16, (key, version, items.clone()));
        LayerItems::Zoomed(items)
    }
}

enum LayerItems<'a> {
    Cached(&'a [Item]),
    Zoomed(Arc<Vec<Item>>),
}

impl std::ops::Deref for LayerItems<'_> {
    type Target = [Item];
    fn deref(&self) -> &[Item] {
        match self {
            LayerItems::Cached(s) => s,
            LayerItems::Zoomed(v) => v,
        }
    }
}

#[derive(Default)]
struct Tiles {
    map: HashMap<TileId, Arc<MapTile>>,
    order: VecDeque<TileId>,
}

impl Tiles {
    fn insert(&mut self, id: TileId, t: MapTile) {
        if self.map.insert(id, Arc::new(t)).is_none() {
            self.order.push_back(id);
        }
        while self.order.len() > TILE_CACHE {
            if let Some(old) = self.order.pop_front() {
                self.map.remove(&old);
            }
        }
    }

    /// The tile, or its nearest loaded ancestor (up to 4 levels) to stand in while it loads.
    fn get_or_ancestor(&self, id: TileId) -> Option<(TileId, Arc<MapTile>)> {
        let (z, x, y) = id;
        (0..=4u8.min(z)).find_map(|k| {
            let a = (z - k, x >> k, y >> k);
            self.map.get(&a).map(|t| (a, t.clone()))
        })
    }

    /// Loaded tiles covering the view (ancestors standing in), lowest zoom first.
    fn visible(&self, vp: &Viewport, z: u8) -> Vec<(TileId, Arc<MapTile>)> {
        let mut seen = HashSet::new();
        let mut out: Vec<(TileId, Arc<MapTile>)> =
            vp.tiles(z).into_iter().filter_map(|id| self.get_or_ancestor(id)).filter(|(id, _)| seen.insert(*id)).collect();
        out.sort_by_key(|(id, _)| id.0);
        out
    }
}

/// A custom layer the map owns, which can be found again by type.
/// (`Send` so a whole map can be built on another task, e.g. while a style loads.)
trait OwnedCustom: CustomLayer + Any + Send {}
impl<T: CustomLayer + Any + Send> OwnedCustom for T {}

enum Entry {
    Style(StyleLayer),
    Custom { id: String, layer: Box<dyn OwnedCustom> },
}

impl Entry {
    fn id(&self) -> &str {
        match self {
            Entry::Style(l) => &l.id,
            Entry::Custom { id, .. } => id,
        }
    }
}

#[derive(Default)]
/// A map: sources, style and custom layers, feature state, and how raster layers draw. See the [module docs](self).
pub struct Map {
    sources: Vec<(String, MapSource)>,
    layers: Vec<Entry>,
    warnings: Vec<String>,
    /// How raster layers draw (cell colors unless set to kitty images).
    pub raster_mode: RasterMode,
    /// The starting view (lon, lat, zoom) a style suggests.
    pub center: Option<(f64, f64, f64)>,
    /// When to ask again for tiles that failed to load.
    pub retry: RetryPolicy,
    /// Tiles whose last load failed: attempts so far and when to try again.
    failed: HashMap<(String, TileId), (u32, std::time::Instant)>,
    /// Feature state by source, then feature id (as text), then key.
    states: HashMap<String, HashMap<String, Vec<(String, Val)>>>,
    /// Bumped whenever feature state changes, so layers that read it are re-evaluated.
    state_version: u64,
}

/// The id a source gives a feature: its `promoteId` property when set, else the feature's id.
fn feature_id(promote: Option<&str>, id: &Option<crate::mvt::Value>, props: &[(String, crate::mvt::Value)]) -> Option<crate::mvt::Value> {
    match promote {
        Some(p) => props.iter().find(|(k, _)| k == p).map(|(_, v)| v.clone()),
        None => id.clone(),
    }
}

/// What layers reading feature state need: the source's states and `promoteId`, and the
/// state version (part of the cache key).
#[derive(Clone, Copy)]
struct StateEnv<'a> {
    states: Option<&'a HashMap<String, Vec<(String, Val)>>>,
    promote: Option<&'a str>,
    version: u64,
}

impl<'a> StateEnv<'a> {
    fn of(&self, id: &Option<crate::mvt::Value>, props: &[(String, crate::mvt::Value)]) -> &'a [(String, Val)] {
        let Some(states) = self.states else { return &[] };
        feature_id(self.promote, id, props).and_then(|id| states.get(&id.to_string())).map_or(&[], |v| v.as_slice())
    }
}

/// Retrying tiles that failed to load (timeouts, network or server errors). A tile that
/// doesn't exist ("no tile here") is not a failure: it is stored empty at once.
#[derive(Clone, Copy, Debug)]
pub struct RetryPolicy {
    /// Wait after the first failure; doubles with each further one.
    pub base: std::time::Duration,
    /// Longest wait between attempts.
    pub max: std::time::Duration,
    /// After this many failures in a row the tile is stored empty and not requested again
    /// (until it leaves the cache).
    pub attempts: u32,
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self { base: std::time::Duration::from_secs(2), max: std::time::Duration::from_secs(60), attempts: 5 }
    }
}

impl Map {
    /// An empty map.
    pub fn new() -> Self {
        Self::default()
    }

    // ---- sources ----

    /// Add a source under `id` (an error if the id is taken).
    pub fn add_source(&mut self, id: impl Into<String>, src: MapSource) -> Result<()> {
        let id = id.into();
        if self.source(&id).is_some() {
            bail!("source \"{id}\" already exists");
        }
        self.sources.push((id, src));
        self.restyle();
        Ok(())
    }

    /// Move another map's sources and layers into this one, its layers just below `before`
    /// (or on top): e.g. a style loaded with [`Map::from_style`] on another task, swapped in
    /// under your own layers. Returns the ids of the layers added. Nothing changes if a source
    /// or layer id is already taken.
    pub fn add_map(&mut self, other: Map, before: Option<&str>) -> Result<Vec<String>> {
        for (id, _) in &other.sources {
            if self.source(id).is_some() {
                bail!("source \"{id}\" already exists");
            }
        }
        for e in &other.layers {
            if self.index(e.id()).is_some() {
                bail!("layer \"{}\" already exists", e.id());
            }
        }
        let at = match before {
            Some(b) => self.index(b).ok_or_else(|| anyhow!("no layer \"{b}\" to add before"))?,
            None => self.layers.len(),
        };
        if self.layers.len() + other.layers.len() >= u16::MAX as usize {
            bail!("too many layers");
        }
        let ids: Vec<String> = other.layers.iter().map(|e| e.id().to_string()).collect();
        self.sources.extend(other.sources);
        self.layers.splice(at..at, other.layers);
        self.warnings.extend(other.warnings);
        self.restyle();
        Ok(ids)
    }

    /// Remove a source; layers that use it stay but draw nothing.
    pub fn remove_source(&mut self, id: &str) -> bool {
        let n = self.sources.len();
        self.sources.retain(|(s, _)| s != id);
        n != self.sources.len()
    }

    /// Every source with its id, in the order added.
    pub fn sources(&self) -> impl Iterator<Item = (&str, &MapSource)> {
        self.sources.iter().map(|(id, s)| (id.as_str(), s))
    }

    /// A source by id.
    pub fn source(&self, id: &str) -> Option<&MapSource> {
        self.sources.iter().find(|(s, _)| s == id).map(|(_, s)| s)
    }

    /// Set a source's credit (plain text, or HTML that [`crate::source::plain_attribution`]
    /// turns into text); empty removes it. Raster sources keep theirs on the shared source.
    pub fn set_attribution(&mut self, id: &str, text: &str) -> Result<()> {
        let text = crate::source::plain_attribution(text);
        let value = (!text.is_empty()).then_some(text);
        match self.sources.iter_mut().find(|(s, _)| s == id) {
            Some((_, MapSource::Vector(v))) => v.attribution = value,
            Some((_, MapSource::GeoJson(g))) => g.attribution = value,
            Some((_, MapSource::Raster(r))) => match Arc::get_mut(&mut r.src) {
                Some(src) => src.attribution = value.unwrap_or_default(),
                None => bail!("raster source \"{id}\" is shared; set its attribution before adding it"),
            },
            None => bail!("no source \"{id}\""),
        }
        Ok(())
    }

    /// Set feature state (MapLibre's `setFeatureState`): the keys of `state` (a JSON object)
    /// are merged into the feature's state, which paint expressions read with
    /// `["feature-state", key]`. `id` is the feature's id, or its `promoteId` property.
    pub fn set_feature_state(&mut self, source: &str, id: &crate::mvt::Value, state: &Json) -> Result<()> {
        if self.source(source).is_none() {
            bail!("no source \"{source}\"");
        }
        let Some(obj) = state.as_object() else { bail!("feature state must be a JSON object") };
        let entry = self.states.entry(source.to_string()).or_default().entry(id.to_string()).or_default();
        for (k, v) in obj {
            let v = crate::expr::Expr::parse(&serde_json::json!(["literal", v])).map(|e| e.eval(&Ctx { zoom: 0.0, geom: GeomType::Unknown, props: &[], state: &[] }))?;
            match entry.iter_mut().find(|(key, _)| key == k) {
                Some(slot) => slot.1 = v,
                None => entry.push((k.clone(), v)),
            }
        }
        self.state_version += 1;
        Ok(())
    }

    /// Remove feature state: one key of one feature, all of one feature's state, or (with no
    /// id) all state of the source.
    pub fn remove_feature_state(&mut self, source: &str, id: Option<&crate::mvt::Value>, key: Option<&str>) {
        let Some(by_id) = self.states.get_mut(source) else { return };
        match (id, key) {
            (None, _) => {
                self.states.remove(source);
            }
            (Some(id), None) => {
                by_id.remove(&id.to_string());
            }
            (Some(id), Some(k)) => {
                if let Some(st) = by_id.get_mut(&id.to_string()) {
                    st.retain(|(key, _)| key != k);
                }
            }
        }
        self.state_version += 1;
    }

    /// A feature's state as a JSON object (empty when none is set).
    pub fn feature_state(&self, source: &str, id: &crate::mvt::Value) -> Json {
        let mut m = serde_json::Map::new();
        if let Some(st) = self.states.get(source).and_then(|s| s.get(&id.to_string())) {
            for (k, v) in st {
                m.insert(k.clone(), val_json(v));
            }
        }
        Json::Object(m)
    }

    fn state_env(&self, source: &str) -> StateEnv<'_> {
        StateEnv { states: self.states.get(source), promote: self.source(source).and_then(MapSource::promote_id), version: self.state_version }
    }

    /// The credits of the sources that the layers shown at `zoom` draw from, in source order
    /// and without repeats, as MapLibre's attribution control lists them.
    pub fn attributions(&self, zoom: f64) -> Vec<String> {
        let used: HashSet<&str> = self
            .layers
            .iter()
            .filter_map(|e| match e {
                Entry::Style(l) if l.shown_at(zoom) && l.kind != Kind::Background => l.source.as_deref(),
                _ => None,
            })
            .collect();
        let mut out: Vec<String> = Vec::new();
        for (id, s) in &self.sources {
            if let Some(a) = s.attribution().filter(|_| used.contains(id.as_str()))
                && !out.iter().any(|o| o == a)
            {
                out.push(a.to_string());
            }
        }
        out
    }

    /// The zoom at which a cluster of a clustered GeoJSON source splits into its parts
    /// (MapLibre's `getClusterExpansionZoom`): fly there when a cluster is clicked.
    pub fn cluster_expansion_zoom(&self, source: &str, cluster_id: u64) -> Option<f64> {
        match self.source(source)? {
            MapSource::GeoJson(g) => g.clusters.as_ref()?.expansion_zoom(cluster_id).map(f64::from),
            _ => None,
        }
    }

    /// Replace a GeoJSON source's data (MapLibre's `setData`).
    pub fn set_data(&mut self, id: &str, doc: &Json) -> Result<()> {
        let mut data = GeoJsonSource::parse(doc)?;
        match self.sources.iter_mut().find(|(s, _)| s == id) {
            Some((_, MapSource::GeoJson(g))) => {
                data.attribution = g.attribution.take();
                data.promote_id = g.promote_id.take();
                data.set_cluster(g.cluster_options());
                *g = data;
            }
            Some(_) => bail!("source \"{id}\" is not a GeoJSON source"),
            None => bail!("no source \"{id}\""),
        }
        Ok(())
    }

    /// Lon/lat bounds (west, south, east, north) of a GeoJSON source, for fitting the view.
    pub fn source_bounds(&self, id: &str) -> Option<(f64, f64, f64, f64)> {
        match self.source(id)? {
            MapSource::GeoJson(g) if !g.features().is_empty() => {
                let (w, n) = to_lonlat(g.bbox[0], g.bbox[1]);
                let (e, s) = to_lonlat(g.bbox[2], g.bbox[3]);
                Some((w, s, e, n))
            }
            _ => None,
        }
    }

    /// Tiles the view needs that aren't loaded yet, for sources that shown layers use and the
    /// map can fetch itself (vector sources from [`MapSource::vector`], raster sources).
    pub fn tile_requests(&self, cam: &Camera, area: Rect) -> Vec<TileRequest> {
        let vp = Viewport::new(cam, area);
        let used: HashSet<&str> = self
            .layers
            .iter()
            .filter_map(|e| match e {
                Entry::Style(l) if l.shown_at(cam.zoom) => l.source.as_deref(),
                _ => None,
            })
            .collect();
        let mut out = Vec::new();
        for (id, src) in &self.sources {
            if !used.contains(id.as_str()) {
                continue;
            }
            match src {
                MapSource::Vector(v) => {
                    let Some(f) = &v.fetch else { continue };
                    let z = cam.tile_zoom(v.min_zoom, v.max_zoom);
                    out.extend(vp.tiles(z).into_iter().filter(|t| !v.tiles.map.contains_key(t) && self.due(id, t)).map(|t| TileRequest {
                        source: id.clone(),
                        id: t,
                        fetch: Fetch::Vector(f.clone()),
                    }));
                }
                MapSource::Raster(r) => {
                    let rz = self.raster_zoom(&r.src, cam);
                    out.extend(crate::raster::tiles_for(cam, area, rz).into_iter().filter(|t| !r.tiles.contains(t) && self.due(id, t)).map(|t| TileRequest {
                        source: id.clone(),
                        id: t,
                        fetch: Fetch::Raster(r.src.clone()),
                    }));
                }
                MapSource::GeoJson(_) => {}
            }
        }
        out
    }

    /// The zoom to fetch a raster source at: about 2 screen pixels per image pixel as kitty
    /// images (like a browser on a Retina screen), one cell per image pixel as cell colors.
    fn raster_zoom(&self, src: &RasterSource, cam: &Camera) -> u8 {
        let dots_per_px = match self.raster_mode {
            RasterMode::Kitty { cell_px } => 2.0 / (cell_px.0 / 2.0).max(0.5),
            RasterMode::Cells => 2.0,
        };
        crate::raster::raster_zoom(cam, src.tile_px, dots_per_px, src.max_zoom)
    }

    /// Store a tile loaded with [`TileRequest::load`]. A failed load returns its error and the
    /// tile is requested again after a growing wait ([`Map::retry`]); after the last attempt
    /// it is stored empty. Lower-zoom tiles stand in until then.
    pub fn insert_loaded(&mut self, source: &str, id: TileId, loaded: Result<Loaded>) -> Result<()> {
        let is_raster = matches!(self.source(source), Some(MapSource::Raster(_)));
        let key = (source.to_string(), id);
        let (tile, err) = match loaded {
            Ok(Loaded(t)) => {
                self.failed.remove(&key);
                (t, None)
            }
            Err(e) => {
                let e = e.context(format!("{source} tile {}/{}/{}", id.0, id.1, id.2));
                let attempts = self.failed.get(&key).map_or(0, |f| f.0) + 1;
                if attempts < self.retry.attempts {
                    // Ask again later; lower-zoom tiles stand in meanwhile.
                    let wait = self.retry.base.saturating_mul(1 << (attempts - 1).min(16)).min(self.retry.max);
                    self.failed.insert(key, (attempts, std::time::Instant::now() + wait));
                    return Err(e);
                }
                // Given up: store it empty so it isn't requested again and again.
                self.failed.remove(&key);
                (if is_raster { LoadedTile::Raster(None) } else { LoadedTile::Vector(Vec::new()) }, Some(e))
            }
        };
        match tile {
            LoadedTile::Vector(features) => self.insert_features(source, id, features)?,
            LoadedTile::Raster(t) => match self.sources.iter_mut().find(|(s, _)| s == source) {
                Some((_, MapSource::Raster(r))) => r.tiles.insert(id, t),
                _ => bail!("no raster source \"{source}\""),
            },
        }
        err.map_or(Ok(()), Err)
    }

    /// Whether a tile may be requested now (it hasn't failed recently).
    fn due(&self, source: &str, id: &TileId) -> bool {
        self.failed.get(&(source.to_string(), *id)).is_none_or(|(_, at)| std::time::Instant::now() >= *at)
    }

    /// Fetch the missing tiles one at a time (simple apps; see [`Map::tile_requests`] to load
    /// in the background). Failed tiles are retried later (see [`Map::retry`]) and the first
    /// error is returned.
    pub async fn load_tiles(&mut self, cam: &Camera, area: Rect) -> Result<()> {
        let mut first_err = None;
        for req in self.tile_requests(cam, area) {
            let loaded = req.load().await;
            if let Err(e) = self.insert_loaded(&req.source, req.id, loaded) {
                first_err.get_or_insert(e);
            }
        }
        first_err.map_or(Ok(()), Err)
    }

    /// The zoom range of the vector sources (min of minimums, max of maximums).
    pub fn zoom_range(&self) -> (u8, u8) {
        self.sources
            .iter()
            .filter_map(|(_, s)| match s {
                MapSource::Vector(v) => Some((v.min_zoom, v.max_zoom)),
                _ => None,
            })
            .reduce(|a, b| (a.0.min(b.0), a.1.max(b.1)))
            .unwrap_or((0, 14))
    }

    /// Kitty graphics commands that place the shown raster layers under the text for this
    /// frame (images are sent once, then only moved). Write them after the frame's cells, in
    /// the same synchronized update. Empty unless [`Map::raster_mode`] is kitty.
    pub fn kitty_commands(&mut self, cam: &Camera, area: Rect, state: &mut KittyState) -> String {
        use crate::kitty;
        let RasterMode::Kitty { .. } = self.raster_mode else { return String::new() };
        state.frame += 1;
        let mut cmds = String::new();
        // Images the store dropped are freed from the terminal too.
        for (_, s) in &mut self.sources {
            if let MapSource::Raster(r) = s {
                for id in r.tiles.take_evicted() {
                    if state.sent.remove(&id).is_some() {
                        cmds.push_str(&kitty::free(id));
                    }
                }
            }
        }
        // This frame's pieces, bottom layer first. Pieces no bigger than 32x16 cells, which
        // herdr relays (it drops placements stretched over very large areas).
        let mut pieces = Vec::new();
        for (i, e) in self.layers.iter().enumerate() {
            let Entry::Style(l) = e else { continue };
            if l.kind != Kind::Raster || !l.shown_at(cam.zoom) {
                continue;
            }
            let Some(MapSource::Raster(r)) = l.source.as_deref().and_then(|s| self.source(s)) else { continue };
            let rz = self.raster_zoom(&r.src, cam);
            pieces.extend(crate::raster::layout_max(&r.tiles, cam, area, rz, (32, 16)).into_iter().map(|b| (i, b)));
        }
        if pieces.is_empty() && state.sent.is_empty() {
            return cmds;
        }
        // Stay under the budget before sending anything new: free the least recently used
        // images this frame doesn't show, so the terminal never evicts one it does.
        let needed: HashSet<u32> = pieces.iter().map(|(_, b)| b.tile.id).collect();
        let mut new_bytes = 0;
        let mut counted = HashSet::new();
        for (_, b) in &pieces {
            if !state.sent.contains_key(&b.tile.id) && counted.insert(b.tile.id) {
                let (w, h) = b.tile.size();
                new_bytes += w as usize * h as usize * 4;
            }
        }
        let mut total = state.bytes();
        if total + new_bytes > state.budget {
            let mut old: Vec<(u64, u32, usize)> =
                state.sent.iter().filter(|(id, _)| !needed.contains(id)).map(|(id, (b, f))| (*f, *id, *b)).collect();
            old.sort_unstable();
            for (_, id, b) in old {
                if total + new_bytes <= state.budget {
                    break;
                }
                state.sent.remove(&id);
                cmds.push_str(&kitty::free(id));
                total -= b;
            }
        }
        cmds.push_str(kitty::CLEAR_PLACEMENTS);
        // Higher layers stack above lower ones, all under the text.
        for (placement, (i, b)) in pieces.iter().enumerate() {
            let (w, h) = b.tile.size();
            let frame = state.frame;
            match state.sent.get_mut(&b.tile.id) {
                Some(entry) => entry.1 = frame,
                None => {
                    state.sent.insert(b.tile.id, (w as usize * h as usize * 4, frame));
                    cmds.push_str(&kitty::transmit(b.tile.id, &b.tile.png));
                }
            }
            cmds.push_str(&kitty::place(b.tile.id, placement as u32 + 1, b.cells.x, b.cells.y, b.src, b.cells.width, b.cells.height, -1000 + *i as i32));
        }
        cmds
    }

    /// Add a vector tile (MVT bytes, or `None` for an empty tile) to a vector source.
    pub fn insert_tile(&mut self, source: &str, id: TileId, bytes: Option<&[u8]>) -> Result<()> {
        let features = match bytes {
            Some(b) => store::decode_features(b),
            None => Ok(Vec::new()),
        };
        let (features, err) = match features {
            Ok(f) => (f, None),
            Err(e) => (Vec::new(), Some(e)),
        };
        self.insert_features(source, id, features)?;
        err.map_or(Ok(()), |e| Err(e.context(format!("tile {}/{}/{}", id.0, id.1, id.2))))
    }

    fn insert_features(&mut self, source: &str, id: TileId, features: Vec<store::Feature>) -> Result<()> {
        let features = Arc::new(features);
        let items = style_tile(&self.layers, source, &features, id.0 as f64);
        match self.sources.iter_mut().find(|(s, _)| s == source) {
            Some((_, MapSource::Vector(v))) => v.tiles.insert(id, MapTile::new(features, items)),
            _ => bail!("no vector source \"{source}\""),
        }
        Ok(())
    }

    // ---- layers ----

    /// Add a style layer on top.
    pub fn add_layer(&mut self, layer: StyleLayer) -> Result<()> {
        self.insert_entry(Entry::Style(layer), None)
    }

    /// Add a style layer just below `before`.
    pub fn add_layer_before(&mut self, layer: StyleLayer, before: &str) -> Result<()> {
        self.insert_entry(Entry::Style(layer), Some(before))
    }

    /// Add a custom layer (see [`CustomLayer`]) on top, or below `before`.
    pub fn add_custom_layer<T: CustomLayer + Any + Send>(&mut self, id: impl Into<String>, layer: T, before: Option<&str>) -> Result<()> {
        self.insert_entry(Entry::Custom { id: id.into(), layer: Box::new(layer) }, before)
    }

    /// Add several style layers at once (on top, or below `before`), restyling loaded tiles
    /// only once. Nothing is added if any id is taken.
    pub fn add_layers(&mut self, layers: Vec<StyleLayer>, before: Option<&str>) -> Result<()> {
        let mut ids = HashSet::new();
        for l in &layers {
            if self.index(&l.id).is_some() || !ids.insert(l.id.as_str()) {
                bail!("layer \"{}\" already exists", l.id);
            }
        }
        if self.layers.len() + layers.len() >= u16::MAX as usize {
            bail!("too many layers");
        }
        let at = match before {
            Some(b) => self.index(b).ok_or_else(|| anyhow!("no layer \"{b}\" to add before"))?,
            None => self.layers.len(),
        };
        self.layers.splice(at..at, layers.into_iter().map(Entry::Style));
        self.restyle();
        Ok(())
    }

    /// Remove several layers at once, restyling only once. Returns how many were removed.
    pub fn remove_layers(&mut self, ids: &[&str]) -> usize {
        let n = self.layers.len();
        self.layers.retain(|e| !ids.contains(&e.id()));
        let removed = n - self.layers.len();
        if removed > 0 {
            self.restyle();
        }
        removed
    }

    fn insert_entry(&mut self, e: Entry, before: Option<&str>) -> Result<()> {
        if self.index(e.id()).is_some() {
            bail!("layer \"{}\" already exists", e.id());
        }
        if self.layers.len() >= u16::MAX as usize {
            bail!("too many layers");
        }
        let at = match before {
            Some(b) => self.index(b).ok_or_else(|| anyhow!("no layer \"{b}\" to add before"))?,
            None => self.layers.len(),
        };
        self.layers.insert(at, e);
        self.restyle();
        Ok(())
    }

    /// Remove a layer; false if there was none.
    pub fn remove_layer(&mut self, id: &str) -> bool {
        let Some(i) = self.index(id) else { return false };
        self.layers.remove(i);
        self.restyle();
        true
    }

    /// Move a layer below `before`, or to the top.
    pub fn move_layer(&mut self, id: &str, before: Option<&str>) -> Result<()> {
        let i = self.index(id).ok_or_else(|| anyhow!("no layer \"{id}\""))?;
        let e = self.layers.remove(i);
        let at = match before {
            Some(b) => match self.index(b) {
                Some(at) => at,
                None => {
                    self.layers.insert(i, e);
                    bail!("no layer \"{b}\"");
                }
            },
            None => self.layers.len(),
        };
        self.layers.insert(at, e);
        self.restyle();
        Ok(())
    }

    /// Layer ids, bottom first.
    pub fn layer_ids(&self) -> Vec<&str> {
        self.layers.iter().map(Entry::id).collect()
    }

    /// A style layer by id (`None` for custom layers).
    pub fn layer(&self, id: &str) -> Option<&StyleLayer> {
        match &self.layers[self.index(id)?] {
            Entry::Style(l) => Some(l),
            Entry::Custom { .. } => None,
        }
    }

    /// A custom layer by id and type, to change its state.
    pub fn custom_layer_mut<T: CustomLayer + Any + Send>(&mut self, id: &str) -> Option<&mut T> {
        let i = self.index(id)?;
        match &mut self.layers[i] {
            Entry::Custom { layer, .. } => (&mut **layer as &mut dyn Any).downcast_mut::<T>(),
            Entry::Style(_) => None,
        }
    }

    /// Set a paint property of a style layer (MapLibre's `setPaintProperty`).
    pub fn set_paint(&mut self, id: &str, name: &str, v: &Json) -> Result<()> {
        self.style_mut(id)?.set_paint(name, v)?;
        self.restyle();
        Ok(())
    }

    /// Set a layout property of a style layer (MapLibre's `setLayoutProperty`).
    pub fn set_layout(&mut self, id: &str, name: &str, v: &Json) -> Result<()> {
        self.style_mut(id)?.set_layout(name, v)?;
        self.restyle();
        Ok(())
    }

    /// Replace a layer's filter (`None` draws every feature).
    pub fn set_filter(&mut self, id: &str, f: Option<&Json>) -> Result<()> {
        let e = f.map(crate::expr::Expr::parse).transpose()?;
        self.style_mut(id)?.filter = e;
        self.restyle();
        Ok(())
    }

    /// Show or hide a layer.
    pub fn set_visibility(&mut self, id: &str, visible: bool) -> Result<()> {
        self.style_mut(id)?.visible = visible;
        Ok(())
    }

    /// Draw a layer only from `min` up to (not including) `max` zoom.
    pub fn set_zoom_range(&mut self, id: &str, min: f64, max: f64) -> Result<()> {
        let l = self.style_mut(id)?;
        (l.min_zoom, l.max_zoom) = (min, max);
        Ok(())
    }

    /// Problems met while building the style (unsupported expressions and the like).
    pub fn warnings(&self) -> &[String] {
        &self.warnings
    }

    pub(crate) fn warn(&mut self, w: String) {
        self.warnings.push(w);
    }

    fn index(&self, id: &str) -> Option<usize> {
        self.layers.iter().position(|e| e.id() == id)
    }

    fn style_mut(&mut self, id: &str) -> Result<&mut StyleLayer> {
        let i = self.index(id).ok_or_else(|| anyhow!("no layer \"{id}\""))?;
        match &mut self.layers[i] {
            Entry::Style(l) => Ok(l),
            Entry::Custom { .. } => bail!("\"{id}\" is a custom layer"),
        }
    }

    /// Re-evaluate the style for every loaded vector tile.
    fn restyle(&mut self) {
        let layers = &self.layers;
        for (sid, src) in &mut self.sources {
            if let MapSource::Vector(v) = src {
                let ids: Vec<TileId> = v.tiles.map.keys().copied().collect();
                for id in ids {
                    let features = v.tiles.map[&id].features.clone();
                    let items = style_tile(layers, sid, &features, id.0 as f64);
                    v.tiles.map.insert(id, Arc::new(MapTile::new(features, items)));
                }
            }
        }
    }

    // ---- drawing and querying ----

    /// A widget that draws the map with this camera.
    pub fn view<'a>(&'a self, camera: &'a Camera) -> MapView<'a> {
        MapView { map: self, camera }
    }

    /// Features drawn under `target` (braille dots relative to `area`: 2 per column, 4 per
    /// row), top layer first and, within a layer, points before lines before polygons.
    /// `layers` limits the search to those layer ids. Custom layers answer with their `pick`.
    pub fn query_rendered_features(&self, cam: &Camera, area: Rect, target: (f64, f64), layers: Option<&[&str]>) -> Vec<Pick> {
        let (w, h) = (area.width as f64 * 2.0, area.height as f64 * 4.0);
        if target.0 < 0.0 || target.1 < 0.0 || target.0 >= w || target.1 >= h {
            return Vec::new();
        }
        let vp = Viewport::new(cam, area);
        let bg = self.background(cam.zoom);
        let mut out = Vec::new();
        for (i, entry) in self.layers.iter().enumerate().rev() {
            if layers.is_some_and(|ids| !ids.contains(&entry.id())) {
                continue;
            }
            let l = match entry {
                Entry::Style(l) => l,
                Entry::Custom { id, layer } => {
                    let ctx = PickContext { camera: cam, area, viewport: vp, target };
                    for mut p in layer.pick(&ctx) {
                        p.style_layer.get_or_insert_with(|| Arc::from(id.as_str()));
                        out.push(p);
                    }
                    continue;
                }
            };
            if !l.shown_at(cam.zoom) || matches!(l.kind, Kind::Background | Kind::Raster | Kind::Other(_)) {
                continue;
            }
            let Some(sid) = l.source.as_deref() else { continue };
            let env = self.state_env(sid);
            let style_layer: Arc<str> = Arc::from(l.id.as_str());
            let source: Arc<str> = Arc::from(sid);
            let edges = l.kind == Kind::Line;
            let mut hits: Vec<(u8, f64, Pick)> = Vec::new();
            let mut push = |geom: GeomType, dots: &[Vec<(f64, f64)>], lonlat: Vec<Vec<(f64, f64)>>, layer: Arc<str>, props: &[(String, crate::mvt::Value)], tile: Option<TileId>, id: Option<crate::mvt::Value>| {
                if let Some((rank, score)) = hit(geom, dots, target, edges) {
                    let st = stats(geom, &lonlat);
                    let score = if geom == GeomType::Polygon { st.area_km2.unwrap_or(0.0) } else { score };
                    let pick = Pick {
                        tile,
                        layer,
                        geom,
                        props: props.to_vec(),
                        lonlat,
                        drawn: true,
                        stats: st,
                        source: Some(source.clone()),
                        style_layer: Some(style_layer.clone()),
                        id,
                    };
                    hits.push((rank, score, pick));
                }
            };
            match self.source(sid) {
                Some(MapSource::Vector(v)) => {
                    let z = cam.tile_zoom(v.min_zoom, v.max_zoom);
                    let n = (1u64 << z) as f64;
                    let (wx, wy) = {
                        let (lon, lat) = vp.dot_to_lonlat(target.0, target.1);
                        to_world(lon, lat)
                    };
                    let id: TileId = (z, (wx * n).floor().clamp(0.0, n - 1.0) as u32, (wy * n).floor().clamp(0.0, n - 1.0) as u32);
                    let Some((tid, tile)) = v.tiles.get_or_ancestor(id) else { continue };
                    let tn = (1u64 << tid.0) as f64;
                    let world = |p: &[f32; 2]| ((tid.1 as f64 + p[0] as f64) / tn, (tid.2 as f64 + p[1] as f64) / tn);
                    let mut seen = HashSet::new();
                    for it in tile.layer_items(i, l, cam.zoom, bg, env).iter() {
                        let f = &tile.features[it.feature as usize];
                        let dots: Vec<Vec<(f64, f64)>> = f.parts.iter().map(|part| part.iter().map(|p| { let (x, y) = world(p); vp.dot(x, y) }).collect()).collect();
                        if hit(f.geom, &dots, target, edges).is_none() || !seen.insert(format!("{:?}{:?}", f.geom, f.props)) {
                            continue;
                        }
                        let lonlat = f.parts.iter().map(|part| part.iter().map(|p| { let (x, y) = world(p); to_lonlat(x, y) }).collect()).collect();
                        push(f.geom, &dots, lonlat, f.layer.clone(), &f.props, Some(tid), feature_id(env.promote, &f.id, &f.props));
                    }
                }
                Some(MapSource::GeoJson(g)) => {
                    for f in g.near(cam.zoom, near_box(&vp, target)) {
                        let c = Ctx { zoom: cam.zoom.floor(), geom: f.geom, props: &f.props, state: env.of(&f.id, &f.props) };
                        if resolve(l, &c, cam.zoom, bg).is_none() {
                            continue;
                        }
                        let dots: Vec<Vec<(f64, f64)>> = f.parts.iter().map(|p| p.iter().map(|&(x, y)| vp.dot(x, y)).collect()).collect();
                        let lonlat = f.parts.iter().map(|p| p.iter().map(|&(x, y)| to_lonlat(x, y)).collect()).collect();
                        push(f.geom, &dots, lonlat, source.clone(), &f.props, None, feature_id(env.promote, &f.id, &f.props));
                    }
                }
                _ => {}
            }
            hits.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1)));
            out.extend(hits.into_iter().map(|(_, _, p)| p));
        }
        out
    }

    /// Every feature of every source under `target` (dots relative to `area`), drawn or not,
    /// for inspecting data. Sources come in the order of their topmost layer, top first;
    /// within a source, points, then lines, then polygons from smallest to largest. Each pick's
    /// `style_layer` is the top shown layer that draws it, and `drawn` says whether one does.
    pub fn query_features(&self, cam: &Camera, area: Rect, target: (f64, f64)) -> Vec<Pick> {
        let (w, h) = (area.width as f64 * 2.0, area.height as f64 * 4.0);
        if target.0 < 0.0 || target.1 < 0.0 || target.0 >= w || target.1 >= h {
            return Vec::new();
        }
        let vp = Viewport::new(cam, area);
        let bg = self.background(cam.zoom);
        let top_layer = |sid: &str| {
            self.layers.iter().rposition(|e| matches!(e, Entry::Style(l) if l.source.as_deref() == Some(sid)))
        };
        let mut order: Vec<&(String, MapSource)> = self.sources.iter().collect();
        order.sort_by_key(|(id, _)| std::cmp::Reverse(top_layer(id).map_or(-1, |i| i as i64)));
        let mut out = Vec::new();
        for (sid, src) in order {
            let source: Arc<str> = Arc::from(sid.as_str());
            let env = self.state_env(sid);
            let mut hits: Vec<(u8, f64, Pick)> = Vec::new();
            let mut push = |geom: GeomType, rank: u8, score: f64, lonlat: Vec<Vec<(f64, f64)>>, layer: Arc<str>, props: &[(String, crate::mvt::Value)], tile: Option<TileId>, by: Option<usize>, id: Option<crate::mvt::Value>| {
                let st = stats(geom, &lonlat);
                let score = if geom == GeomType::Polygon { st.area_km2.unwrap_or(0.0) } else { score };
                let style_layer = by.map(|i| Arc::from(self.layers[i].id()));
                let pick = Pick { tile, layer, geom, props: props.to_vec(), lonlat, drawn: by.is_some(), stats: st, source: Some(source.clone()), style_layer, id };
                hits.push((rank, score, pick));
            };
            match src {
                MapSource::Vector(v) => {
                    let z = cam.tile_zoom(v.min_zoom, v.max_zoom);
                    let n = (1u64 << z) as f64;
                    let (wx, wy) = {
                        let (lon, lat) = vp.dot_to_lonlat(target.0, target.1);
                        to_world(lon, lat)
                    };
                    let id: TileId = (z, (wx * n).floor().clamp(0.0, n - 1.0) as u32, (wy * n).floor().clamp(0.0, n - 1.0) as u32);
                    let Some((tid, tile)) = v.tiles.get_or_ancestor(id) else { continue };
                    // The top shown layer drawing each feature.
                    let mut drawn_by: HashMap<u32, usize> = HashMap::new();
                    for (i, e) in self.layers.iter().enumerate() {
                        if let Entry::Style(l) = e
                            && l.source.as_deref() == Some(sid.as_str())
                            && l.shown_at(cam.zoom)
                        {
                            for it in tile.layer_items(i, l, cam.zoom, bg, env).iter() {
                                drawn_by.insert(it.feature, i);
                            }
                        }
                    }
                    let tn = (1u64 << tid.0) as f64;
                    let world = |p: &[f32; 2]| ((tid.1 as f64 + p[0] as f64) / tn, (tid.2 as f64 + p[1] as f64) / tn);
                    let mut seen = HashSet::new();
                    for (fi, f) in tile.features.iter().enumerate() {
                        let dots: Vec<Vec<(f64, f64)>> = f.parts.iter().map(|part| part.iter().map(|p| { let (x, y) = world(p); vp.dot(x, y) }).collect()).collect();
                        let Some((rank, score)) = hit(f.geom, &dots, target, false) else { continue };
                        // A feature cut by tile edges can appear more than once; keep one.
                        if !seen.insert(format!("{}|{:?}|{:?}", f.layer, f.geom, f.props)) {
                            continue;
                        }
                        let lonlat = f.parts.iter().map(|part| part.iter().map(|p| { let (x, y) = world(p); to_lonlat(x, y) }).collect()).collect();
                        push(f.geom, rank, score, lonlat, f.layer.clone(), &f.props, Some(tid), drawn_by.get(&(fi as u32)).copied(), feature_id(env.promote, &f.id, &f.props));
                    }
                }
                MapSource::GeoJson(g) => {
                    for f in g.near(cam.zoom, near_box(&vp, target)) {
                        let dots: Vec<Vec<(f64, f64)>> = f.parts.iter().map(|p| p.iter().map(|&(x, y)| vp.dot(x, y)).collect()).collect();
                        let Some((rank, score)) = hit(f.geom, &dots, target, true) else { continue };
                        let c = Ctx { zoom: cam.zoom.floor(), geom: f.geom, props: &f.props, state: env.of(&f.id, &f.props) };
                        let by = self.layers.iter().rposition(|e| {
                            matches!(e, Entry::Style(l) if l.source.as_deref() == Some(sid.as_str()) && l.shown_at(cam.zoom) && resolve(l, &c, cam.zoom, bg).is_some())
                        });
                        let lonlat = f.parts.iter().map(|p| p.iter().map(|&(x, y)| to_lonlat(x, y)).collect()).collect();
                        push(f.geom, rank, score, lonlat, source.clone(), &f.props, None, by, feature_id(env.promote, &f.id, &f.props));
                    }
                }
                MapSource::Raster(_) => {}
            }
            hits.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1)));
            out.extend(hits.into_iter().map(|(_, _, p)| p));
        }
        out
    }

    /// The color of the top shown background layer, used to blend translucent colors.
    fn background(&self, zoom: f64) -> Option<Rgba> {
        self.layers.iter().rev().find_map(|e| match e {
            Entry::Style(l) if l.kind == Kind::Background && l.shown_at(zoom) => {
                let c = Ctx { zoom, geom: GeomType::Unknown, props: &[], state: &[] };
                l.paint_expr("background-color").and_then(|e| e.eval(&c).as_color())
            }
            _ => None,
        })
    }
}

/// Evaluate every style layer that draws from `source` against a tile's features.
fn style_tile(layers: &[Entry], source: &str, features: &[store::Feature], zoom: f64) -> Vec<Item> {
    let mut by_layer: HashMap<&str, Vec<usize>> = HashMap::new();
    for (i, f) in features.iter().enumerate() {
        by_layer.entry(&f.layer).or_default().push(i);
    }
    let bg = layers.iter().rev().find_map(|e| match e {
        Entry::Style(l) if l.kind == Kind::Background && l.shown_at(zoom) => {
            l.paint_expr("background-color").and_then(|e| e.eval(&Ctx { zoom, geom: GeomType::Unknown, props: &[], state: &[] }).as_color())
        }
        _ => None,
    });
    let mut items = Vec::new();
    for (li, e) in layers.iter().enumerate() {
        let Entry::Style(l) = e else { continue };
        if l.source.as_deref() != Some(source) {
            continue;
        }
        // Layers whose style depends on the zoom or feature state are evaluated when drawn.
        if l.uses_zoom() || l.uses_state() {
            continue;
        }
        let Some(fs) = l.source_layer.as_deref().and_then(|s| by_layer.get(s)) else { continue };
        for &fi in fs {
            let f = &features[fi];
            if let Some(paint) = resolve(l, &Ctx { zoom, geom: f.geom, props: &f.props, state: &[] }, zoom, bg) {
                items.push(Item { layer: li as u16, feature: fi as u32, paint });
            }
        }
    }
    items
}

/// A color with its opacity, blended over the background when there is one. Nearly
/// transparent colors aren't drawn.
fn term_color(c: Rgba, opacity: f64, bg: Option<Rgba>) -> Option<Color> {
    let alpha = c.a * opacity;
    if alpha <= 0.05 {
        return None;
    }
    let c = match bg {
        Some(b) if alpha < 0.999 => c.over(b, alpha),
        _ => c,
    };
    let (r, g, b) = c.to_u8();
    Some(Color::Rgb(r, g, b))
}

const BLACK: Rgba = Rgba::rgb(0, 0, 0);

/// What one style layer draws for one feature, or `None`. The filter and layout use `c`
/// (its zoom is the whole zoom level); paint uses `paint_zoom`.
fn resolve(l: &StyleLayer, c: &Ctx, paint_zoom: f64, bg: Option<Rgba>) -> Option<Resolved> {
    if let Some(f) = &l.filter
        && !f.eval(c).as_bool()
    {
        return None;
    }
    let cp = Ctx { zoom: paint_zoom, geom: c.geom, props: c.props, state: c.state };
    let color = |prop: &str, opacity: &str, default: Option<Rgba>| -> Option<Color> {
        let rgba = l.paint_expr(prop).and_then(|e| e.eval(&cp).as_color()).or(default)?;
        let o = l.paint_expr(opacity).and_then(|e| e.eval(&cp).as_num()).unwrap_or(1.0);
        term_color(rgba, o, bg)
    };
    let layout_str = |name: &str| l.layout_expr(name).map(|e| e.eval(c).to_text()).filter(|s| !s.is_empty());
    let sort = || {
        let k = l.layout_expr("symbol-sort-key").and_then(|e| e.eval(c).as_num()).unwrap_or(0.0);
        LAYER_PRIORITY / 2 - (k.round() as i32).clamp(-(LAYER_PRIORITY / 2 - 1), LAYER_PRIORITY / 2 - 1)
    };
    match l.kind {
        Kind::Fill if c.geom == GeomType::Polygon => {
            let fill = color("fill-color", "fill-opacity", Some(BLACK));
            let outline = color("fill-outline-color", "fill-opacity", None);
            (fill.is_some() || outline.is_some()).then_some(Resolved::Fill { color: fill, outline })
        }
        Kind::Line if c.geom != GeomType::Point => {
            let color = color("line-color", "line-opacity", Some(BLACK))?;
            // Pixels to dots: a braille dot is about 4 px, so thin lines stay one dot and wide
            // roads get two or three (no wider: braille can't show an outline around a fill).
            let px = l.paint_expr("line-width").and_then(|e| e.eval(&cp).as_num()).unwrap_or(1.0);
            if px <= 0.0 {
                return None;
            }
            let width = if px < 6.0 { 1 } else if px < 14.0 { 2 } else { 3 };
            // Dash lengths are in line widths; at least a dot each so the pattern shows. All
            // gaps zero means a solid line.
            let dash = match l.paint_expr("line-dasharray").map(|e| e.eval(&cp)) {
                Some(crate::expr::Val::Array(a)) => {
                    let v: Vec<f64> = a.iter().filter_map(|x| x.as_num()).map(|n| (n * px.max(1.0) / 4.0).max(1.0)).collect();
                    let gaps = a.iter().skip(1).step_by(2).any(|x| x.as_num().is_some_and(|n| n > 0.0));
                    (v.len() >= 2 && gaps).then_some(v)
                }
                _ => None,
            };
            Some(Resolved::Line { color, width, dash })
        }
        Kind::Circle if c.geom == GeomType::Point => {
            let fg = color("circle-color", "circle-opacity", Some(BLACK))?;
            let glyph = layout_str("mapterm:glyph").unwrap_or_else(|| "●".into());
            Some(Resolved::Point { glyph, style: TStyle::new().fg(fg), label: None, priority: sort() })
        }
        Kind::Symbol => {
            let fg = color("text-color", "text-opacity", Some(BLACK))?;
            let mut style = TStyle::new().fg(fg);
            let font = l.layout_expr("text-font").map(|e| e.eval(c).to_text().to_lowercase()).unwrap_or_default();
            if font.contains("bold") {
                style = style.add_modifier(Modifier::BOLD);
            }
            if font.contains("italic") {
                style = style.add_modifier(Modifier::ITALIC);
            }
            // Terminal labels are one line: the first non-empty line of the text.
            let text = layout_str("text-field")
                .and_then(|t| t.lines().map(str::trim).find(|s| !s.is_empty()).map(str::to_string))
                .map(|t| match layout_str("text-transform").as_deref() {
                    Some("uppercase") => t.to_uppercase(),
                    Some("lowercase") => t.to_lowercase(),
                    _ => t,
                });
            // A point's glyph: `mapterm:glyph`, else the style's icon as a one-cell symbol.
            let glyph = layout_str("mapterm:glyph").or_else(|| {
                (c.geom == GeomType::Point).then(|| layout_str("icon-image")).flatten().and_then(|n| crate::icons::glyph(&n)).map(str::to_string)
            });
            let priority = sort();
            match (glyph, text) {
                (Some(glyph), text) if c.geom == GeomType::Point => {
                    let icon = color("icon-color", "icon-opacity", None).map_or(style, |c| TStyle::new().fg(c));
                    Some(Resolved::Point { glyph, style: icon, label: text.map(|t| (t, style)), priority })
                }
                (_, Some(text)) => {
                    // Where the text sits relative to its point: text-variable-anchor (tried
                    // in order), else text-anchor.
                    let variable: Vec<(i8, i8)> = match l.layout_expr("text-variable-anchor").map(|e| e.eval(c)) {
                        Some(crate::expr::Val::Array(a)) => a.iter().map(|v| anchor_of(&v.to_text())).collect(),
                        _ => Vec::new(),
                    };
                    let anchors = if variable.is_empty() { vec![anchor_of(layout_str("text-anchor").as_deref().unwrap_or("center"))] } else { variable };
                    // Ems to dots: an em is about two columns wide (4 dots) and a row tall.
                    let offset = match l.layout_expr("text-offset").map(|e| e.eval(c)) {
                        Some(crate::expr::Val::Array(a)) if a.len() == 2 => (a[0].as_num().unwrap_or(0.0) * 4.0, a[1].as_num().unwrap_or(0.0) * 4.0),
                        _ => (0.0, 0.0),
                    };
                    // CSS pixels to dots: a dot is about 4 px.
                    let spacing = (l.layout_expr("symbol-spacing").and_then(|e| e.eval(c).as_num()).unwrap_or(250.0) / 4.0).max(8.0);
                    let along_line = c.geom != GeomType::Point && layout_str("symbol-placement").is_some_and(|p| p.starts_with("line"));
                    Some(Resolved::Text { text, style, anchors, offset, along_line, spacing, priority })
                }
                _ => None,
            }
        }
        _ => None,
    }
}

/// Draw one resolved feature whose parts are already in dots.
fn draw_resolved(ctx: &mut DrawContext, r: &Resolved, geom: GeomType, dots: &[Vec<(f64, f64)>]) {
    match r {
        Resolved::Fill { color, outline } => {
            if let Some(c) = color {
                ctx.fill_dots(dots, *c);
            }
            if let Some(c) = outline {
                for ring in dots {
                    ctx.line_dots(ring, *c);
                }
            }
        }
        Resolved::Line { color, width, dash } => {
            for part in dots {
                ctx.line_styled(part, *color, *width, dash.as_deref());
            }
        }
        Resolved::Point { glyph, style, label, priority } => {
            for &p in dots.iter().flatten() {
                ctx.marker_dot(p, glyph, *style, *priority, label.clone());
            }
        }
        Resolved::Text { text, style, anchors, offset, along_line, spacing, priority } => {
            // Candidate positions around a point, one per anchor: half the text's width in
            // dots (2 per column) plus a column of room sideways, a row up or down.
            let place = |p: (f64, f64)| -> Vec<(f64, f64)> {
                let half = text.width() as f64 + 2.0;
                anchors.iter().map(|a| (p.0 + a.0 as f64 * half + offset.0, p.1 + a.1 as f64 * 4.0 + offset.1)).collect()
            };
            if geom == GeomType::Point {
                for &p in dots.iter().flatten() {
                    ctx.label_dots(place(p), text, *style, *priority);
                }
            } else if *along_line || geom == GeomType::Line {
                // Repeat along the line every `spacing`, measured from the start of each part
                // at the whole zoom level, so labels stay put while the map pans and zooms
                // within a level (MapLibre's symbol-spacing). Short parts get one, mid-way.
                // The same name keeps most of a spacing apart, so a road made of many short
                // pieces is still labelled about once per spacing.
                let scale = 2f64.powf(ctx.camera.zoom - ctx.camera.zoom.floor());
                let repeat = (spacing * scale * 0.8).max(crate::layer::REPEAT_DOTS);
                for part in dots {
                    for p in along(part, spacing * scale) {
                        if ctx.inside(p) {
                            ctx.label_spaced(place(p), text, *style, *priority, repeat);
                        }
                    }
                }
            } else {
                // Polygons: the middle of the outer ring's bounding box.
                if let Some(ring) = dots.first().filter(|r| !r.is_empty()) {
                    let (mut x0, mut y0, mut x1, mut y1) = (f64::MAX, f64::MAX, f64::MIN, f64::MIN);
                    for &(x, y) in ring {
                        (x0, y0, x1, y1) = (x0.min(x), y0.min(y), x1.max(x), y1.max(y));
                    }
                    ctx.label_dots(place(((x0 + x1) / 2.0, (y0 + y1) / 2.0)), text, *style, *priority);
                }
            }
        }
    }
}

/// A `text-anchor` name as a direction: (-1 left .. 1 right, -1 up .. 1 down) from the
/// point to the text's center.
fn anchor_of(name: &str) -> (i8, i8) {
    match name {
        "left" => (1, 0),
        "right" => (-1, 0),
        "top" => (0, 1),
        "bottom" => (0, -1),
        "top-left" => (1, 1),
        "top-right" => (-1, 1),
        "bottom-left" => (1, -1),
        "bottom-right" => (-1, -1),
        _ => (0, 0),
    }
}

/// Points every `spacing` along a polyline, starting half a spacing in; a line shorter than
/// one spacing gets its middle.
fn along(part: &[(f64, f64)], spacing: f64) -> Vec<(f64, f64)> {
    let lens: Vec<f64> = part.windows(2).map(|w| (w[1].0 - w[0].0).hypot(w[1].1 - w[0].1)).collect();
    let total: f64 = lens.iter().sum();
    if total <= 0.0 || part.len() < 2 {
        return part.first().copied().into_iter().collect();
    }
    let mut targets = Vec::new();
    if total < spacing {
        targets.push(total / 2.0);
    } else {
        let mut s = spacing / 2.0;
        while s < total {
            targets.push(s);
            s += spacing;
        }
    }
    let mut out = Vec::with_capacity(targets.len());
    let (mut seg, mut start) = (0usize, 0.0);
    for t in targets {
        while seg < lens.len() - 1 && start + lens[seg] < t {
            start += lens[seg];
            seg += 1;
        }
        let k = if lens[seg] > 0.0 { ((t - start) / lens[seg]).clamp(0.0, 1.0) } else { 0.0 };
        let (a, b) = (part[seg], part[seg + 1]);
        out.push((a.0 + (b.0 - a.0) * k, a.1 + (b.1 - a.1) * k));
    }
    out
}

/// A box (world coordinates) around a spot in dots, wide enough for the hit tolerances.
fn near_box(vp: &Viewport, target: (f64, f64)) -> crate::index::BBox {
    let tol = 6.0;
    let (w, n) = vp.dot_to_lonlat(target.0 - tol, target.1 - tol);
    let (e, s) = vp.dot_to_lonlat(target.0 + tol, target.1 + tol);
    let (x0, y0) = to_world(w, n);
    let (x1, y1) = to_world(e, s);
    [x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)]
}

/// An expression value as JSON.
fn val_json(v: &Val) -> Json {
    match v {
        Val::Null => Json::Null,
        Val::Bool(b) => Json::Bool(*b),
        Val::Num(n) => serde_json::Number::from_f64(*n).map_or(Json::Null, Json::Number),
        Val::Str(s) => Json::String(s.clone()),
        Val::Color(c) => Json::String(Val::Color(*c).to_text()),
        Val::Array(a) => Json::Array(a.iter().map(val_json).collect()),
    }
}

/// Draws a [`Map`] with a camera. Make one with [`Map::view`].
pub struct MapView<'a> {
    /// The map.
    pub map: &'a Map,
    /// The camera to draw with.
    pub camera: &'a Camera,
}

impl Widget for MapView<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let (map, cam) = (self.map, self.camera);
        let vp = Viewport::new(cam, area);
        let bg = map.background(cam.zoom);
        // Loaded tiles per vector source, looked up once per frame.
        let tiles: HashMap<&str, Vec<(TileId, Arc<MapTile>)>> = map
            .sources
            .iter()
            .filter_map(|(id, s)| match s {
                MapSource::Vector(v) => Some((id.as_str(), v.tiles.visible(&vp, cam.tile_zoom(v.min_zoom, v.max_zoom)))),
                _ => None,
            })
            .collect();
        render_with(cam, area, buf, |ctx| {
            for (i, entry) in map.layers.iter().enumerate() {
                ctx.base = i as i32 * LAYER_PRIORITY;
                let l = match entry {
                    Entry::Style(l) => l,
                    Entry::Custom { layer, .. } => {
                        layer.draw(ctx);
                        continue;
                    }
                };
                if !l.shown_at(cam.zoom) {
                    continue;
                }
                if l.kind == Kind::Background {
                    if let Some(c) = l.paint_expr("background-color").and_then(|e| e.eval(&Ctx { zoom: cam.zoom, geom: GeomType::Unknown, props: &[], state: &[] }).as_color())
                        && let Some(c) = term_color(c, l.paint_expr("background-opacity").and_then(|e| e.eval(&Ctx { zoom: cam.zoom, geom: GeomType::Unknown, props: &[], state: &[] }).as_num()).unwrap_or(1.0), None)
                    {
                        let (w, h) = (area.width as f64 * 2.0, area.height as f64 * 4.0);
                        ctx.fill_dots(&[vec![(0.0, 0.0), (w, 0.0), (w, h), (0.0, h), (0.0, 0.0)]], c);
                    }
                    continue;
                }
                let Some(sid) = l.source.as_deref() else { continue };
                let env = map.state_env(sid);
                match map.source(sid) {
                    Some(MapSource::Vector(_)) => {
                        for ((z, tx, ty), tile) in tiles.get(sid).into_iter().flatten() {
                            let n = (1u64 << z) as f64;
                            for it in tile.layer_items(i, l, cam.zoom, bg, env).iter() {
                                let f = &tile.features[it.feature as usize];
                                let dots: Vec<Vec<(f64, f64)>> = f
                                    .parts
                                    .iter()
                                    .map(|part| part.iter().map(|p| vp.dot((*tx as f64 + p[0] as f64) / n, (*ty as f64 + p[1] as f64) / n)).collect())
                                    .collect();
                                draw_resolved(ctx, &it.paint, f.geom, &dots);
                            }
                        }
                    }
                    Some(MapSource::GeoJson(g)) => {
                        for f in g.near(cam.zoom, ctx.world_view()) {
                            if !ctx.visible(f.bbox) {
                                continue;
                            }
                            let c = Ctx { zoom: cam.zoom.floor(), geom: f.geom, props: &f.props, state: env.of(&f.id, &f.props) };
                            let Some(r) = resolve(l, &c, cam.zoom, bg) else { continue };
                            let dots: Vec<Vec<(f64, f64)>> = f.parts.iter().map(|p| p.iter().map(|&(x, y)| vp.dot(x, y)).collect()).collect();
                            draw_resolved(ctx, &r, f.geom, &dots);
                        }
                    }
                    Some(MapSource::Raster(r)) if map.raster_mode == RasterMode::Cells => {
                        // raster-opacity: blended with the style's background (the terminal's
                        // own background color isn't known); nearly transparent isn't drawn.
                        let opacity = l
                            .paint_expr("raster-opacity")
                            .and_then(|e| e.eval(&Ctx { zoom: cam.zoom, geom: GeomType::Unknown, props: &[], state: &[] }).as_num())
                            .unwrap_or(1.0)
                            .clamp(0.0, 1.0);
                        if opacity <= 0.05 {
                            continue;
                        }
                        let blend = |c: Color| match (c, bg) {
                            (Color::Rgb(r, g, b), Some(under)) if opacity < 0.999 => {
                                let (r, g, b) = Rgba::rgb(r, g, b).over(under, opacity).to_u8();
                                Color::Rgb(r, g, b)
                            }
                            (c, _) => c,
                        };
                        let rz = map.raster_zoom(&r.src, cam);
                        for b in crate::raster::layout(&r.tiles, cam, area, rz) {
                            let (sx, sy, sw, sh) = b.src;
                            let (cw, ch) = (sw as f64 / b.cells.width as f64, sh as f64 / b.cells.height as f64);
                            for row in 0..b.cells.height {
                                for col in 0..b.cells.width {
                                    let (x, y) = (sx + (col as f64 * cw) as u32, sy + (row as f64 * ch) as u32);
                                    let c = b.tile.average(x, y, cw.ceil() as u32, ch.ceil() as u32);
                                    ctx.cell_bg(b.cells.x - area.x + col, b.cells.y - area.y + row, blend(c));
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::layer::DrawContext;
    use serde_json::json;

    fn sample() -> Map {
        let mut map = Map::new();
        map.add_source(
            "sites",
            MapSource::geojson(&json!({"type": "FeatureCollection", "features": [
                {"type": "Feature", "properties": {"name": "Box", "kind": "zone"},
                 "geometry": {"type": "Polygon", "coordinates": [[[-77.05, 38.88], [-77.02, 38.88], [-77.02, 38.90], [-77.05, 38.90], [-77.05, 38.88]]]}},
                {"type": "Feature", "properties": {"name": "Pin", "kind": "site"},
                 "geometry": {"type": "Point", "coordinates": [-77.0353, 38.8895]}}
            ]}))
            .unwrap(),
        )
        .unwrap();
        map.add_layer(StyleLayer::background("bg").color("#000010")).unwrap();
        map.add_layer(StyleLayer::fill("zones", "sites").filter(json!(["==", ["get", "kind"], "zone"])).color("#204060")).unwrap();
        map.add_layer(StyleLayer::symbol("pins", "sites").filter(json!(["==", "kind", "site"])).glyph("◆").text(json!(["get", "name"])).color("#ff0")).unwrap();
        map
    }

    fn render(map: &Map, cam: &Camera, area: Rect) -> Buffer {
        let mut buf = Buffer::empty(area);
        map.view(cam).render(area, &mut buf);
        buf
    }

    #[test]
    fn draws_geojson_through_style_layers() {
        let map = sample();
        let cam = Camera::new(-77.0353, 38.8895, 15.0);
        let area = Rect::new(0, 0, 60, 20);
        let buf = render(&map, &cam, area);
        let text: String = (0..area.height).map(|y| (0..area.width).map(|x| buf[(x, y)].symbol().to_string()).collect::<String>()).collect::<Vec<_>>().join("\n");
        assert!(text.contains("◆ Pin"), "{text}");
        assert_eq!(buf[(30, 10)].symbol(), "◆");
        assert_eq!(buf[(30, 10)].fg, Color::Rgb(255, 255, 0));
        assert_eq!(buf[(2, 2)].bg, Color::Rgb(0x20, 0x40, 0x60), "inside the zone");
        // Hide the zones: the background shows instead.
        let mut map = map;
        map.set_visibility("zones", false).unwrap();
        assert_eq!(render(&map, &cam, area)[(2, 2)].bg, Color::Rgb(0, 0, 0x10));
    }

    #[test]
    fn queries_by_layer_top_first() {
        let map = sample();
        let cam = Camera::new(-77.0353, 38.8895, 15.0);
        let area = Rect::new(0, 0, 60, 20);
        let center = (60.0, 40.0);
        let ids: Vec<(String, String)> = map
            .query_rendered_features(&cam, area, center, None)
            .iter()
            .map(|p| (p.style_layer.as_deref().unwrap().to_string(), p.title()))
            .collect();
        assert_eq!(ids, vec![("pins".into(), "Pin".into()), ("zones".into(), "Box".into())]);
        let only = map.query_rendered_features(&cam, area, center, Some(&["zones"]));
        assert_eq!(only.len(), 1);
        assert_eq!(only[0].source.as_deref(), Some("sites"));
    }

    #[test]
    fn layer_order_and_set_data() {
        let mut map = sample();
        assert_eq!(map.layer_ids(), vec!["bg", "zones", "pins"]);
        map.move_layer("pins", Some("zones")).unwrap();
        assert_eq!(map.layer_ids(), vec!["bg", "pins", "zones"]);
        assert!(map.add_layer(StyleLayer::line("zones", "sites")).is_err(), "duplicate id");
        assert!(map.add_layer_before(StyleLayer::line("x", "sites"), "nope").is_err());
        map.set_data("sites", &json!({"type": "Point", "coordinates": [0, 0]})).unwrap();
        let (w, s, e, n) = map.source_bounds("sites").unwrap();
        assert!(w.abs() < 1e-9 && s.abs() < 1e-9 && e.abs() < 1e-9 && n.abs() < 1e-9);
        assert!(!map.remove_layer("x") && map.remove_layer("zones"));
    }

    struct Counter {
        hits: usize,
    }

    impl CustomLayer for Counter {
        fn name(&self) -> &str {
            "counter"
        }
        fn draw(&self, ctx: &mut DrawContext) {
            ctx.marker(-77.0353, 38.8895, "X", TStyle::new(), 0, None);
        }
    }

    #[test]
    fn custom_layers_draw_in_order_and_downcast() {
        let mut map = sample();
        map.add_custom_layer("count", Counter { hits: 0 }, None).unwrap();
        map.custom_layer_mut::<Counter>("count").unwrap().hits += 1;
        assert_eq!(map.custom_layer_mut::<Counter>("count").unwrap().hits, 1);
        assert!(map.custom_layer_mut::<Counter>("pins").is_none());
        // The custom layer is on top, so its marker wins the cell over the pin.
        let cam = Camera::new(-77.0353, 38.8895, 15.0);
        let buf = render(&map, &cam, Rect::new(0, 0, 60, 20));
        assert_eq!(buf[(30, 10)].symbol(), "X");
    }

    #[test]
    fn vector_tiles_are_styled_on_insert_and_restyled() {
        // A one-feature MVT: layer "water", a square polygon covering the tile.
        let mvt = crate::mvt::tests_support::square_tile("water", 4096);
        let mut map = Map::new();
        map.add_source("v", MapSource::vector_tiles(0, 14)).unwrap();
        map.add_layer(StyleLayer::fill("water", "v").source_layer("water").color("#0000ff")).unwrap();
        map.insert_tile("v", (0, 0, 0), Some(&mvt)).unwrap();
        let cam = Camera::new(0.0, 0.0, 0.0);
        let area = Rect::new(0, 0, 40, 20);
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Rgb(0, 0, 255));
        map.set_paint("water", "fill-color", &json!("#ff0000")).unwrap();
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Rgb(255, 0, 0));
        map.set_filter("water", Some(&json!(["==", "class", "lake"]))).unwrap();
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Reset);
        assert!(map.tile_requests(&cam, area).is_empty(), "app-supplied tiles are never requested");
    }

    fn raster_map() -> Map {
        let mut map = Map::new();
        let src = Arc::new(crate::raster::RasterSource::new("https://t.example/{z}/{x}/{y}.png", false, 19).unwrap());
        map.add_source("img", MapSource::raster(src)).unwrap();
        map.add_layer(StyleLayer::raster("img", "img")).unwrap();
        map
    }

    fn solid_tile(r: u8, g: u8, b: u8) -> RasterTile {
        let rgba = image::RgbaImage::from_pixel(256, 256, image::Rgba([r, g, b, 255]));
        RasterTile { id: 4242 + r as u32, png: vec![1, 2, 3], rgba }
    }

    #[test]
    fn raster_layers_draw_cells_or_kitty_images() {
        let mut map = raster_map();
        let cam = Camera::new(0.0, 0.0, 0.0);
        let area = Rect::new(0, 0, 40, 20);
        let reqs = map.tile_requests(&cam, area);
        assert_eq!(reqs.len(), 1);
        assert!(reqs[0].is_raster() && reqs[0].id == (0, 0, 0));
        map.insert_loaded("img", (0, 0, 0), Ok(Loaded(LoadedTile::Raster(Some(solid_tile(10, 200, 30)))))).unwrap();
        assert!(map.tile_requests(&cam, area).is_empty());
        // Cells: every cell takes the image color.
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Rgb(10, 200, 30));
        // Kitty: nothing in the cells; the image is sent once, then only placed.
        map.raster_mode = RasterMode::Kitty { cell_px: (10.0, 20.0) };
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Reset);
        let mut k = KittyState::default();
        let first = map.kitty_commands(&cam, area, &mut k);
        assert!(first.starts_with(crate::kitty::CLEAR_PLACEMENTS) && first.contains("a=t,f=100") && first.contains("z=-1000"));
        let second = map.kitty_commands(&cam, area, &mut k);
        assert!(!second.contains("a=t,") && second.contains("a=p,"));
        map.set_visibility("img", false).unwrap();
        assert!(!map.kitty_commands(&cam, area, &mut k).contains("a=p,"), "hidden layers aren't placed");
    }

    #[test]
    fn kitty_images_stay_under_the_budget() {
        let mut map = raster_map();
        map.raster_mode = RasterMode::Kitty { cell_px: (10.0, 20.0) };
        let area = Rect::new(0, 0, 40, 20);
        // Four zoom 1 tiles, each 256 KiB decoded; room for only two at a time.
        for (i, id) in [(1, 0, 0), (1, 1, 0), (1, 0, 1), (1, 1, 1)].into_iter().enumerate() {
            let mut t = solid_tile(10, 20, 30);
            t.id = 9000 + i as u32;
            map.insert_loaded("img", id, Ok(Loaded(LoadedTile::Raster(Some(t))))).unwrap();
        }
        let mut k = KittyState { budget: 2 * 256 * 1024, ..KittyState::default() };
        // Zoomed in on the north-west quarter: one tile shown, sent.
        let nw = Camera::new(-90.0, 45.0, 3.0);
        let c1 = map.kitty_commands(&nw, area, &mut k);
        assert_eq!(c1.matches("a=t,").count(), 1);
        // Pan to the north-east: a second image fits without freeing anything.
        let ne = Camera::new(90.0, 45.0, 3.0);
        let c2 = map.kitty_commands(&ne, area, &mut k);
        assert!(c2.matches("a=t,").count() == 1 && !c2.contains("a=d,d=I"));
        // A third doesn't fit: the least recently used (north-west, not on screen) is freed
        // before the new one is sent, and the budget holds.
        let sw = Camera::new(-90.0, -45.0, 3.0);
        let c3 = map.kitty_commands(&sw, area, &mut k);
        assert!(c3.contains("a=d,d=I,i=9000,"), "{c3}");
        assert!(c3.find("a=d,d=I").unwrap() < c3.find("a=t,").unwrap(), "freed before sending");
        assert!(k.bytes() <= k.budget);
        // Back to the north-west: it is sent again rather than assumed loaded.
        assert_eq!(map.kitty_commands(&nw, area, &mut k).matches("a=t,").count(), 1);
    }

    #[test]
    fn failed_loads_are_retried_then_given_up() {
        use std::time::Duration;
        let mut map = raster_map();
        let cam = Camera::new(0.0, 0.0, 0.0);
        let area = Rect::new(0, 0, 40, 20);
        let fail = |map: &mut Map| map.insert_loaded("img", (0, 0, 0), Err(anyhow!("HTTP 500"))).unwrap_err();
        // A long wait: not requested again until it passes.
        map.retry = RetryPolicy { base: Duration::from_secs(3600), max: Duration::from_secs(3600), attempts: 3 };
        assert!(fail(&mut map).to_string().contains("img tile 0/0/0"));
        assert!(map.tile_requests(&cam, area).is_empty(), "waiting before the retry");
        // No wait: requested again right away, still nothing drawn.
        map.retry.base = Duration::ZERO;
        map.failed.values_mut().for_each(|f| f.1 = std::time::Instant::now());
        assert_eq!(map.tile_requests(&cam, area).len(), 1);
        fail(&mut map);
        assert_eq!(map.tile_requests(&cam, area).len(), 1);
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Reset);
        // The last attempt gives up: stored empty, never requested again.
        fail(&mut map);
        assert!(map.tile_requests(&cam, area).is_empty());
        // A success clears the failure record.
        let mut map = raster_map();
        map.retry.base = Duration::ZERO;
        fail(&mut map);
        map.insert_loaded("img", (0, 0, 0), Ok(Loaded(LoadedTile::Raster(Some(solid_tile(1, 2, 3)))))).unwrap();
        assert!(map.failed.is_empty() && map.tile_requests(&cam, area).is_empty());
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Rgb(1, 2, 3));
    }

    #[test]
    fn vector_failures_are_retried_too() {
        let mut map = Map::new();
        map.retry.base = std::time::Duration::ZERO;
        let src = crate::source::Source::xyz_for_tests();
        map.add_source("v", MapSource::vector(src)).unwrap();
        map.add_layer(StyleLayer::fill("water", "v").source_layer("water").color("#00f")).unwrap();
        let (cam, area) = (Camera::new(0.0, 0.0, 0.0), Rect::new(0, 0, 40, 20));
        let req = map.tile_requests(&cam, area).remove(0);
        assert!(map.insert_loaded(&req.source, req.id, Err(anyhow!("timed out"))).is_err());
        assert_eq!(map.tile_requests(&cam, area).len(), 1, "asked for again, not stored empty");
    }

    #[test]
    fn query_features_lists_drawn_and_undrawn() {
        let mvt = crate::mvt::tests_support::square_tile("water", 4096);
        let mut map = sample();
        // A z0-only source, so the camera's z15 view uses this tile.
        map.add_source("v", MapSource::vector_tiles(0, 0)).unwrap();
        map.add_layers(vec![StyleLayer::fill("lakes", "v").source_layer("water").filter(json!(["==", "class", "lake"])).color("#00f")], Some("zones")).unwrap();
        map.insert_tile("v", (0, 0, 0), Some(&mvt)).unwrap();
        let cam = Camera::new(-77.0353, 38.8895, 15.0);
        let area = Rect::new(0, 0, 60, 20);
        let picks = map.query_features(&cam, area, (60.0, 40.0));
        let got: Vec<(String, Option<String>, bool)> =
            picks.iter().map(|p| (p.title(), p.style_layer.as_deref().map(str::to_string), p.drawn)).collect();
        assert_eq!(
            got,
            vec![
                ("Pin".into(), Some("pins".into()), true),
                ("Box".into(), Some("zones".into()), true),
                ("water".into(), None, false),
            ],
            "GeoJSON (top layers) first; the unstyled water square is listed but not drawn"
        );
        map.set_visibility("pins", false).unwrap();
        let pin = &map.query_features(&cam, area, (60.0, 40.0))[0];
        assert!(pin.title() == "Pin" && !pin.drawn, "hidden layers don't count as drawing");
    }

    #[test]
    fn geojson_overlay_layers() {
        let mut map = Map::new();
        map.add_source("mine", MapSource::geojson(&json!({"type": "FeatureCollection", "features": [
            {"type": "Feature", "properties": {"name": "Pin"}, "geometry": {"type": "Point", "coordinates": [-77.0353, 38.8895]}}]})).unwrap()).unwrap();
        map.add_layers(crate::geojson::overlay_layers("mine", Color::Rgb(255, 99, 132), true), None).unwrap();
        assert_eq!(map.layer_ids(), vec!["mine-fill", "mine-line", "mine-point", "mine-cluster", "mine-label"]);
        let cam = Camera::new(-77.0353, 38.8895, 15.0);
        let buf = render(&map, &cam, Rect::new(0, 0, 60, 20));
        let row: String = (0..60).map(|x| buf[(x, 10)].symbol().to_string()).collect();
        assert!(row.contains("● Pin"), "{row}");
        assert_eq!(buf[(30, 10)].fg, Color::Rgb(255, 99, 132));
    }

    fn water_map(max_zoom: u8, tile: TileId, layer: StyleLayer) -> Map {
        let mvt = crate::mvt::tests_support::square_tile("water", 4096);
        let mut map = Map::new();
        map.add_source("v", MapSource::vector_tiles(0, max_zoom)).unwrap();
        map.add_layer(layer).unwrap();
        map.insert_tile("v", tile, Some(&mvt)).unwrap();
        map
    }

    #[test]
    fn paint_follows_the_fractional_camera_zoom() {
        // Black at z2 fading to white at z4, drawn from a z2-only source.
        let layer = StyleLayer::fill("water", "v").source_layer("water").color(json!(["interpolate", ["linear"], ["zoom"], 2, "#000000", 4, "#ffffff"]));
        let map = water_map(2, (2, 1, 1), layer);
        // The middle of tile 2/1/1 (lon -45, lat ~40.98).
        let at = |z: f64| render(&map, &Camera::new(-45.0, 40.979898, z), Rect::new(0, 0, 40, 20))[(20, 10)].bg;
        assert_eq!(at(2.0), Color::Rgb(0, 0, 0));
        assert_eq!(at(2.5), Color::Rgb(64, 64, 64), "a quarter of the way at z2.5, not the tile's z2");
        assert_eq!(at(3.0), Color::Rgb(128, 128, 128));
        assert_eq!(at(4.0), Color::Rgb(255, 255, 255), "past the source's last zoom");
    }

    #[test]
    fn filters_use_the_whole_zoom_level_past_the_source_max() {
        // Only drawn from z16, from a source whose tiles stop at z14.
        let layer = StyleLayer::fill("water", "v").source_layer("water").filter(json!([">=", ["zoom"], 16])).color("#0000ff");
        let map = water_map(14, (14, 4686, 6268), layer);
        let center = |z: f64| Camera::new(-77.025146484375, 38.882481197550796, z);
        let area = Rect::new(0, 0, 40, 20);
        assert_eq!(render(&map, &center(15.9), area)[(20, 10)].bg, Color::Reset, "z15.9 is level 15");
        assert_eq!(render(&map, &center(16.0), area)[(20, 10)].bg, Color::Rgb(0, 0, 255));
        assert_eq!(map.query_rendered_features(&center(16.2), area, (40.0, 40.0), None).len(), 1);
        assert!(map.query_rendered_features(&center(15.2), area, (40.0, 40.0), None).is_empty());
        let picks = map.query_features(&center(15.2), area, (40.0, 40.0));
        assert!(picks.len() == 1 && !picks[0].drawn, "listed by inspect, but not drawn at z15");
    }

    #[test]
    fn attributions_of_drawn_sources() {
        let mut map = sample();
        assert!(map.attributions(15.0).is_empty(), "no credits given");
        map.set_attribution("sites", "<a href='https://example.org'>&copy; Example Sites</a>").unwrap();
        map.add_source("other", MapSource::geojson(&json!({"type": "Point", "coordinates": [0, 0]})).unwrap()).unwrap();
        map.set_attribution("other", "© Example Sites").unwrap();
        map.add_layer(StyleLayer::circle("other-dots", "other").min_zoom(16.0)).unwrap();
        assert_eq!(map.attributions(15.0), vec!["© Example Sites"]);
        assert_eq!(map.attributions(16.0), vec!["© Example Sites"], "the same credit once");
        map.set_attribution("other", "Other Data").unwrap();
        assert_eq!(map.attributions(16.0), vec!["© Example Sites", "Other Data"]);
        assert_eq!(map.attributions(15.0), vec!["© Example Sites"], "layers not shown at this zoom don't credit");
        for id in ["zones", "pins"] {
            map.set_visibility(id, false).unwrap();
        }
        assert_eq!(map.attributions(16.0), vec!["Other Data"], "hidden layers don't credit");
        map.set_data("other", &json!({"type": "Point", "coordinates": [1, 1]})).unwrap();
        assert_eq!(map.attributions(16.0), vec!["Other Data"], "new data keeps the credit");
        assert!(map.set_attribution("nope", "x").is_err());
    }

    #[test]
    fn add_map_merges_sources_and_layers_below_before() {
        let mut map = sample();
        let mut other = Map::new();
        other.add_source("v", MapSource::vector_tiles(0, 14)).unwrap();
        other.add_layer(StyleLayer::fill("land", "v").source_layer("earth").color("#222")).unwrap();
        other.add_layer(StyleLayer::line("roads", "v").source_layer("roads").color("#888")).unwrap();
        let added = map.add_map(other, Some("zones")).unwrap();
        assert_eq!(added, vec!["land", "roads"]);
        assert_eq!(map.layer_ids(), vec!["bg", "land", "roads", "zones", "pins"]);
        assert!(map.source("v").is_some());
        let mut again = Map::new();
        again.add_source("v", MapSource::vector_tiles(0, 14)).unwrap();
        assert!(map.add_map(again, None).is_err(), "source id taken");
        // Map can move to another task (built while a style loads).
        fn is_send<T: Send>() {}
        is_send::<Map>();
    }

    fn text_rows(buf: &Buffer) -> Vec<String> {
        let a = buf.area;
        (a.y..a.bottom()).map(|y| (a.x..a.right()).map(|x| buf[(x, y)].symbol().to_string()).collect::<String>()).collect()
    }

    #[test]
    fn points_along_a_line() {
        let line = [(0.0, 0.0), (60.0, 0.0), (100.0, 0.0)];
        assert_eq!(along(&line, 30.0), vec![(15.0, 0.0), (45.0, 0.0), (75.0, 0.0)]);
        assert_eq!(along(&line, 300.0), vec![(50.0, 0.0)], "a short line gets its middle");
        assert_eq!(along(&[(0.0, 0.0), (0.0, 40.0)], 40.0), vec![(0.0, 20.0)]);
    }

    #[test]
    fn long_roads_repeat_their_name_and_close_copies_merge() {
        let mut map = Map::new();
        // A road across the whole view, and two same-named points: one close to the other,
        // one far away.
        map.add_source("r", MapSource::geojson(&json!({"type": "FeatureCollection", "features": [
            {"type": "Feature", "properties": {"name": "Long Road"}, "geometry": {"type": "LineString", "coordinates": [[-0.5, 0], [0.5, 0]]}},
            {"type": "Feature", "properties": {"name": "Pump"}, "geometry": {"type": "Point", "coordinates": [-0.2, 0.1]}},
            {"type": "Feature", "properties": {"name": "Pump"}, "geometry": {"type": "Point", "coordinates": [-0.195, 0.1]}},
            {"type": "Feature", "properties": {"name": "Pump"}, "geometry": {"type": "Point", "coordinates": [0.25, 0.1]}}
        ]})).unwrap()).unwrap();
        map.add_layer(StyleLayer::symbol("road", "r").filter(json!(["==", ["geometry-type"], "LineString"])).text("{name}").color("#fff")).unwrap();
        map.add_layer(StyleLayer::symbol("pumps", "r").filter(json!(["==", ["geometry-type"], "Point"])).text("{name}").color("#fff")).unwrap();
        let buf = render(&map, &Camera::new(0.0, 0.0, 9.0), Rect::new(0, 0, 160, 20));
        let all = text_rows(&buf).join("\n");
        let roads = all.matches("Long Road").count();
        assert!(roads >= 3, "repeated along the road: {roads}\n{all}");
        assert_eq!(all.matches("Pump").count(), 2, "the close copy merges, the far one stays\n{all}");
        // Wider spacing, fewer repeats.
        map.set_layout("road", "symbol-spacing", &json!(1000)).unwrap();
        let fewer = text_rows(&render(&map, &Camera::new(0.0, 0.0, 9.0), Rect::new(0, 0, 160, 20))).join("\n").matches("Long Road").count();
        assert!(fewer < roads, "{fewer} < {roads}");
    }

    #[test]
    fn variable_anchor_and_offset() {
        let mut map = Map::new();
        map.add_source("p", MapSource::geojson(&json!({"type": "FeatureCollection", "features": [
            {"type": "Feature", "properties": {"name": "Blocker"}, "geometry": {"type": "Point", "coordinates": [0, 0]}},
            {"type": "Feature", "properties": {"name": "Here"}, "geometry": {"type": "Point", "coordinates": [0, 0]}}
        ]})).unwrap()).unwrap();
        // "Blocker" (higher layer, placed first) sits on the point; "Here" wants the same spot
        // first, then tries below it.
        map.add_layer(StyleLayer::symbol("here", "p").filter(json!(["==", "name", "Here"])).text("{name}").color("#fff")
            .layout("text-variable-anchor", json!(["center", "top"]))).unwrap();
        map.add_layer(StyleLayer::symbol("blocker", "p").filter(json!(["==", "name", "Blocker"])).text("{name}").color("#fff")).unwrap();
        let area = Rect::new(0, 0, 40, 11);
        let rows = text_rows(&render(&map, &Camera::new(0.0, 0.0, 5.0), area));
        assert!(rows[5].contains("Blocker") && rows[6].contains("Here"), "{rows:#?}");
        // Without the alternative, it has nowhere to go.
        map.set_layout("here", "text-variable-anchor", &json!(["center"])).unwrap();
        let rows = text_rows(&render(&map, &Camera::new(0.0, 0.0, 5.0), area));
        assert!(!rows.join("").contains("Here"));
        // text-offset in ems: two ems down is two rows.
        map.set_layout("here", "text-offset", &json!([0, 2])).unwrap();
        let rows = text_rows(&render(&map, &Camera::new(0.0, 0.0, 5.0), area));
        assert!(rows[7].contains("Here"), "{rows:#?}");
    }

    /// How many braille dots a buffer shows.
    fn dot_count(buf: &Buffer) -> u32 {
        let a = buf.area;
        let mut n = 0;
        for y in a.y..a.bottom() {
            for x in a.x..a.right() {
                if let Some(ch) = buf[(x, y)].symbol().chars().next()
                    && ('\u{2800}'..='\u{28FF}').contains(&ch)
                {
                    n += (ch as u32 - 0x2800).count_ones();
                }
            }
        }
        n
    }

    #[test]
    fn line_widths_and_dashes() {
        let line_map = |layer: StyleLayer| {
            let mut map = Map::new();
            map.add_source("l", MapSource::geojson(&json!({"type": "LineString", "coordinates": [[-0.2, 0], [0.2, 0]]})).unwrap()).unwrap();
            map.add_layer(layer).unwrap();
            dot_count(&render(&map, &Camera::new(0.0, 0.0, 9.0), Rect::new(0, 0, 80, 10)))
        };
        let solid = line_map(StyleLayer::line("l", "l").color("#fff"));
        assert!(solid > 100, "{solid}");
        let dashed = line_map(StyleLayer::line("l", "l").color("#fff").paint("line-dasharray", json!([4, 4])));
        assert!(dashed * 10 > solid * 4 && dashed * 10 < solid * 6, "about half the dots: {dashed} of {solid}");
        let no_gaps = line_map(StyleLayer::line("l", "l").color("#fff").paint("line-dasharray", json!([12, 0])));
        assert_eq!(no_gaps, solid, "no gaps is a solid line");
        let thick = line_map(StyleLayer::line("l", "l").color("#fff").paint("line-width", 10));
        assert!(thick >= solid * 2 - 4, "two dots thick: {thick} vs {solid}");
        let thicker = line_map(StyleLayer::line("l", "l").color("#fff").paint("line-width", 40));
        assert!(thicker >= solid * 3 - 6 && thicker <= solid * 3 + 6, "three at most: {thicker}");
        assert_eq!(line_map(StyleLayer::line("l", "l").color("#fff").paint("line-width", 0)), 0, "zero width draws nothing");
        // Widths don't make a layer re-evaluate as the zoom changes.
        assert!(!StyleLayer::line("l", "l").paint("line-width", json!(["interpolate", ["linear"], ["zoom"], 5, 1, 15, 8])).uses_zoom());
    }

    #[test]
    fn raster_opacity_blends_with_the_background() {
        let mut map = raster_map();
        map.add_layer_before(StyleLayer::background("bg").color("#000000"), "img").unwrap();
        map.insert_loaded("img", (0, 0, 0), Ok(Loaded(LoadedTile::Raster(Some(solid_tile(200, 100, 0)))))).unwrap();
        let cam = Camera::new(0.0, 0.0, 0.0);
        let area = Rect::new(0, 0, 40, 20);
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Rgb(200, 100, 0));
        map.set_paint("img", "raster-opacity", &json!(0.5)).unwrap();
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Rgb(100, 50, 0));
        map.set_paint("img", "raster-opacity", &json!(0.0)).unwrap();
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Rgb(0, 0, 0), "invisible: the background shows");
    }

    #[test]
    fn feature_state_by_id_and_promote_id() {
        // A vector tile feature with id 42, styled by its state.
        let mvt = crate::mvt::tests_support::square_tile_with_id("water", 4096, Some(42));
        let mut map = Map::new();
        map.add_source("v", MapSource::vector_tiles(0, 14)).unwrap();
        map.add_layer(StyleLayer::fill("water", "v").source_layer("water")
            .color(json!(["case", ["boolean", ["feature-state", "picked"], false], "#ff0000", "#0000ff"]))).unwrap();
        map.insert_tile("v", (0, 0, 0), Some(&mvt)).unwrap();
        let (cam, area) = (Camera::new(0.0, 0.0, 0.0), Rect::new(0, 0, 40, 20));
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Rgb(0, 0, 255));
        let id = crate::mvt::Value::Num(42.0);
        map.set_feature_state("v", &id, &json!({"picked": true})).unwrap();
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Rgb(255, 0, 0), "restyled when the state changes");
        assert_eq!(map.feature_state("v", &id), json!({"picked": true}));
        assert_eq!(map.query_rendered_features(&cam, area, (40.0, 40.0), None)[0].id, Some(id.clone()));
        map.remove_feature_state("v", Some(&id), Some("picked"));
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Rgb(0, 0, 255));
        assert!(map.set_feature_state("nope", &id, &json!({})).is_err());

        // GeoJSON without ids, using a property instead (promoteId).
        let mut map = Map::new();
        map.add_source("g", MapSource::geojson(&json!({"type": "Feature", "properties": {"code": "DC"},
            "geometry": {"type": "Polygon", "coordinates": [[[-1, -1], [1, -1], [1, 1], [-1, 1], [-1, -1]]]}})).unwrap()
            .with_promote_id("code")).unwrap();
        map.add_layer(StyleLayer::fill("g", "g").color(json!(["case", ["boolean", ["feature-state", "on"], false], "#00ff00", "#000000"]))).unwrap();
        let cam = Camera::new(0.0, 0.0, 6.0);
        map.set_feature_state("g", &crate::mvt::Value::Str("DC".into()), &json!({"on": true})).unwrap();
        assert_eq!(render(&map, &cam, area)[(20, 10)].bg, Color::Rgb(0, 255, 0));
        assert_eq!(map.query_features(&cam, area, (40.0, 40.0))[0].id, Some(crate::mvt::Value::Str("DC".into())));
    }

    #[test]
    fn clustered_geojson_source() {
        // 50 points in a tight group plus one far away.
        let mut feats: Vec<Json> = (0..50).map(|i| json!({"type": "Feature", "properties": {"name": format!("p{i}")},
            "geometry": {"type": "Point", "coordinates": [-77.03 + i as f64 * 1e-5, 38.89]}})).collect();
        feats.push(json!({"type": "Feature", "properties": {"name": "far"}, "geometry": {"type": "Point", "coordinates": [-76.0, 38.89]}}));
        let doc = json!({"type": "FeatureCollection", "features": feats});
        let mut map = Map::new();
        map.add_source("pts", MapSource::geojson(&doc).unwrap().with_cluster(crate::cluster::ClusterOptions::default())).unwrap();
        map.add_layers(crate::geojson::overlay_layers("pts", Color::Rgb(255, 99, 132), false), None).unwrap();
        let area = Rect::new(0, 0, 80, 20);
        let cam = Camera::new(-77.03, 38.89, 10.0);
        let rows = text_rows(&render(&map, &cam, area)).join("\n");
        assert!(rows.contains("◉ 50"), "one cluster of 50: {rows}");
        let picks = map.query_rendered_features(&cam, area, (80.0, 40.0), Some(&["pts-cluster"]));
        assert_eq!(picks.len(), 1);
        let Some(crate::mvt::Value::Num(id)) = picks[0].prop("cluster_id").cloned() else { panic!("{:?}", picks[0]) };
        let z = map.cluster_expansion_zoom("pts", id as u64).unwrap();
        assert!(z > 10.0, "{z}");
        // Past the cluster max zoom, the points themselves.
        let close = Camera::new(-77.03, 38.89, 15.5);
        assert!(!text_rows(&render(&map, &close, area)).join("").contains('◉'));
        // New data keeps clustering on.
        map.set_data("pts", &doc).unwrap();
        assert!(text_rows(&render(&map, &cam, area)).join("").contains("◉ 50"));
    }
}