ezu-cli 0.13.0

Command-line renderer for the Ezu Style Spec
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
//! `ezu frame --features` (and `ezu bbox --features`): a features node's
//! value, gathered over every tile a print frame or a mosaic renders,
//! written as one GeoJSON file. `ezu tile --features` writes one tile's.
//!
//! The gathering and the file are the renderer's
//! ([`ezu::renderer::FrameFeatureCollector`]), so the Go and JS packages
//! write the same bytes; what is here is reading the flags into its request.

use std::collections::BTreeMap;
use std::path::PathBuf;

use clap::ValueEnum;
use ezu::renderer::{FrameFeatureNode, FrameFeatures};

/// Coordinate system of the written GeoJSON.
// The variants carry plain comments, not doc comments: clap would turn
// those into per-value help and switch the whole command's `--help` to
// its long layout.
#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub(crate) enum FeatureCoords {
    // WGS84 longitude and latitude, as RFC 7946 GeoJSON expects.
    Lonlat,
    // Pixels of the output image: origin at its top-left corner, y down.
    Px,
}

/// Coordinate system of `ezu tile --features`.
// Plain comments on the variants, as on `FeatureCoords`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub(crate) enum TileCoords {
    // The node's own units: whole numbers, 0..extent across the tile.
    Tile,
    // WGS84 longitude and latitude.
    Lonlat,
    // Pixels of the tile: origin at its top-left corner, y down.
    Px,
}

impl From<TileCoords> for ezu::renderer::TileFeatureCoords {
    fn from(c: TileCoords) -> Self {
        match c {
            TileCoords::Tile => Self::Tile,
            TileCoords::Lonlat => Self::LonLat,
            TileCoords::Px => Self::Px,
        }
    }
}

impl From<FeatureCoords> for ezu::renderer::FeatureCoords {
    fn from(c: FeatureCoords) -> Self {
        match c {
            FeatureCoords::Lonlat => Self::LonLat,
            FeatureCoords::Px => Self::Px,
        }
    }
}

/// One page's `--features` and `--features-key`, as the request the
/// renderer gathers them by, and the file each of its entries is written to.
pub(crate) struct Exports {
    pub request: FrameFeatures,
    pub paths: Vec<PathBuf>,
}

/// `NODE=PATH`.
pub(crate) fn parse_node_path(s: &str) -> Result<(String, PathBuf), String> {
    match s.split_once('=') {
        Some((node, path)) if !node.is_empty() && !path.is_empty() => {
            Ok((node.to_string(), PathBuf::from(path)))
        }
        _ => Err(format!("expected NODE=PATH, got `{s}`")),
    }
}

/// `NODE=PROP[,PROP…]`.
pub(crate) fn parse_node_key(s: &str) -> Result<(String, Vec<String>), String> {
    let bad = || format!("expected NODE=PROP[,PROP...], got `{s}`");
    let (node, props) = s.split_once('=').ok_or_else(bad)?;
    let props: Vec<String> = props.split(',').map(|p| p.trim().to_string()).collect();
    if node.is_empty() || props.iter().any(String::is_empty) {
        return Err(bad());
    }
    Ok((node.to_string(), props))
}

/// Pair every `--features` with its key. Whether each names a features
/// node of the style is the renderer's to check, which it does before
/// anything renders.
pub(crate) fn exports(
    features: &[(String, PathBuf)],
    keys: &[(String, Vec<String>)],
    coords: FeatureCoords,
) -> Result<Exports, String> {
    let mut key_of: BTreeMap<&str, &Vec<String>> = BTreeMap::new();
    for (node, props) in keys {
        if !features.iter().any(|(n, _)| n == node) {
            return Err(format!(
                "--features-key names `{node}`, which no --features writes"
            ));
        }
        if key_of.insert(node, props).is_some() {
            return Err(format!("--features-key is given twice for `{node}`"));
        }
    }
    Ok(Exports {
        request: FrameFeatures {
            nodes: features
                .iter()
                .map(|(node, _)| FrameFeatureNode {
                    node: node.clone(),
                    key: key_of.get(node.as_str()).map(|k| (*k).clone()),
                })
                .collect(),
            coords: coords.into(),
        },
        paths: features.iter().map(|(_, path)| path.clone()).collect(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{run_frame, CommonArgs, FrameCmd};
    use ezu::core::coord::{lat_to_world_y, lon_to_world_x, world_x_to_lon, world_y_to_lat};
    use ezu::core::frame::{Frame, FrameRequest};
    use std::path::Path;

    /// 1:5000 at 96 dpi: tiles of ~188 px at z=17, and a 300×200 frame
    /// that straddles tiles on every side.
    fn request() -> FrameRequest {
        FrameRequest {
            center_lon: 139.70,
            center_lat: 35.66,
            scale: 5000.0,
            dpi: 96.0,
            width: 300,
            height: 200,
            zoom: None,
            style_tile_size: 256,
        }
    }

    fn scratch(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!("ezu-{name}-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    fn font_url() -> String {
        let path = Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../ezu-core/tests/fonts/NotoSans-Regular.latin.ttf");
        format!("file:{}", path.display()).replace('\\', "/")
    }

    /// `lon,lat` of output-image pixel `(x, y)` of `frame`.
    fn lonlat_at(frame: &Frame, x: f64, y: f64) -> (f64, f64) {
        let world = frame.tile_size as f64 * (1u64 << frame.zoom) as f64;
        (
            world_x_to_lon((x + frame.left as f64) / world),
            world_y_to_lat((y + frame.top as f64) / world),
        )
    }

    /// Point features as GeoJSON, each `(name, lon, lat)`.
    fn points(pts: &[(&str, f64, f64)]) -> String {
        let feats: Vec<String> = pts
            .iter()
            .map(|(name, lon, lat)| {
                format!(
                    r#"{{ "type": "Feature", "properties": {{ "name": "{name}" }},
                         "geometry": {{ "type": "Point", "coordinates": [{lon}, {lat}] }} }}"#
                )
            })
            .collect();
        format!(
            r#"{{ "type": "FeatureCollection", "features": [{}] }}"#,
            feats.join(",")
        )
    }

    async fn frame(
        style: &str,
        dir: &Path,
        features: &[(&str, &Path)],
        key: Option<&str>,
        coords: FeatureCoords,
    ) -> Result<(), String> {
        let style_path = dir.join("style.json");
        std::fs::write(&style_path, style).unwrap();
        let req = request();
        run_frame(FrameCmd {
            common: CommonArgs {
                style: style_path.to_string_lossy().into_owned(),
                assets_dir: None,
                pmtiles: None,
                mvt: None,
                overzoom_levels: 4,
                params: Vec::new(),
                strict: false,
                pad: None,
            },
            pages: None,
            center: Some((req.center_lon, req.center_lat)),
            scale: Some(req.scale),
            dpi: Some(req.dpi),
            size_mm: None,
            size_px: Some((req.width, req.height)),
            zoom: None,
            out: dir.join("page.png"),
            format: None,
            png: Default::default(),
            concurrency: Some(3),
            features: features
                .iter()
                .map(|(n, p)| (n.to_string(), p.to_path_buf()))
                .collect(),
            features_key: key
                .map(|k| vec![parse_node_key(k).unwrap()])
                .unwrap_or_default(),
            features_coords: coords,
        })
        .await
        .map_err(|e| e.to_string())
    }

    fn read(path: &Path) -> serde_json::Value {
        serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()
    }

    fn features(v: &serde_json::Value) -> &Vec<serde_json::Value> {
        v["features"].as_array().unwrap()
    }

    /// Black labels on white, with every `label-boxes` box exported.
    fn label_style(pts: &str) -> String {
        label_style_with(pts, "")
    }

    /// [`label_style`] with `text_extra` added to the label layer's fields.
    fn label_style_with(pts: &str, text_extra: &str) -> String {
        format!(
            r##"{{
              "name": "frame-labels",
              "tile-size": 256,
              "sources": {{
                "pts":  {{ "type": "geojson", "data": {pts} }},
                "body": {{ "type": "font", "url": "{font}" }}
              }},
              "nodes": {{
                "bg":     {{ "op": "solid", "color": "#ffffff" }},
                "feats":  {{ "op": "features", "source": "pts", "layer": "pts" }},
                "labels": {{ "op": "text-labels", "features": "@feats", "source": "pts", "layer": "pts",
                             "font": ["body"], "text": ["get", "name"], "size": 16,
                             "color": "#000000" {text_extra} }},
                "placed": {{ "op": "label-placement", "labels": ["@labels"] }},
                "boxes":  {{ "op": "label-boxes", "placement": "@placed", "include": "all" }},
                "text":   {{ "op": "text-draw", "labels": "@labels", "placement": "@placed" }},
                "out":    {{ "op": "stack", "layers": ["@bg", "@text"] }}
              }},
              "output": "@out"
            }}"##,
            font = font_url(),
        )
    }

    /// Labels straddling a tile edge inside the frame are listed once each
    /// with the label-boxes key, the file is the same on every run, and the
    /// text boxes sit where the text is drawn.
    #[tokio::test(flavor = "multi_thread")]
    async fn label_boxes_across_tile_edges_are_listed_once() {
        let dir = scratch("frame-labels");
        let fr = Frame::compute(&request()).unwrap();
        assert!(fr.tiles_x.len() >= 3);
        // The first tile edge inside the frame, and a row in its middle.
        let (edge, _) = fr.tile_offset(fr.tiles_x.start + 1, fr.tiles_y.start);
        let edge = edge as f64;
        let pts = [("Alpha", edge - 3.0, 60.0), ("Beta", edge + 4.0, 140.0)];
        let geo: Vec<(&str, f64, f64)> = pts
            .iter()
            .map(|&(n, x, y)| {
                let (lon, lat) = lonlat_at(&fr, x, y);
                (n, lon, lat)
            })
            .collect();
        let style = label_style(&points(&geo));

        let (a, b, raw) = (dir.join("a.json"), dir.join("b.json"), dir.join("raw.json"));
        let key = Some("boxes=label-id,part");
        frame(&style, &dir, &[("boxes", &a)], key, FeatureCoords::Px)
            .await
            .unwrap();
        frame(&style, &dir, &[("boxes", &b)], key, FeatureCoords::Px)
            .await
            .unwrap();
        frame(&style, &dir, &[("boxes", &raw)], None, FeatureCoords::Px)
            .await
            .unwrap();
        assert_eq!(std::fs::read(&a).unwrap(), std::fs::read(&b).unwrap());

        let key_of = |f: &serde_json::Value| {
            let p = &f["properties"];
            (
                p["label-id"].as_i64().unwrap(),
                p["part"].as_str().unwrap().to_string(),
            )
        };
        let doc = read(&a);
        let keyed: Vec<_> = features(&doc).iter().map(key_of).collect();
        let mut sorted = keyed.clone();
        sorted.sort();
        sorted.dedup();
        assert_eq!(keyed, sorted, "each (label-id, part) once, in key order");
        // Both tiles beside the edge saw both labels: without the key the
        // copies are all there.
        let raw_doc = read(&raw);
        assert!(features(&raw_doc).len() > features(&doc).len());

        let image = tiny_skia::Pixmap::load_png(dir.join("page.png")).unwrap();
        let dark = |x: u32, y: u32| image.pixel(x, y).is_some_and(|p| p.red() < 128);
        for (name, ax, ay) in pts {
            let text = features(&doc)
                .iter()
                .find(|f| f["properties"]["name"] == name && f["properties"]["part"] == "text")
                .unwrap_or_else(|| panic!("a text box for {name}"));
            assert_eq!(text["properties"]["placed"], true);
            let ring = text["geometry"]["coordinates"][0].as_array().unwrap();
            let xs: Vec<f64> = ring.iter().map(|c| c[0].as_f64().unwrap()).collect();
            let ys: Vec<f64> = ring.iter().map(|c| c[1].as_f64().unwrap()).collect();
            let fold = |v: &[f64], f: fn(f64, f64) -> f64, init| v.iter().copied().fold(init, f);
            let (x0, x1) = (fold(&xs, f64::min, f64::MAX), fold(&xs, f64::max, f64::MIN));
            let (y0, y1) = (fold(&ys, f64::min, f64::MAX), fold(&ys, f64::max, f64::MIN));
            // Centred on the anchor, and across the tile edge.
            assert!((0.5 * (x0 + x1) - ax).abs() < 2.0, "{name}: {x0}..{x1}");
            assert!((0.5 * (y0 + y1) - ay).abs() < 3.0, "{name}: {y0}..{y1}");
            assert!(x0 < edge && x1 > edge, "{name} straddles the edge");
            // The text's ink lies inside its box, and there is ink.
            let mut ink = 0;
            for y in (y0 - 6.0) as u32..(y1 + 6.0) as u32 {
                for x in (x0 - 6.0) as u32..(x1 + 6.0) as u32 {
                    if dark(x, y) {
                        ink += 1;
                        let inside = (x as f64) >= x0 - 1.0
                            && (x as f64) <= x1 + 1.0
                            && (y as f64) >= y0 - 1.0
                            && (y as f64) <= y1 + 1.0;
                        assert!(inside, "{name}: ink at ({x}, {y}) outside its box");
                    }
                }
            }
            assert!(ink > 20, "{name}: drawn");
        }
        std::fs::remove_dir_all(&dir).ok();
    }

    /// A label culled for its size is still listed, once, as dropped, even
    /// where it straddles a tile edge, and nothing of it is drawn.
    #[tokio::test(flavor = "multi_thread")]
    async fn a_culled_label_is_listed_once_as_dropped() {
        let dir = scratch("frame-culled");
        let fr = Frame::compute(&request()).unwrap();
        let (edge, _) = fr.tile_offset(fr.tiles_x.start + 1, fr.tiles_y.start);
        let (lon, lat) = lonlat_at(&fr, edge as f64 - 3.0, 100.0);
        let style = label_style_with(&points(&[("Gamma", lon, lat)]), r#", "max-extent-px": 4"#);

        let out = dir.join("boxes.json");
        let key = Some("boxes=label-id,part");
        frame(&style, &dir, &[("boxes", &out)], key, FeatureCoords::Px)
            .await
            .unwrap();
        let doc = read(&out);
        let mut parts: Vec<&str> = features(&doc)
            .iter()
            .map(|f| {
                let p = &f["properties"];
                assert_eq!(p["name"], "Gamma");
                assert_eq!(p["placed"], false);
                assert_eq!(p["drop-reason"], "culled");
                assert!(p.get("variant-index").is_none() && p.get("overlapped").is_none());
                p["part"].as_str().unwrap()
            })
            .collect();
        parts.sort();
        assert_eq!(parts, ["collision", "text"]);

        let image = tiny_skia::Pixmap::load_png(dir.join("page.png")).unwrap();
        assert!(
            image.pixels().iter().all(|p| p.red() >= 128),
            "nothing is drawn"
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    /// A point written as lon/lat comes back where it was declared, and as
    /// px where the frame puts it.
    #[tokio::test(flavor = "multi_thread")]
    async fn a_point_round_trips_through_lonlat_and_px() {
        let dir = scratch("frame-point");
        let fr = Frame::compute(&request()).unwrap();
        let (lon, lat) = (139.7012345, 35.6598765);
        let style = format!(
            r##"{{
              "name": "frame-point",
              "tile-size": 256,
              "sources": {{ "pts": {{ "type": "geojson", "data": {pts} }} }},
              "nodes": {{
                "feats": {{ "op": "features", "source": "pts", "layer": "pts" }},
                "dots":  {{ "op": "circles", "features": "@feats", "radius": 3, "color": "#000000" }}
              }},
              "output": "@dots"
            }}"##,
            pts = points(&[("p", lon, lat)]),
        );
        let (geo, px) = (dir.join("geo.json"), dir.join("px.json"));
        let key = Some("feats=name");
        frame(&style, &dir, &[("feats", &geo)], key, FeatureCoords::Lonlat)
            .await
            .unwrap();
        frame(&style, &dir, &[("feats", &px)], key, FeatureCoords::Px)
            .await
            .unwrap();

        let doc = read(&geo);
        let [f] = features(&doc).as_slice() else {
            panic!("one feature: {doc}");
        };
        assert_eq!(f["geometry"]["type"], "Point");
        let c = &f["geometry"]["coordinates"];
        // Tile geometry is quantised to 1/4096 of a ~188 px tile.
        assert!((c[0].as_f64().unwrap() - lon).abs() < 2e-6, "{c}");
        assert!((c[1].as_f64().unwrap() - lat).abs() < 2e-6, "{c}");

        let doc = read(&px);
        let c = &features(&doc)[0]["geometry"]["coordinates"];
        let world = fr.tile_size as f64 * (1u64 << fr.zoom) as f64;
        let want = (
            lon_to_world_x(lon) * world - fr.left as f64,
            lat_to_world_y(lat) * world - fr.top as f64,
        );
        assert!(
            (c[0].as_f64().unwrap() - want.0).abs() < 0.1,
            "{c} vs {want:?}"
        );
        assert!(
            (c[1].as_f64().unwrap() - want.1).abs() < 0.1,
            "{c} vs {want:?}"
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    /// One point, drawn as a dot, with its features node `feats`.
    fn point_style(lon: f64, lat: f64) -> String {
        format!(
            r##"{{
              "name": "tile-point",
              "tile-size": 256,
              "sources": {{ "pts": {{ "type": "geojson", "data": {pts} }} }},
              "nodes": {{
                "feats": {{ "op": "features", "source": "pts", "layer": "pts" }},
                "dots":  {{ "op": "circles", "features": "@feats", "radius": 3, "color": "#000000" }}
              }},
              "output": "@dots"
            }}"##,
            pts = points(&[("p", lon, lat)]),
        )
    }

    fn common_for(style: &Path) -> CommonArgs {
        CommonArgs {
            style: style.to_string_lossy().into_owned(),
            assets_dir: None,
            pmtiles: None,
            mvt: None,
            overzoom_levels: 4,
            params: Vec::new(),
            strict: false,
            pad: None,
        }
    }

    /// `ezu tile --features` writes the tile's collection as the embedding
    /// packages hand it back — tile units by default — and in lon/lat or the
    /// tile's pixels on request, at the tile size it rendered.
    #[tokio::test(flavor = "multi_thread")]
    async fn a_tile_writes_its_features_in_the_coords_asked_for() {
        use crate::{run_tile, TileCmd, TileSizeArgs};
        let dir = scratch("tile-features");
        let (lon, lat) = (139.7012345, 35.6598765);
        let style = dir.join("style.json");
        std::fs::write(&style, point_style(lon, lat)).unwrap();
        let z = 16u8;
        let n = (1u32 << z) as f64;
        let (wx, wy) = (lon_to_world_x(lon) * n, lat_to_world_y(lat) * n);
        let tile = ezu::core::TileId::new(z, wx as u32, wy as u32);
        let run = |coords: TileCoords, tile_size: Option<u32>, out: &str| {
            let out = dir.join(out);
            let cmd = TileCmd {
                common: common_for(&style),
                size: TileSizeArgs {
                    tile_size,
                    ..TileSizeArgs::default()
                },
                tile,
                out: dir.join("tile.png"),
                format: None,
                png: Default::default(),
                features: vec![("feats".into(), out.clone())],
                features_coords: coords,
            };
            async move {
                run_tile(cmd).await.unwrap();
                std::fs::read_to_string(&out).unwrap()
            }
        };
        let units = run(TileCoords::Tile, None, "units.json").await;
        let doc: serde_json::Value = serde_json::from_str(&units).unwrap();
        assert_eq!(
            (doc["node"].as_str(), doc["extent"].as_u64()),
            (Some("feats"), Some(4096))
        );
        let at = |doc: &serde_json::Value| {
            let c = &features(doc)[0]["geometry"]["coordinates"];
            (c[0].as_f64().unwrap(), c[1].as_f64().unwrap())
        };
        let (u, v) = at(&doc);
        let want = ((wx - tile.x as f64) * 4096.0, (wy - tile.y as f64) * 4096.0);
        assert!(
            (u - want.0).abs() <= 1.0 && (v - want.1).abs() <= 1.0,
            "{u},{v}"
        );
        assert!(
            !units.contains('.'),
            "tile units are whole numbers: {units}"
        );

        let doc = read_text(&run(TileCoords::Lonlat, None, "lonlat.json").await);
        let (x, y) = at(&doc);
        assert!((x - lon).abs() < 2e-6 && (y - lat).abs() < 2e-6, "{x},{y}");

        let doc = read_text(&run(TileCoords::Px, Some(512), "px.json").await);
        let (x, y) = at(&doc);
        assert!(
            (x - u / 8.0).abs() < 0.01 && (y - v / 8.0).abs() < 0.01,
            "{x},{y}"
        );

        // A name that is no features node stops the run before it writes.
        let out = dir.join("never.png");
        let err = run_tile(TileCmd {
            common: common_for(&style),
            size: TileSizeArgs::default(),
            tile,
            out: out.clone(),
            format: None,
            png: Default::default(),
            features: vec![("dots".into(), dir.join("never.json"))],
            features_coords: TileCoords::Tile,
        })
        .await
        .unwrap_err();
        assert!(err.to_string().contains("`dots` produces raster"), "{err}");
        assert!(!out.exists());
        std::fs::remove_dir_all(&dir).ok();
    }

    /// `ezu bbox --features` writes a mosaic's features as `frame` writes a
    /// page's: one copy with a key, and mosaic pixels for `px`.
    #[tokio::test(flavor = "multi_thread")]
    async fn a_mosaic_writes_its_features_as_a_page_does() {
        use crate::{run_bbox, BBox, BboxCmd, TileSizeArgs};
        let dir = scratch("bbox-features");
        let (lon, lat) = (139.7012345, 35.6598765);
        let style = dir.join("style.json");
        std::fs::write(&style, point_style(lon, lat)).unwrap();
        let z = 16u8;
        let bbox = BBox {
            min_lng: lon - 0.01,
            min_lat: lat - 0.01,
            max_lng: lon + 0.01,
            max_lat: lat + 0.01,
        };
        let run = |coords: FeatureCoords, key: Option<&str>, out: &str| {
            let out = dir.join(out);
            let cmd = BboxCmd {
                common: common_for(&style),
                size: TileSizeArgs::default(),
                bbox,
                zoom: z,
                out: dir.join("mosaic.png"),
                format: None,
                png: Default::default(),
                concurrency: Some(3),
                features: vec![("feats".into(), out.clone())],
                features_key: key
                    .map(|k| vec![parse_node_key(k).unwrap()])
                    .unwrap_or_default(),
                features_coords: coords,
            };
            async move {
                run_bbox(cmd).await.unwrap();
                std::fs::read_to_string(&out).unwrap()
            }
        };
        let doc = read_text(&run(FeatureCoords::Lonlat, Some("feats=name"), "geo.json").await);
        let [f] = features(&doc).as_slice() else {
            panic!("one feature: {doc}");
        };
        let c = &f["geometry"]["coordinates"];
        assert!((c[0].as_f64().unwrap() - lon).abs() < 2e-6, "{c}");
        assert!((c[1].as_f64().unwrap() - lat).abs() < 2e-6, "{c}");

        let doc = read_text(&run(FeatureCoords::Px, Some("feats=name"), "px.json").await);
        let c = &features(&doc)[0]["geometry"]["coordinates"];
        let n = (1u32 << z) as f64;
        let x0 = (lon_to_world_x(bbox.min_lng) * n).floor();
        let y0 = (lat_to_world_y(bbox.max_lat) * n).floor();
        let want = (
            (lon_to_world_x(lon) * n - x0) * 256.0,
            (lat_to_world_y(lat) * n - y0) * 256.0,
        );
        assert!(
            (c[0].as_f64().unwrap() - want.0).abs() < 0.1,
            "{c} vs {want:?}"
        );
        assert!(
            (c[1].as_f64().unwrap() - want.1).abs() < 0.1,
            "{c} vs {want:?}"
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    fn read_text(text: &str) -> serde_json::Value {
        serde_json::from_str(text).unwrap()
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn only_features_nodes_can_be_written() {
        let dir = scratch("frame-features-errors");
        let style = label_style(&points(&[]));
        let out = dir.join("x.json");
        let err = frame(&style, &dir, &[("nope", &out)], None, FeatureCoords::Lonlat)
            .await
            .unwrap_err();
        assert!(err.contains("no node `nope`"), "{err}");
        let err = frame(&style, &dir, &[("bg", &out)], None, FeatureCoords::Lonlat)
            .await
            .unwrap_err();
        assert!(err.contains("`bg` produces raster"), "{err}");
        let err = frame(
            &style,
            &dir,
            &[("boxes", &out)],
            Some("feats=name"),
            FeatureCoords::Lonlat,
        )
        .await
        .unwrap_err();
        assert!(err.contains("no --features writes"), "{err}");
        assert!(!out.exists());
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn flags_parse() {
        assert_eq!(
            parse_node_path("boxes=out/labels.geojson"),
            Ok(("boxes".into(), PathBuf::from("out/labels.geojson")))
        );
        assert!(parse_node_path("boxes").is_err());
        assert!(parse_node_path("=x.geojson").is_err());
        assert_eq!(
            parse_node_key("boxes=label-id,part"),
            Ok(("boxes".into(), vec!["label-id".into(), "part".into()]))
        );
        assert!(parse_node_key("boxes=label-id,").is_err());
    }
}