manby 0.1.8

Builds vector tilesets from geographic features, like tippecanoe, several times faster
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
//! manby join's tile work against tile-join's own: random tiles, odd ones
//! included, decoded both ways, and joined both ways, by `join_tile` and by
//! tile-join's `append_tile` and `mvt_tile::encode` as tippecanoe-rs has them,
//! copied here with its rules as arguments.

use std::collections::{BTreeMap, BTreeSet, HashMap};

use tippecanoe::mbtiles::{LayermapEntry, add_to_tilestats, merge_layermaps};
use tippecanoe::mvt::{
    MVT_DOUBLE, MVT_LINESTRING, MVT_LINETO, MVT_MOVETO, MVT_NULL, MVT_POINT, MVT_POLYGON,
    MVT_STRING, MvtFeature, MvtLayer, MvtTile, MvtValue, mvt_value_to_serial_val,
    stringified_to_mvt_value,
};
use tippecanoe::protozero::PbfWriter;
use tippecanoe::serial::SerialVal;

use super::csv::RawCsv;
use super::key::{Key, TieBreak, cmp, is_libdeflate_gzip};
use super::stats::{Accum, std_max, std_min};
use super::tile::{self, Decoded, Rules, Scratch, Val};

struct Rng(u64);

impl Rng {
    fn next(&mut self) -> u64 {
        self.0 = self
            .0
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        self.0 >> 33
    }

    fn below(&mut self, n: u64) -> u64 {
        self.next() % n.max(1)
    }

    fn chance(&mut self, percent: u64) -> bool {
        self.below(100) < percent
    }
}

const NAMES: [&str; 4] = ["roads", "water", "places", "b\u{e5}d"];
const KEYS: [&str; 8] = ["name", "id", "kind", "pop", "area", "flag", "GEOID", "x"];

fn value(rng: &mut Rng, w: &mut PbfWriter<'_>) {
    match rng.below(9) {
        0 | 1 => {
            let s = format!("v{}", rng.below(40));
            w.add_string(1, s.as_bytes());
        }
        2 => w.add_float(2, rng.below(1000) as f32 / 8.0),
        3 => w.add_double(3, rng.below(100000) as f64 / 3.0),
        4 => w.add_int64(4, rng.below(1000) as i64 - 500),
        5 => w.add_uint64(5, rng.below(1 << 40)),
        6 => w.add_sint64(6, rng.below(1000) as i64 - 500),
        7 => w.add_bool(7, rng.chance(50)),
        _ => {
            // more than one field: the last one decides
            w.add_string(1, b"first");
            w.add_int64(4, rng.below(10) as i64);
        }
    }
}

fn geometry(rng: &mut Rng, type_: i32) -> Vec<u32> {
    let mut g = Vec::new();
    let zz = |v: i32| ((v << 1) ^ (v >> 31)) as u32;
    let coord = |rng: &mut Rng, g: &mut Vec<u32>| {
        g.push(zz(rng.below(600) as i32 - 300));
        g.push(zz(rng.below(600) as i32 - 300));
    };
    let parts = 1 + rng.below(3);
    for _ in 0..parts {
        if rng.chance(5) {
            // commands as an encoder that merges nothing would write them
            g.push(1 << 3 | 1);
            coord(rng, &mut g);
            g.push(1 << 3 | 2);
            coord(rng, &mut g);
            g.push(1 << 3 | 2);
            coord(rng, &mut g);
            g.push(2 << 3 | 7);
            continue;
        }
        if rng.chance(2) {
            // a command with no points
            g.push(2);
        }
        let n = if type_ == MVT_POINT {
            1 + rng.below(2)
        } else {
            1
        };
        g.push((n as u32) << 3 | 1);
        for _ in 0..n {
            coord(rng, &mut g);
        }
        if type_ != MVT_POINT {
            let m = 1 + rng.below(5);
            g.push((m as u32) << 3 | 2);
            for _ in 0..m {
                coord(rng, &mut g);
            }
            if type_ == MVT_POLYGON {
                g.push(1 << 3 | 7);
            }
        }
    }
    if rng.chance(3) {
        // a count with too few coordinates, at the end, where decoding stops
        g.push(3 << 3 | 2);
        coord(rng, &mut g);
    }
    g
}

/// A random tile, with the oddities tile-join meets in tiles from other tools.
fn random_tile(rng: &mut Rng, many_values: bool) -> Vec<u8> {
    let mut tile = Vec::new();
    let mut w = PbfWriter::new(&mut tile);
    let layers = 1 + rng.below(3);
    for _ in 0..layers {
        let mut layer = Vec::new();
        let mut lw = PbfWriter::new(&mut layer);
        if rng.chance(95) {
            lw.add_uint32(15, 2);
        }
        lw.add_string(1, NAMES[rng.below(4) as usize].as_bytes());
        lw.add_uint32(5, [4096, 4096, 1024, 8192][rng.below(4) as usize]);
        if rng.chance(3) {
            // an unknown field
            lw.add_uint32(9, 7);
        }
        let mut nkeys = 1 + rng.below(KEYS.len() as u64);
        for i in 0..nkeys {
            lw.add_string(3, KEYS[i as usize].as_bytes());
        }
        if many_values {
            // enough keys that some share a hash slot in tile-join's table
            for i in 0..3000 {
                lw.add_string(3, format!("key number {}", i).as_bytes());
            }
            nkeys += 3000;
        }
        if rng.chance(5) {
            // a duplicate key
            lw.add_string(3, KEYS[0].as_bytes());
            nkeys += 1;
        }
        if rng.chance(3) {
            // not UTF-8
            lw.add_string(3, b"k\xff");
            nkeys += 1;
        }
        let nvalues = if many_values { 3000 } else { 1 + rng.below(30) };
        for i in 0..nvalues {
            let mut v = Vec::new();
            let mut vw = PbfWriter::new(&mut v);
            if many_values {
                vw.add_string(1, format!("value number {}", i).as_bytes());
            } else {
                value(rng, &mut vw);
            }
            lw.add_message(4, &v);
        }
        let nfeatures = 1 + rng.below(if many_values { 400 } else { 20 });
        for _ in 0..nfeatures {
            let mut f = Vec::new();
            let mut fw = PbfWriter::new(&mut f);
            if rng.chance(30) {
                fw.add_uint64(1, rng.below(1 << 20));
            }
            let type_ = 1 + rng.below(3) as i32;
            fw.add_enum(3, type_);
            let ntags = rng.below(if many_values { 40 } else { 6 });
            let tags: Vec<u32> = (0..ntags)
                .flat_map(|_| [rng.below(nkeys) as u32, rng.below(nvalues) as u32])
                .collect();
            if rng.chance(5) && tags.len() >= 2 {
                // two tag fields
                fw.add_packed_uint32(2, tags[..2].iter().copied());
                fw.add_packed_uint32(2, tags[2..].iter().copied());
            } else {
                fw.add_packed_uint32(2, tags.iter().copied());
            }
            fw.add_packed_uint32(4, geometry(rng, type_));
            lw.add_message(2, &f);
        }
        w.add_message(3, &layer);
    }
    if rng.chance(5) {
        w.add_uint32(7, 1);
    }
    tile
}

#[test]
fn decoding_matches_tippecanoe() {
    let mut rng = Rng(1);
    for i in 0..300 {
        let data = random_tile(&mut rng, i % 50 == 0);
        let ours = Decoded::decode(&data).unwrap();
        let mut theirs = MvtTile::default();
        let mut was_compressed = false;
        assert!(theirs.try_decode(&data, &mut was_compressed).unwrap());
        assert_eq!(ours.layers.len(), theirs.layers.len());
        for (a, b) in ours.layers.iter().zip(&theirs.layers) {
            assert_eq!(a.name, b.name);
            assert_eq!(a.version, b.version);
            assert_eq!(a.extent, b.extent);
            assert_eq!(a.keys, b.keys);
            assert_eq!(a.values.len(), b.values.len());
            for (va, vb) in a.values.iter().zip(&b.values) {
                assert_eq!(va.to_mvt(), *vb);
                assert_eq!(va.mvt_type(), vb.type_);
            }
            assert_eq!(a.features.len(), b.features.len());
            for (fa, fb) in a.features.iter().zip(&b.features) {
                assert_eq!(fa.id, fb.id);
                assert_eq!(fa.has_id, fb.has_id);
                assert_eq!(fa.type_, fb.type_);
                assert_eq!(
                    &ours.tags[fa.tags.start as usize..fa.tags.end as usize],
                    &fb.tags[..]
                );
                let mut points = Vec::new();
                let commands = &ours.geoms[fa.geom.start as usize..fa.geom.end as usize];
                tile::for_each_point_for_test(commands, |op, x, y| points.push((op, x, y)));
                let expected: Vec<(i32, i64, i64)> =
                    fb.geometry.iter().map(|g| (g.op, g.x, g.y)).collect();
                assert_eq!(points, expected);
            }
        }
    }
}

/// tile-join's rules for a test, and the same as `Rules`.
struct TestRules {
    header: Vec<String>,
    mapping: BTreeMap<String, Vec<String>>,
    exclude: BTreeSet<String>,
    include: BTreeSet<String>,
    keep_layers: BTreeSet<String>,
    remove_layers: BTreeSet<String>,
    renames: BTreeMap<String, String>,
    ifmatched: bool,
    exclude_all: bool,
    exclude_all_tile_attributes: bool,
    exclude_all_tile_geometries: bool,
    pe: bool,
}

/// What tile-join's `append_tile` collects besides the tile.
struct Collected {
    layermap: BTreeMap<String, LayermapEntry>,
    minlat: f64,
    minlon: f64,
    maxlat: f64,
    maxlon: f64,
    minlon2: f64,
    maxlon2: f64,
}

/// tile-join's `append_tile`, from tippecanoe-rs, without the feature filter.
#[allow(clippy::needless_range_loop)]
fn append_tile(
    tile: &mut MvtTile,
    z: i32,
    x: u32,
    y: u32,
    r: &TestRules,
    outtile: &mut MvtTile,
    a: &mut Collected,
) {
    for layer in tile.layers.iter_mut() {
        if let Some(found) = r.renames.get(&layer.name) {
            layer.name = found.clone();
        }
        let layer: &MvtLayer = layer;
        if !r.keep_layers.is_empty() && !r.keep_layers.contains(&layer.name) {
            continue;
        }
        if r.remove_layers.contains(&layer.name) {
            continue;
        }
        let mut ol = 0;
        while ol < outtile.layers.len() {
            if layer.name == outtile.layers[ol].name {
                break;
            }
            ol += 1;
        }
        if ol == outtile.layers.len() {
            outtile.layers.push(MvtLayer::default());
            outtile.layers[ol].name = layer.name.clone();
            outtile.layers[ol].version = layer.version;
            outtile.layers[ol].extent = layer.extent;
        }
        let outlayer = &mut outtile.layers[ol];
        if layer.extent != outlayer.extent && layer.extent > outlayer.extent {
            for feature in outlayer.features.iter_mut() {
                for g in feature.geometry.iter_mut() {
                    g.x = g.x * layer.extent / outlayer.extent;
                    g.y = g.y * layer.extent / outlayer.extent;
                }
            }
            outlayer.extent = layer.extent;
        }
        let mut tilestats_found = a.layermap.contains_key(&layer.name);
        let mut minx = i64::MAX;
        let mut miny = i64::MAX;
        let mut maxx = i64::MIN;
        let mut maxy = i64::MIN;
        let mut minx2 = i64::MAX;
        let mut maxx2 = i64::MIN;
        let mut features_added_to_layer = false;

        for feat in &layer.features {
            let exclude_attributes: BTreeSet<String> = BTreeSet::new();
            #[derive(Default)]
            struct Match {
                has_id: bool,
                id: u64,
                attributes: BTreeMap<String, (MvtValue, SerialVal)>,
                key_order: Vec<String>,
            }
            let mut matches: Vec<Match> = Vec::new();
            let mut matched = false;
            {
                let mut m = Match {
                    id: feat.id,
                    has_id: feat.has_id,
                    ..Default::default()
                };
                for t in (0..feat.tags.len().saturating_sub(1)).step_by(2) {
                    let key = &layer.keys[feat.tags[t] as usize];
                    let val = &layer.values[feat.tags[t + 1] as usize];
                    let sv = mvt_value_to_serial_val(val);
                    if val.type_ == MVT_NULL {
                        continue;
                    }
                    if !r.exclude_all_tile_attributes
                        && (r.include.contains(key)
                            || (!r.exclude_all
                                && !r.exclude.contains(key)
                                && !exclude_attributes.contains(key)))
                    {
                        m.attributes
                            .entry(key.clone())
                            .or_insert((val.clone(), sv.clone()));
                        m.key_order.push(key.clone());
                    }
                    if !r.header.is_empty()
                        && *key == r.header[0]
                        && let Some(fields) = r.mapping.get(&sv.s)
                    {
                        let fields = fields.clone();
                        matched = true;
                        for i in 1..fields.len() {
                            let joinkey = r.header[i].clone();
                            let mut joinval = fields[i].clone();
                            let mut attr_type = MVT_STRING;
                            if !joinval.is_empty() {
                                if joinval.as_bytes()[0] == b'"' {
                                    joinval = tippecanoe::csv::csv_dequote(joinval);
                                } else if tippecanoe::csv::is_number(&joinval) {
                                    attr_type = MVT_DOUBLE;
                                }
                            } else if r.pe {
                                attr_type = MVT_NULL;
                            }
                            if r.include.contains(&joinkey)
                                || (!r.exclude_all
                                    && !r.exclude.contains(&joinkey)
                                    && !exclude_attributes.contains(&joinkey)
                                    && attr_type != MVT_NULL)
                            {
                                let t = if attr_type == MVT_STRING {
                                    MVT_STRING
                                } else {
                                    MVT_DOUBLE
                                };
                                m.attributes.remove(&joinkey);
                                let outsv = SerialVal {
                                    type_: t,
                                    s: joinval.clone(),
                                };
                                let outval = stringified_to_mvt_value(t, &joinval);
                                m.attributes
                                    .entry(joinkey.clone())
                                    .or_insert((outval, outsv));
                                m.key_order.push(joinkey);
                            }
                        }
                    }
                }
                if matched {
                    matches.push(m);
                }
            }
            if !matched && !r.ifmatched {
                let mut m = Match {
                    id: feat.id,
                    has_id: feat.has_id,
                    ..Default::default()
                };
                if !r.exclude_all_tile_attributes {
                    for t in (0..feat.tags.len().saturating_sub(1)).step_by(2) {
                        let key = &layer.keys[feat.tags[t] as usize];
                        let val = &layer.values[feat.tags[t + 1] as usize];
                        let sv = mvt_value_to_serial_val(val);
                        if r.include.contains(key)
                            || (!r.exclude_all
                                && !r.exclude.contains(key)
                                && !exclude_attributes.contains(key))
                        {
                            m.attributes.entry(key.clone()).or_insert((val.clone(), sv));
                            m.key_order.push(key.clone());
                        }
                    }
                }
                matches.push(m);
            }

            for m in matches.iter_mut() {
                if !tilestats_found {
                    let n = a.layermap.len();
                    a.layermap.insert(layer.name.clone(), LayermapEntry::new(n));
                    tilestats_found = true;
                    let tilestats = a.layermap.get_mut(&layer.name).unwrap();
                    tilestats.minzoom = z;
                    tilestats.maxzoom = z;
                }
                let tilestats = a.layermap.get_mut(&layer.name).unwrap();
                let mut outfeature = MvtFeature {
                    id: m.id,
                    has_id: m.has_id,
                    ..Default::default()
                };
                for k in &m.key_order {
                    if let Some(fa) = m.attributes.remove(k) {
                        outlayer.tag(&mut outfeature, k, &fa.0);
                        add_to_tilestats(&mut tilestats.tilestats, k, &fa.1);
                    }
                }
                if r.exclude_all_tile_geometries {
                    outfeature.type_ = -1;
                } else {
                    outfeature.type_ = feat.type_;
                    outfeature.geometry = feat.geometry.clone();
                    if layer.extent != outlayer.extent {
                        for g in outfeature.geometry.iter_mut() {
                            g.x = g.x * outlayer.extent / layer.extent;
                            g.y = g.y * outlayer.extent / layer.extent;
                        }
                    }
                    for g in &outfeature.geometry {
                        if g.op == MVT_MOVETO || g.op == MVT_LINETO {
                            let mut gx = std::cmp::min(outlayer.extent, std::cmp::max(0, g.x));
                            let mut gy = std::cmp::min(outlayer.extent, std::cmp::max(0, g.y));
                            gx = gx * (1i64 << (32 - z)) / outlayer.extent;
                            gy = gy * (1i64 << (32 - z)) / outlayer.extent;
                            gx += (1i64 << (32 - z)) * x as i64;
                            gy += (1i64 << (32 - z)) * y as i64;
                            minx = std::cmp::min(minx, gx);
                            miny = std::cmp::min(miny, gy);
                            maxx = std::cmp::max(maxx, gx);
                            maxy = std::cmp::max(maxy, gy);
                            if gx < (1i64 << 31) {
                                gx += 1i64 << 32;
                            }
                            minx2 = std::cmp::min(minx2, gx);
                            maxx2 = std::cmp::max(maxx2, gx);
                        }
                    }
                }
                features_added_to_layer = true;
                outlayer.features.push(outfeature);
                if z < tilestats.minzoom {
                    tilestats.minzoom = z;
                }
                if z > tilestats.maxzoom {
                    tilestats.maxzoom = z;
                }
                if feat.type_ == MVT_POINT {
                    tilestats.points += 1;
                } else if feat.type_ == MVT_LINESTRING {
                    tilestats.lines += 1;
                } else if feat.type_ == MVT_POLYGON {
                    tilestats.polygons += 1;
                }
            }
        }
        if features_added_to_layer {
            use tippecanoe::projection::tile2lonlat;
            let (lon1, lat1) = tile2lonlat(minx, maxy, 32);
            let (lon2, lat2) = tile2lonlat(maxx, miny, 32);
            a.minlat = std_min(a.minlat, std_min(lat1, lat2));
            a.minlon = std_min(a.minlon, std_min(lon1, lon2));
            a.maxlat = std_max(a.maxlat, std_max(lat1, lat2));
            a.maxlon = std_max(a.maxlon, std_max(lon1, lon2));
            let (lon1, _) = tile2lonlat(minx2, maxy, 32);
            let (lon2, _) = tile2lonlat(maxx2, miny, 32);
            a.minlon2 = std_min(a.minlon2, std_min(lon1, lon2));
            a.maxlon2 = std_max(a.maxlon2, std_max(lon1, lon2));
        }
    }
}

/// tile-join's `join_worker` for one tile.
fn reference_join(
    inputs: &[Vec<u8>],
    z: i32,
    x: u32,
    y: u32,
    r: &TestRules,
    a: &mut Collected,
) -> Option<Vec<u8>> {
    let mut tile = MvtTile::default();
    for message in inputs {
        let mut t = MvtTile::default();
        let mut was_compressed = false;
        assert!(t.try_decode(message, &mut was_compressed).unwrap());
        append_tile(&mut t, z, x, y, r, &mut tile, a);
    }
    let mut outtile = MvtTile::default();
    for layer in tile.layers {
        if !layer.features.is_empty() {
            outtile.layers.push(layer);
        }
    }
    if outtile.layers.is_empty() {
        None
    } else {
        Some(outtile.encode())
    }
}

fn rules_for(r: &TestRules) -> Rules {
    let csv = RawCsv {
        header: r.header.clone(),
        rows: r
            .mapping
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect(),
    }
    .prepare(r.pe);
    Rules {
        header: csv.header,
        mapping: csv.mapping,
        exclude: r.exclude.clone(),
        include: r.include.clone(),
        keep_layers: r.keep_layers.clone(),
        remove_layers: r.remove_layers.clone(),
        renames: r.renames.clone(),
        ifmatched: r.ifmatched,
        exclude_all: r.exclude_all,
        exclude_all_tile_attributes: r.exclude_all_tile_attributes,
        exclude_all_tile_geometries: r.exclude_all_tile_geometries,
        filter: None,
        unidecode_data: Vec::new(),
    }
}

fn plain_rules() -> TestRules {
    TestRules {
        header: Vec::new(),
        mapping: BTreeMap::new(),
        exclude: BTreeSet::new(),
        include: BTreeSet::new(),
        keep_layers: BTreeSet::new(),
        remove_layers: BTreeSet::new(),
        renames: BTreeMap::new(),
        ifmatched: false,
        exclude_all: false,
        exclude_all_tile_attributes: false,
        exclude_all_tile_geometries: false,
        pe: false,
    }
}

fn some_rules(i: usize) -> TestRules {
    let set = |v: &[&str]| {
        v.iter()
            .map(|s| s.to_string())
            .collect::<BTreeSet<String>>()
    };
    let mut r = plain_rules();
    match i % 9 {
        0 => {}
        1 => r.exclude = set(&["name", "pop"]),
        2 => {
            r.exclude_all = true;
            r.include = set(&["kind"]);
        }
        3 => r.exclude_all_tile_attributes = true,
        4 => r.exclude_all_tile_geometries = true,
        5 => {
            r.keep_layers = set(&["roads", "water"]);
            r.renames.insert("water".to_string(), "roads".to_string());
        }
        6..=8 => {
            r.header = ["kind", "joined", "number", "empty", "quoted"]
                .iter()
                .map(|s| s.to_string())
                .collect();
            for v in 0..40 {
                if v % 3 == 0 {
                    continue;
                }
                let key = format!("v{}", v);
                r.mapping.insert(
                    key.clone(),
                    vec![
                        key,
                        format!("j{}", v % 7),
                        format!("{}.5", v),
                        String::new(),
                        "\"a \"\"b\"\"\"".to_string(),
                    ],
                );
            }
            r.ifmatched = i % 9 == 7;
            r.pe = i % 9 == 8;
            if i % 9 == 8 {
                r.exclude = set(&["number"]);
            }
        }
        _ => unreachable!(),
    }
    r.remove_layers = if i % 11 == 10 {
        set(&["places"])
    } else {
        BTreeSet::new()
    };
    r
}

#[test]
fn joining_matches_tile_join() {
    // tile-stats read tippecanoe-rs's globals, which other tests set
    let _g = crate::tiler::oracle::GLOBALS
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    tippecanoe::globals::MAX_TILESTATS_SAMPLE_VALUES
        .store(1000, std::sync::atomic::Ordering::Relaxed);
    let mut rng = Rng(7);
    for i in 0..400 {
        let n = 1 + rng.below(3) as usize;
        let inputs: Vec<Vec<u8>> = (0..n)
            .map(|k| random_tile(&mut rng, i % 97 == 0 && k == 0))
            .collect();
        let (z, x, y) = (
            rng.below(14) as i32,
            rng.below(1 << 13) as u32,
            rng.below(1 << 13) as u32,
        );
        let (x, y) = (x % (1 << z), y % (1 << z));
        let r = some_rules(i);

        let mut collected = Collected {
            layermap: BTreeMap::new(),
            minlat: i32::MAX as f64,
            minlon: i32::MAX as f64,
            maxlat: i32::MIN as f64,
            maxlon: i32::MIN as f64,
            minlon2: i32::MAX as f64,
            maxlon2: i32::MIN as f64,
        };
        let expected = reference_join(&inputs, z, x, y, &r, &mut collected);

        let rules = rules_for(&r);
        let mut acc = Accum::new(1000);
        let mut scratch = Scratch::default();
        let refs: Vec<&[u8]> = inputs.iter().map(|v| &v[..]).collect();
        let mut buffers = vec![Vec::new(); refs.len()];
        let mut d = libdeflater::Decompressor::new();
        let decoded = tile::decode_all(&refs, &mut buffers, &mut d, z, x, y).unwrap();
        let mut pbf = Vec::new();
        let anything =
            tile::join_tile(&decoded, z, x, y, &rules, &mut acc, &mut scratch, &mut pbf).unwrap();
        assert_eq!(anything, expected.is_some(), "case {}", i);
        if let Some(expected) = expected {
            assert!(pbf == expected, "case {}: tiles differ", i);
        }

        let ours = merge_layermaps(&[acc.layermap()]);
        let theirs = merge_layermaps(&[collected.layermap]);
        assert_eq!(format!("{:?}", ours), format!("{:?}", theirs), "case {}", i);
        assert_eq!(
            (
                acc.minlat,
                acc.minlon,
                acc.maxlat,
                acc.maxlon,
                acc.minlon2,
                acc.maxlon2
            ),
            (
                collected.minlat,
                collected.minlon,
                collected.maxlat,
                collected.maxlon,
                collected.minlon2,
                collected.maxlon2
            ),
            "case {}",
            i
        );
    }
}

#[test]
fn values_hash_and_compare_as_tippecanoe_does() {
    let vals = [
        Val::Str("a".into()),
        Val::Str("a\0b".into()),
        Val::Float(1.5),
        Val::Float(-0.0),
        Val::Double(1.5),
        Val::Double(f64::NAN),
        Val::Int(-3),
        Val::Uint(3),
        Val::Sint(-3),
        Val::Bool(true),
        Val::Bool(false),
    ];
    for a in &vals {
        // one layer with the value tagged twice: the dedup and the encoding
        let mut ours = MvtLayer::default();
        let mut f = MvtFeature::default();
        ours.tag(&mut f, "k", &a.to_mvt());
        ours.tag(&mut f, "k", &a.to_mvt());
        assert_eq!(Val::from_mvt(&a.to_mvt()).mvt_type(), a.mvt_type());
        for b in &vals {
            assert_eq!(
                a.to_mvt() == b.to_mvt(),
                tile::same_for_test(a, b),
                "{:?} {:?}",
                a,
                b
            );
        }
        let sv = mvt_value_to_serial_val(&a.to_mvt());
        let (t, s) = a.serial();
        assert_eq!((sv.type_, sv.s.as_str()), (t, s.as_ref()));
    }
}

#[test]
fn csv_rows_too_long_are_errors() {
    let r = TestRules {
        header: vec!["kind".to_string(), "a".to_string()],
        mapping: [(
            "v1".to_string(),
            vec!["v1".to_string(), "x".to_string(), "y".to_string()],
        )]
        .into_iter()
        .collect(),
        ..plain_rules()
    };
    let rules = rules_for(&r);
    let mut tile = Vec::new();
    let mut w = PbfWriter::new(&mut tile);
    let mut layer = Vec::new();
    let mut lw = PbfWriter::new(&mut layer);
    lw.add_uint32(15, 2);
    lw.add_string(1, b"l");
    lw.add_uint32(5, 4096);
    lw.add_string(3, b"kind");
    let mut v = Vec::new();
    PbfWriter::new(&mut v).add_string(1, b"v1");
    lw.add_message(4, &v);
    let mut f = Vec::new();
    let mut fw = PbfWriter::new(&mut f);
    fw.add_enum(3, 1);
    fw.add_packed_uint32(2, [0u32, 0]);
    fw.add_packed_uint32(4, [9u32, 2, 2]);
    lw.add_message(2, &f);
    w.add_message(3, &layer);

    let decoded = vec![Decoded::decode(&tile).unwrap()];
    let mut acc = Accum::new(1000);
    let e = tile::join_tile(
        &decoded,
        0,
        0,
        0,
        &rules,
        &mut acc,
        &mut Scratch::default(),
        &mut Vec::new(),
    )
    .err()
    .unwrap();
    assert_eq!(e.exit_code, tippecanoe::errors::EXIT_CSV);
}

#[test]
fn zlib_keys_order_as_zlib_bytes() {
    let mut rng = Rng(3);
    let tiles: Vec<Vec<u8>> = (0..60).map(|_| random_tile(&mut rng, false)).collect();
    let zlib: Vec<Vec<u8>> = tiles
        .iter()
        .map(|t| crate::compress::gzip(t, false))
        .collect();
    let libdeflate: Vec<Vec<u8>> = tiles
        .iter()
        .map(|t| crate::compress::gzip(t, true))
        .collect();
    for (i, l) in libdeflate.iter().enumerate() {
        assert!(is_libdeflate_gzip(l));
        assert!(!is_libdeflate_gzip(&zlib[i]));
        for (j, z) in zlib.iter().enumerate() {
            // a manby tile against a tippecanoe one
            let a = Key::new(l.clone().into(), true, TieBreak::Zlib);
            let b = Key::new(z.clone().into(), true, TieBreak::Zlib);
            assert_eq!(cmp(&a, &b), zlib[i].cmp(z), "{} {}", i, j);
            // two manby tiles
            let c = Key::new(libdeflate[j].clone().into(), true, TieBreak::Zlib);
            let a = Key::new(l.clone().into(), true, TieBreak::Zlib);
            assert_eq!(cmp(&a, &c), zlib[i].cmp(&zlib[j]), "{} {}", i, j);
            // the bytes as stored, for other tools' tiles and on request
            let a = Key::new(l.clone().into(), false, TieBreak::Zlib);
            let c = Key::new(libdeflate[j].clone().into(), true, TieBreak::Bytes);
            assert_eq!(cmp(&a, &c), l.cmp(&libdeflate[j]), "{} {}", i, j);
            // against uncompressed tiles, decided by the header
            let a = Key::new(l.clone().into(), true, TieBreak::Zlib);
            let d = Key::new(tiles[j].clone().into(), true, TieBreak::Zlib);
            assert_eq!(cmp(&a, &d), zlib[i].cmp(&tiles[j]), "{} {}", i, j);
        }
    }
}

#[test]
fn values_out_of_order_with_csv_text() {
    // the joined values keep the text tile-stats record for them
    let raw = RawCsv {
        header: vec!["k".to_string(), "n".to_string()],
        rows: [("a".to_string(), vec!["a".to_string(), "1.50".to_string()])]
            .into_iter()
            .collect::<HashMap<_, _>>(),
    };
    let csv = raw.prepare(false);
    let f = &csv.mapping["a"].fields[0];
    assert_eq!(f.text, "1.50");
    assert_eq!(f.sv_type, MVT_DOUBLE);
    assert!(matches!(f.value, Val::Float(v) if v == 1.5));
}

#[test]
fn batches_never_split_a_tile_and_order_by_zlib_bytes() {
    use super::merge::{Queue, Reader, Settings};
    use super::source::Source;
    let _g = crate::tiler::oracle::GLOBALS
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    let tests = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("crates/tippecanoe-rs/tests");
    let dir = std::env::temp_dir().join(format!("manby-join-batches-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let mut names = Vec::new();
    for (out, input) in [
        ("a.pmtiles", "ne_110m_ocean/in.json"),
        ("b.pmtiles", "ne_110m_admin_0_countries/in.json.gz"),
        ("c.pmtiles", "ne_110m_populated_places/in.json"),
    ] {
        let out = dir.join(out).to_string_lossy().into_owned();
        let argv: Vec<String> = ["tippecanoe", "-q", "-f", "-z3", "-o", &out]
            .iter()
            .map(|s| s.to_string())
            .chain([tests.join(input).to_string_lossy().into_owned()])
            .collect();
        crate::run(&crate::options::parse_args(&argv).unwrap()).unwrap();
        names.push(out);
    }
    let settings = Settings {
        want_overzoom: false,
        buffer: 5,
        tie_break: TieBreak::Zlib,
    };
    let run = |limit: usize| {
        let sources: Vec<Source> = names
            .iter()
            .map(|n| Source::open(n, 0, 32).unwrap())
            .collect();
        let mut queue = Queue::new();
        for (i, s) in sources.iter().enumerate() {
            queue
                .add(Reader::begin(s, i, &settings).unwrap(), &settings)
                .unwrap();
        }
        let mut all = Vec::new();
        loop {
            let mut groups = Vec::new();
            let more = queue.run(&settings, 0, 32, limit, &mut groups).unwrap();
            for g in groups {
                // in the order of the bytes zlib makes of the tiles, as
                // tile-join orders tippecanoe's tiles
                let zlib: Vec<Vec<u8>> = g
                    .members
                    .iter()
                    .map(|(i, t)| {
                        let data = sources[*i].data(t).unwrap();
                        let mut content = Vec::new();
                        assert!(tile::decompress(
                            &mut libdeflater::Decompressor::new(),
                            &data,
                            &mut content
                        ));
                        crate::compress::gzip(&content, false)
                    })
                    .collect();
                assert!(zlib.windows(2).all(|w| w[0] <= w[1]));
                all.push((
                    g.z,
                    g.x,
                    g.y,
                    g.members.iter().map(|m| m.0).collect::<Vec<_>>(),
                ));
            }
            if !more {
                break;
            }
        }
        all
    };
    let whole = run(usize::MAX);
    assert!(whole.iter().any(|g| g.3.len() == 3));
    // the inputs disagree with their own order somewhere
    assert!(whole.iter().any(|g| g.3.windows(2).any(|w| w[0] > w[1])));
    assert_eq!(run(1), whole);
    assert_eq!(run(7), whole);
    let _ = std::fs::remove_dir_all(&dir);
}

/// An unchanged tile keeps its compression only if zlib or libdeflate made
/// it; any other gzip stream is compressed again.
#[test]
fn only_zlib_and_libdeflate_streams_pass_through() {
    let _g = crate::tiler::oracle::GLOBALS
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    let dir = std::env::temp_dir().join(format!("manby-join-through-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    let input = dir.join("in");

    // tiles as a join writes them, so that joining them again changes nothing
    let mut rng = Rng(11);
    let mut tiles = Vec::new();
    while tiles.len() < 4 {
        let raw = random_tile(&mut rng, false);
        let decoded = vec![Decoded::decode(&raw).unwrap()];
        let mut pbf = Vec::new();
        let rules = rules_for(&plain_rules());
        let mut acc = Accum::new(1000);
        if tile::join_tile(
            &decoded,
            3,
            1,
            2,
            &rules,
            &mut acc,
            &mut Scratch::default(),
            &mut pbf,
        )
        .unwrap()
        {
            tiles.push(pbf);
        }
    }
    let zlib = crate::compress::gzip(&tiles[0], false);
    let libdeflate = crate::compress::gzip(&tiles[1], true);
    // zlib's stream under another system's header, as Java's GZIPOutputStream
    // writes it, and with a modification time
    let mut other_os = crate::compress::gzip(&tiles[2], false);
    other_os[9] = 0;
    let mut mtime = crate::compress::gzip(&tiles[3], false);
    mtime[4] = 1;
    let stored = [zlib, libdeflate, other_os, mtime];
    for (i, data) in stored.iter().enumerate() {
        let d = input.join(format!("3/{}", i));
        std::fs::create_dir_all(&d).unwrap();
        std::fs::write(d.join("2.pbf"), data).unwrap();
    }
    std::fs::write(
        input.join("metadata.json"),
        r#"{"name": "in", "generator": "tippecanoe v2.82.0"}"#,
    )
    .unwrap();

    let output = dir.join("out");
    let o = super::JoinOptions {
        inputs: vec![input.to_string_lossy().into_owned()],
        output: crate::options::Output::Directory(output.to_string_lossy().into_owned()),
        quiet: true,
        ..Default::default()
    };
    super::join(&o).unwrap();

    for (i, data) in stored.iter().enumerate() {
        let out = std::fs::read(output.join(format!("3/{}/2.pbf", i))).unwrap();
        let mut back = Vec::new();
        assert!(tile::decompress(
            &mut libdeflater::Decompressor::new(),
            &out,
            &mut back
        ));
        assert_eq!(back, tiles[i], "tile {}", i);
        assert_eq!(out == *data, i < 2, "tile {}", i);
        assert!(
            out[..10] == *super::key::zlib_header() || out[..10] == super::key::LIBDEFLATE_HEADER,
            "tile {}",
            i
        );
    }
    let _ = std::fs::remove_dir_all(&dir);
}

/// Overzooming makes what tippecanoe-rs's `overzoom_source_tiles` makes with
/// tile-join's arguments, children included.
#[test]
fn overzooming_matches_tippecanoe() {
    use std::collections::HashMap;
    let mut rng = Rng(5);
    let mut cases = 0;
    for i in 0..120 {
        let data = random_tile(&mut rng, i % 40 == 0);
        let ours = Decoded::decode(&data).unwrap();
        let mut theirs = MvtTile::default();
        let mut was_compressed = false;
        assert!(theirs.try_decode(&data, &mut was_compressed).unwrap());
        let (sz, sx, sy) = (3, 1i64, 2i64);
        let depth = 1 + rng.below(3) as i32;
        let n = 1i64 << depth;
        let (dx, dy) = (rng.below(n as u64) as i64, rng.below(n as u64) as i64);
        let dst = (sz + depth, sx * n + dx, sy * n + dy);
        for buffer in [0, 5, 64] {
            let mut our_children = Vec::new();
            let pbf =
                super::overzoom::overzoom(&ours, (sz, sx, sy), dst, buffer, &mut our_children)
                    .unwrap();
            let mut their_children = Vec::new();
            let expected = tippecanoe::clip::overzoom_source_tiles(
                &[tippecanoe::geometry::SourceTile {
                    tile: theirs.clone(),
                    z: sz,
                    x: sx as i32,
                    y: sy as i32,
                }],
                dst.0,
                dst.1 as i32,
                dst.2 as i32,
                -1,
                buffer,
                &BTreeSet::new(),
                &BTreeSet::new(),
                &[],
                false,
                Some(&mut their_children),
                false,
                None,
                false,
                &HashMap::new(),
                &[],
                0.0,
                0.0,
                &[],
                "",
                "",
                usize::MAX,
                &[],
                false,
            );
            assert!(pbf == expected, "case {} buffer {}", i, buffer);
            assert_eq!(our_children, their_children, "case {} buffer {}", i, buffer);
            cases += !expected.is_empty() as usize;
        }
    }
    assert!(cases > 100);
}

/// The text tile-stats keep for a number reads back, with `atof`, as the
/// number `Val::number` gives, bit for bit.
#[test]
fn numbers_read_back_from_their_text() {
    let mut rng = Rng(9);
    let mut vals = vec![
        Val::Double(0.0),
        Val::Double(-0.0),
        Val::Double(f64::MAX),
        Val::Double(f64::MIN_POSITIVE),
        Val::Double(5e-324),
        Val::Double(0.1),
        Val::Double(1e21),
        Val::Double(123456789012345680.0),
        Val::Float(0.1),
        Val::Float(-0.0),
        Val::Float(f32::MAX),
        Val::Float(f32::MIN_POSITIVE),
        Val::Int(i64::MIN),
        Val::Int(i64::MAX),
        Val::Sint(-9007199254740993),
        Val::Uint(u64::MAX),
        Val::Uint(9007199254740993),
    ];
    for _ in 0..200_000 {
        let bits = (rng.next() << 33) ^ (rng.next() << 2) ^ rng.next();
        let d = f64::from_bits(bits);
        if d.is_finite() {
            vals.push(Val::Double(d));
        }
        let f = f32::from_bits(bits as u32);
        if f.is_finite() {
            vals.push(Val::Float(f));
        }
        vals.push(Val::Int(bits as i64));
        vals.push(Val::Uint(bits.rotate_left(17)));
        vals.push(Val::Double((bits % 1_000_000) as f64 / 1000.0));
    }
    for v in &vals {
        let (_, s) = v.serial();
        let n = v.number().unwrap();
        assert_eq!(
            tippecanoe::cstd::atof(&s).to_bits(),
            n.to_bits(),
            "{:?} as {}",
            v,
            s
        );
    }
    assert_eq!(Val::Double(f64::NAN).number(), None);
    assert_eq!(Val::Float(f32::INFINITY).number(), None);
}