utiles 0.8.0

Web map tile utils (aka utiles)
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
use clap::{Args, Parser, Subcommand};
use std::path::PathBuf;
use strum_macros::AsRefStr;

use utiles_core::{
    BBox, LngLat, TileStringFormatter, VERSION, ZoomSet, geobbox_merge,
    parsing::parse_bbox_ext, zoom,
};

use crate::cli::commands::dev::DevArgs;
use crate::cli::commands::serve::ServeArgs;
use crate::cli::commands::shapes::ShapesArgs;
use crate::cli::commands::{analyze_main, header_main, vacuum_main};
use crate::copy::CopyConfig;
use crate::errors::UtilesResult;
use crate::hash_types::HashType;
use crate::mbt::{MbtType, TilesFilter};
use crate::sqlite::InsertStrategy;

// ██╗   ██╗████████╗██╗██╗     ███████╗███████╗
// ██║   ██║╚══██╔══╝██║██║     ██╔════╝██╔════╝
// ██║   ██║   ██║   ██║██║     █████╗  ███████╗
// ██║   ██║   ██║   ██║██║     ██╔══╝  ╚════██║
// ╚██████╔╝   ██║   ██║███████╗███████╗███████║
//  ╚═════╝    ╚═╝   ╚═╝╚══════╝╚══════╝╚══════╝

/// utiles cli (rust) ~ v{VERSION}
fn about() -> String {
    let is_debug: bool = cfg!(debug_assertions);
    if is_debug {
        format!("utiles cli (rust) ~ v{VERSION} ~ DEBUG")
    } else {
        format!("utiles cli (rust) ~ v{VERSION}")
    }
}

#[derive(Debug, Parser)]
pub struct AboutArgs {
    /// output `json`
    #[arg(required = false, long, short, action = clap::ArgAction::SetTrue,)]
    pub json: bool,
}

#[derive(Debug, Parser)]
#[command(name = "ut", about = about(), version = VERSION, author, max_term_width = 120)]
pub struct Cli {
    /// debug mode (print/log more)
    #[arg(long, global = true, default_value = "false", action = clap::ArgAction::SetTrue)]
    pub debug: bool,

    /// trace mode (print/log EVEN more)
    #[arg(long, global = true, default_value = "false", action = clap::ArgAction::SetTrue)]
    pub trace: bool,

    /// format log as NDJSON
    #[arg(long, global = true, default_value = "false", action = clap::ArgAction::SetTrue)]
    pub log_json: bool,

    /// CLI subcommands
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Debug, Parser)]
pub struct TileInputStreamArgs {
    #[arg(required = false)]
    pub input: Option<String>,
}

fn tile_fmt_string_long_help() -> String {
    r#"Format string for tiles (default: `{json_arr}`)

Example:
    > utiles tiles 1 * --fmt "http://thingy.com/{z}/{x}/{y}.png"
    http://thingy.com/1/0/0.png
    http://thingy.com/1/0/1.png
    http://thingy.com/1/1/0.png
    http://thingy.com/1/1/1.png
    > utiles tiles 1 * --fmt "SELECT * FROM tiles WHERE zoom_level = {z} AND tile_column = {x} AND tile_row = {-y};"
    SELECT * FROM tiles WHERE zoom_level = 1 AND tile_column = 0 AND tile_row = 1;
    SELECT * FROM tiles WHERE zoom_level = 1 AND tile_column = 0 AND tile_row = 0;
    SELECT * FROM tiles WHERE zoom_level = 1 AND tile_column = 1 AND tile_row = 1;
    SELECT * FROM tiles WHERE zoom_level = 1 AND tile_column = 1 AND tile_row = 0;

fmt-tokens:
    `{json_arr}`/`{json}`  -> [x, y, z]
    `{json_obj}`/`{obj}`   -> {x: x, y: y, z: z}
    `{quadkey}`/`{qk}`     -> quadkey string
    `{pmtileid}`/`{pmid}`  -> pmtile-id
    `{x}`                  -> x tile coord
    `{y}`                  -> y tile coord
    `{z}`                  -> z/zoom level
    `{-y}`/`{yup}`         -> y tile coord flipped/tms
    `{zxy}`                -> z/x/y
    `{bbox}`               -> [w, s, e, n] bbox lnglat (wgs84)
    `{projwin}`            -> ulx,uly,lrx,lry projwin 4 gdal (wgs84)
    `{bbox_web}`           -> [w, s, e, n] bbox web-mercator (epsg:3857)
    `{projwin_web}`        -> ulx,uly,lrx,lry projwin 4 gdal (epsg:3857)
    "#
        .to_string()
}
#[derive(Debug, Parser)]
pub struct TileFmtOptions {
    /// Write tiles as RS-delimited JSON sequence
    #[arg(required = false, long, action = clap::ArgAction::SetTrue)]
    pub seq: bool,

    /// Format tiles as json objects (equiv to `-F/--fmt "{json_obj}"`)
    #[arg(required = false, long, action = clap::ArgAction::SetTrue)]
    pub obj: bool,

    /// Format string for tiles (default: `{json_arr}`); see --help for more
    #[arg(
        required = false,
        long,
        alias = "fmt",
        short = 'F',
        conflicts_with = "obj",
        long_help = tile_fmt_string_long_help()
    )]
    pub fmt: Option<String>,
}

impl TileFmtOptions {
    #[must_use]
    pub fn formatter(&self) -> TileStringFormatter {
        if let Some(fmt) = &self.fmt {
            TileStringFormatter::new(fmt)
        } else if self.obj {
            TileStringFormatter::new("{json_obj}")
        } else {
            TileStringFormatter::default()
        }
    }
}
impl From<&TileFmtOptions> for TileStringFormatter {
    fn from(opts: &TileFmtOptions) -> Self {
        opts.formatter()
    }
}

#[derive(Debug, Parser)]
pub struct TilesArgs {
    /// Zoom level (0-30)
    #[arg(required = true, value_parser = clap::value_parser ! (u8).range(0..=30))]
    pub zoom: u8,

    #[command(flatten)]
    pub inargs: TileInputStreamArgs,

    #[command(flatten)]
    pub fmtopts: TileFmtOptions,
}

#[derive(Debug, Parser)]
pub struct TileFmtArgs {
    #[command(flatten)]
    pub inargs: TileInputStreamArgs,

    #[command(flatten)]
    pub fmtopts: TileFmtOptions,
}
#[derive(Debug, Parser)]
pub struct EdgesArgs {
    /// Wrap x/longitude across antimeridian (default: false)
    #[arg(required = false, long, action = clap::ArgAction::SetTrue,)]
    pub wrapx: bool,

    #[command(flatten)]
    pub inargs: TileInputStreamArgs,

    #[command(flatten)]
    pub fmtopts: TileFmtOptions,
}

#[derive(Debug, Parser)]
pub struct BurnArgs {
    /// Zoom level (0-30)
    #[arg(required = true, value_parser = clap::value_parser ! (u8).range(0..=30))]
    pub zoom: u8,

    #[command(flatten)]
    pub inargs: TileInputStreamArgs,

    #[command(flatten)]
    pub fmtopts: TileFmtOptions,
}

#[derive(Debug, Parser)]
pub struct MergeArgs {
    /// min zoom level (0-30) to merge to
    #[arg(long, short = 'Z', default_value = "0")]
    pub minzoom: u8,

    #[arg(required = false, short, long, action = clap::ArgAction::SetTrue)]
    pub sort: bool,

    #[command(flatten)]
    pub inargs: TileInputStreamArgs,

    #[command(flatten)]
    pub fmtopts: TileFmtOptions,
}

#[derive(Debug, Parser)]
pub struct FmtStrArgs {
    #[command(flatten)]
    pub inargs: TileInputStreamArgs,

    #[command(flatten)]
    pub fmtopts: TileFmtOptions,
}

#[derive(Debug, Parser)]
pub struct ParentChildrenArgs {
    #[command(flatten)]
    pub inargs: TileInputStreamArgs,

    #[command(flatten)]
    pub fmtopts: TileFmtOptions,

    #[arg(required = false, long, default_value = "1")]
    pub depth: u8,
}

#[derive(Debug, Parser)]
pub struct SqliteDbCommonArgs {
    /// sqlite filepath
    #[arg(required = true)]
    pub filepath: String,

    /// compact/minified json (default: false)
    #[arg(required = false, short, long, action = clap::ArgAction::SetTrue)]
    pub min: bool,
}

#[derive(Debug, Parser)]
pub struct TilesFilterArgs {
    /// bbox(es) (west, south, east, north)
    #[arg(required = false, long, value_parser = parse_bbox_ext, allow_hyphen_values = true)]
    pub bbox: Option<Vec<BBox>>,

    #[command(flatten)]
    pub zoom: Option<ZoomArgGroup>,
}

impl TilesFilterArgs {
    #[must_use]
    pub fn zooms(&self) -> Option<Vec<u8>> {
        match &self.zoom {
            Some(zoom) => zoom.zooms(),
            None => None,
        }
    }

    #[must_use]
    pub fn bboxes(&self) -> Option<Vec<BBox>> {
        self.bbox.clone()
    }

    #[must_use]
    pub fn tiles_filter_maybe(&self) -> Option<TilesFilter> {
        if self.bbox.is_none() && self.zoom.is_none() {
            None
        } else {
            Some(TilesFilter::new(self.bboxes(), self.zooms()))
        }
    }
}

#[derive(Debug, Parser, Clone, clap::ValueEnum)]
pub enum DbtypeOption {
    Flat,
    Hash,
    Norm,
}

impl From<&DbtypeOption> for MbtType {
    fn from(opt: &DbtypeOption) -> Self {
        match opt {
            DbtypeOption::Flat => Self::Flat,
            DbtypeOption::Hash => Self::Hash,
            DbtypeOption::Norm => Self::Norm,
        }
    }
}

#[derive(Debug, Parser, Clone)]
pub struct TouchArgs {
    /// mbtiles filepath
    #[arg(required = true)]
    pub filepath: String,

    /// page size
    #[arg(required = false, long)]
    pub page_size: Option<i64>,

    /// db-type (default: flat)
    #[arg(
        required = false, long = "dbtype", aliases = ["db-type", "mbtype", "mbt-type"], default_value = "flat"
    )]
    pub dbtype: Option<DbtypeOption>,
}

impl TouchArgs {
    #[must_use]
    pub fn mbtype(&self) -> MbtType {
        self.dbtype.as_ref().map_or(MbtType::Flat, |opt| opt.into())
    }
}

#[derive(Debug, Subcommand)]
/// sqlite utils/cmds
pub enum SqliteCommands {
    Analyze(AnalyzeArgs),
    Header(SqliteHeaderArgs),
    Vacuum(VacuumArgs),
}

impl SqliteCommands {
    pub async fn run(&self) -> UtilesResult<()> {
        match self {
            Self::Analyze(args) => analyze_main(args).await,
            Self::Header(args) => header_main(args).await,
            Self::Vacuum(args) => vacuum_main(args).await,
        }
    }
}

// #[derive(Debug, Parser)]
// pub struct SqliteSchemaArgs {
//     #[command(flatten)]
//     pub common: SqliteDbCommonArgs,
// }

#[derive(Debug, Parser)]
/// Analyze sqlite db
pub struct AnalyzeArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,

    #[arg(required = false, long)]
    pub analysis_limit: Option<usize>,
}

#[derive(Debug, Parser)]
/// Dump sqlite db header
pub struct SqliteHeaderArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,
}

#[derive(Debug, Parser)]
/// vacuum sqlite db inplace/into
pub struct VacuumArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,

    /// fspath to vacuum db into
    #[arg(required = false)]
    pub into: Option<String>,

    /// Analyze db after vacuum
    #[arg(required = false, long, short, action = clap::ArgAction::SetTrue)]
    pub analyze: bool,

    /// page size to set
    #[arg(required = false, long)]
    pub page_size: Option<i64>,
}

#[derive(Debug, Parser)]
pub struct MetadataArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,

    /// Output as json object not array
    #[arg(required = false, long, action = clap::ArgAction::SetTrue)]
    pub obj: bool,

    /// Output as json string for values (default: false)
    #[arg(required = false, long, action = clap::ArgAction::SetTrue, default_value = "false")]
    pub raw: bool,
}

#[derive(Debug, Parser)]
pub struct MetadataSetArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,

    /// key or json-fspath
    #[arg(required = true, value_name = "KEY/FSPATH")]
    pub key: String,

    /// value
    #[arg(required = false)]
    pub value: Option<String>,

    /// dryrun (don't actually set)
    #[arg(
        required = false, long, aliases = ["dry-run"], short = 'n', action = clap::ArgAction::SetTrue
    )]
    pub dryrun: bool,
}

#[derive(Debug, Parser)]
pub struct TilejsonArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,

    /// include tilestats
    #[arg(required = false, short, long, action = clap::ArgAction::SetTrue)]
    pub tilestats: bool,
}

#[derive(Debug, Parser)]
pub struct LintArgs {
    /// filepath(s) or dirpath(s)
    #[arg(required = true, num_args(1..))]
    pub(crate) fspaths: Vec<String>,

    /// fix lint errors (NOT IMPLEMENTED)
    #[arg(
        required = false, long, action = clap::ArgAction::SetTrue,
        default_value = "false", hide = true
    )]
    pub(crate) fix: bool,
}

#[derive(Debug, Parser)]
pub struct InfoArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,

    #[arg(required = false, long, action = clap::ArgAction::SetTrue)]
    pub(crate) full: bool,

    #[arg(required = false, long, short, visible_aliases = ["stats"], action = clap::ArgAction::SetTrue)]
    pub(crate) statistics: bool,
}

#[derive(Debug, Parser)]
pub struct AggHashArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,

    #[command(flatten)]
    pub filter_args: TilesFilterArgs,
    // /// bbox(es) (west, south, east, north)
    // #[arg(required = false, long, value_parser = parse_bbox_ext, allow_hyphen_values = true)]
    // pub bbox: Option<Vec<BBox>>,
    /// hash to use for blob-id if copying to normal/hash db type
    #[arg(required = false, long)]
    pub hash: Option<HashType>,
}

#[derive(Debug, Parser)]
pub struct UpdateArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,

    /// dryrun (don't actually update)
    #[arg(required = false, long, short = 'n', action = clap::ArgAction::SetTrue)]
    pub(crate) dryrun: bool,
}
#[derive(Debug, Parser)]
pub struct ZxyifyArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,

    /// un-zxyify a db
    #[arg(required = false, long, action = clap::ArgAction::SetTrue)]
    pub(crate) rm: bool,
}
#[derive(Debug, Parser)]
#[command(
    name = "enumerate",
    about = "enumerate db tiles like `tippecanoe-enumerate`"
)]
pub struct EnumerateArgs {
    #[arg(required = true)]
    pub(crate) fspaths: Vec<String>,

    #[command(flatten)]
    pub filter_args: TilesFilterArgs,

    /// tippecanoe-enumerate like output '{relpath} {x} {y} {z}'
    #[arg(required = false, long, short = 't', action = clap::ArgAction::SetTrue)]
    pub(crate) tippecanoe: bool,
}

#[derive(Debug, Parser)]
/// Optimize tiles-db
pub struct OptimizeArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,

    /// destination dataset fspath (mbtiles, dirpath)
    #[arg(required = true)]
    pub dst: String,
}

// #[derive(Debug, Parser)]
// pub struct OxipngArgs {
//     #[command(flatten)]
//     pub common: SqliteDbCommonArgs,
//
//     /// destination dataset fspath (mbtiles, dirpath)
//     #[arg(required = true)]
//     pub dst: String,
//
//     /// optimize level
//     #[arg(required = false, long, short, default_value = "2")]
//     pub(crate) opt: u8,
//
//     /// n-jobs ~ 0=ncpus (default: max(4, ncpus))
//     #[arg(required = false, long, short)]
//     pub jobs: Option<u8>,
//
//     /// quiet
//     #[arg(required = false, long, short, action = clap::ArgAction::SetTrue)]
//     pub(crate) quiet: bool,
// }

#[derive(Debug, Parser)]
pub struct WebpifyArgs {
    #[command(flatten)]
    pub common: SqliteDbCommonArgs,

    /// destination dataset fspath (mbtiles, dirpath)
    #[arg(required = true)]
    pub dst: String,

    /// n-jobs ~ 0=ncpus (default: max(4, ncpus))
    #[arg(required = false, long, short)]
    pub jobs: Option<u8>,

    /// quiet
    #[arg(required = false, long, short, action = clap::ArgAction::SetTrue)]
    pub(crate) quiet: bool,
}

#[derive(Debug, Parser)]
pub struct CommandsArgs {
    #[arg(required = false, short, long, action = clap::ArgAction::SetTrue)]
    pub full: bool,

    #[arg(required = false, short, long, action = clap::ArgAction::SetTrue)]
    pub(crate) table: bool,
    // /// compact/minified json (default: false)
    // #[arg(required = false, short, long, action = clap::ArgAction::SetTrue)]
    // pub min: bool,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
    // Alias `aboot` for possible Canadian users as they will not understand
    // `about` -- similarly I often alias `math` to `maths` for british
    // colleagues who would otherwise have no idea what I'm talking about
    /// Echo info about utiles
    #[command(name = "about", visible_alias = "aboot")]
    About(AboutArgs),

    /// list all commands
    #[command(name = "commands", visible_alias = "cmds")]
    Commands(CommandsArgs),

    #[command(subcommand, visible_alias = "db")]
    Sqlite(SqliteCommands),

    /// Echo the `tile.json` for mbtiles file
    #[command(name = "tilejson", visible_alias = "tj", alias = "trader-joes")]
    Tilejson(TilejsonArgs),

    /// Create new mbtiles db w/ schema
    #[command(name = "touch")]
    Touch(TouchArgs),

    /// Copy tiles from src -> dst
    #[command(name = "copy", visible_alias = "cp")]
    Copy(CopyArgs),

    /// Lint mbtiles file(s) (wip)
    #[command(name = "lint")]
    Lint(LintArgs),

    /// Aggregate tile hashes for tiles-db
    #[command(name = "agg-hash")]
    AggHash(AggHashArgs),

    /// Echo metadata (table) as json arr/obj
    #[command(name = "metadata", visible_aliases = ["meta", "md"])]
    Metadata(MetadataArgs),

    /// Set metadata key/value or from `json` file if key is fspath
    #[command(name = "metadata-set", visible_aliases = ["meta-set", "mds"])]
    MetadataSet(MetadataSetArgs),

    /// Update mbtiles db
    #[command(name = "update", visible_aliases = ["up"])]
    Update(UpdateArgs),

    /// Enumerate tiles db
    #[command(name = "enumerate", visible_aliases = ["enum"])]
    Enumerate(EnumerateArgs),

    /// rm-rf dirpath
    #[command(name = "rimraf", visible_alias = "rmrf", hide = true)]
    Rimraf(RimrafArgs),

    /// Echo mbtiles info/stats
    #[command(name = "info")]
    Info(InfoArgs),

    #[command(name = "vacuum", visible_alias = "vac")]
    Vacuum(VacuumArgs),

    /// Determine if mbtiles contains a latlong
    #[command(name = "dbcontains")]
    Contains {
        /// mbtiles filepath
        #[arg(required = true)]
        filepath: String,

        /// lat/long
        #[arg(required = true)]
        lnglat: LngLat,
    },

    /// zxyify/unzxyify tiles-db
    ///
    /// Adds/removes `z/x/y` table/view for querying tiles not inverted
    #[command(name = "zxyify", aliases = ["xyzify"])]
    Zxyify(ZxyifyArgs),

    /*
    ========================================================================
    TILE CLI UTILS - MERCANTILE LIKE CLI
    ========================================================================
    */
    /// Format json-tiles `[x, y, z]` tiles w/ format-string
    ///
    /// fmt-tokens:
    ///     `{json_arr}`/`{json}`  -> [x, y, z]
    ///     `{json_obj}`/`{obj}`   -> {x: x, y: y, z: z}
    ///     `{quadkey}`/`{qk}`     -> quadkey string
    ///     `{pmtileid}`/`{pmid}`  -> pmtile-id
    ///     `{x}`                  -> x tile coord
    ///     `{y}`                  -> y tile coord
    ///     `{z}`                  -> z/zoom level
    ///     `{-y}`/`{yup}`         -> y tile coord flipped/tms
    ///     `{zxy}`                -> z/x/y
    ///
    ///
    /// Example:
    ///     ```
    ///     \> echo "[486, 332, 10]" | utiles fmtstr
    ///     [486, 332, 10]
    ///     \> echo "[486, 332, 10]" | utiles fmtstr --fmt "{x},{y},{z}"
    ///     486,332,10
    ///     \> echo "[486, 332, 10]" | utiles fmt --fmt "SELECT * FROM tiles WHERE zoom_level = {z} AND tile_column = {x} AND tile_row = {y};"
    ///     SELECT * FROM tiles WHERE zoom_level = 10 AND tile_column = 486 AND tile_row = 332;
    ///     ```
    ///
    #[command(
        name = "fmtstr",
        aliases = & ["fmt", "xt"],
        verbatim_doc_comment,
    )]
    Fmt(TileFmtArgs),

    /// Echo the Web Mercator tile at ZOOM level bounding `GeoJSON` [west, south,
    /// east, north] bounding boxes, features, or collections read from stdin.
    ///
    /// Input may be a compact newline-delimited sequences of JSON or a
    /// pretty-printed ASCII RS-delimited sequence of JSON (like
    /// <https://tools.ietf.org/html/rfc8142> and
    /// <https://tools.ietf.org/html/rfc7159>).
    ///
    /// Examples:
    ///
    ///   \> echo "[-105.05, 39.95, -105, 40]" | utiles bounding-tile
    ///   [426, 775, 11]
    #[command(
        name = "bounding-tile",
        verbatim_doc_comment,
        about = "Echo bounding tile at zoom for bbox / geojson"
    )]
    BoundingTile(TileFmtArgs),

    /// Converts tiles to/from quadkey/[x, y, z]
    ///
    /// Input may be a compact newline-delimited sequences of JSON or a
    /// pretty-printed ASCII RS-delimited sequence of JSON (like
    /// <https://tools.ietf.org/html/rfc8142> and
    /// <https://tools.ietf.org/html/rfc7159>).
    ///
    /// Examples:
    ///
    ///   \> echo "[486, 332, 10]" | utiles quadkey
    ///   0313102310
    ///   \> echo "0313102310" | utiles quadkey
    ///   [486, 332, 10]
    ///   \> utiles quadkey 0313102310
    ///   [486, 332, 10]
    #[command(name = "quadkey", verbatim_doc_comment, visible_alias = "qk")]
    Quadkey(TileFmtArgs),

    /// Converts tile(s) to/from pmtile-id/[x, y, z]
    ///
    /// Input may be a compact newline-delimited sequences of JSON or a
    /// pretty-printed ASCII RS-delimited sequence of JSON (like
    /// <https://tools.ietf.org/html/rfc8142> and
    /// <https://tools.ietf.org/html/rfc7159>).
    ///
    /// Examples:
    ///
    ///   \> echo "[486, 332, 10]" | utiles pmtileid
    ///   506307
    ///   \> echo "506307" | utiles pmtileid
    ///   [486, 332, 10]
    ///   \> utiles pmtileid 506307
    ///   [486, 332, 10]
    #[command(name = "pmtileid", verbatim_doc_comment, visible_alias = "pmid")]
    Pmtileid(TileFmtArgs),

    /// Echos web-mercator tiles at zoom level intersecting given geojson-bbox [west, south,
    /// east, north], geojson-features, or geojson-collections read from stdin.
    ///
    /// Output format is a JSON `[x, y, z]` array by default; use --obj to output a
    /// JSON object `{x: x, y: y, z: z}`.
    ///
    /// bbox shorthands (case-insensitive):
    ///     "*"  | "world"     => [-180, -85.0511, 180, 85.0511]
    ///     "n"  | "north"     => [-180, 0, 180, 85.0511]
    ///     "s"  | "south"     => [-180, -85.0511, 180, 0]
    ///     "e"  | "east"      => [0, -85.0511, 180, 85.0511]
    ///     "w"  | "west"      => [-180, -85.0511, 0, 85.0511]
    ///     "ne" | "northeast" => [0, 0, 180, 85.0511]
    ///     "se" | "southeast" => [0, -85.0511, 180, 0]
    ///     "nw" | "northwest" => [-180, 0, 0, 85.0511]
    ///     "sw" | "southwest" => [-180, -85.0511, 0, 0]
    ///
    /// Input may be a compact newline-delimited sequences of JSON or a
    /// pretty-printed ASCII RS-delimited sequence of JSON (like
    /// <https://tools.ietf.org/html/rfc8142> and
    /// <https://tools.ietf.org/html/rfc7159>).
    ///
    /// Example:
    ///
    ///   \\> echo "[-105.05, 39.95, -105, 40]" | utiles tiles 12
    ///   [852, 1550, 12]
    ///   [852, 1551, 12]
    ///   [853, 1550, 12]
    ///   [853, 1551, 12]
    ///   \> utiles tiles 12 "[-105.05, 39.95, -105, 40]"
    ///   [852, 1550, 12]
    ///   [852, 1551, 12]
    ///   [853, 1550, 12]
    ///   [853, 1551, 12]
    #[command(
        name = "tiles",
        verbatim_doc_comment,
        about = "Echo tiles at zoom intersecting geojson bbox / feature / collection"
    )]
    Tiles(TilesArgs),

    /// Echo the neighbor tiles for input tiles
    ///
    /// Input may be a compact newline-delimited sequences of JSON or a
    /// pretty-printed ASCII RS-delimited sequence of JSON (like
    /// <https://tools.ietf.org/html/rfc8142> and
    /// <https://tools.ietf.org/html/rfc7159>).
    #[command(name = "neighbors")]
    Neighbors(TileFmtArgs),

    /// Echo children tiles of input tiles
    ///
    /// Input may be a compact newline-delimited sequences of JSON or a
    /// pretty-printed ASCII RS-delimited sequence of JSON (like
    /// <https://tools.ietf.org/html/rfc8142> and
    /// <https://tools.ietf.org/html/rfc7159>).
    ///
    /// Example:
    ///
    ///   \> echo "[486, 332, 10]" | utiles children
    ///   [972, 664, 11]
    #[command(name = "children", verbatim_doc_comment)]
    Children(ParentChildrenArgs),

    /// Echo parent tile of input tiles
    #[command(name = "parent")]
    Parent(ParentChildrenArgs),

    /// Echo tiles as `GeoJSON` feature collections/sequences
    ///
    /// Input may be a compact newline-delimited sequences of JSON or a
    /// pretty-printed ASCII RS-delimited sequence of JSON (like
    /// <https://tools.ietf.org/html/rfc8142> and
    /// <https://tools.ietf.org/html/rfc7159>).
    ///
    /// Example:
    ///
    ///   \> echo "[486, 332, 10]" | utiles shapes --precision 4 --bbox
    ///   [-9.1406, 53.1204, -8.7891, 53.3309]
    #[command(name = "shapes")]
    Shapes(ShapesArgs),

    /// Burn tiles from `GeoJSON` stream at zoom level (tile coverage)
    #[command(name = "burn", visible_alias = "cover")]
    Burn(BurnArgs),

    /// Merge tiles from stream removing parent tiles if children are present
    #[command(name = "merge")]
    Merge(MergeArgs),

    /// Echo edge tiles from stream of xyz tiles
    #[command(name = "edges")]
    Edges(EdgesArgs),

    /// Convert raster mbtiles to webp format
    #[command(
        name = "webpify",
        about = "Convert raster mbtiles to webp format",
        hide = true
    )]
    Webpify(WebpifyArgs),

    /// Convert raster mbtiles to webp format
    #[command(
        name = "optimize",
        about = "Optimize tiles-db",
        aliases = ["opt"],
        hide = true
    )]
    Optimize(OptimizeArgs),

    /// utiles server (wip)
    #[command(name = "serve", hide = true)]
    Serve(ServeArgs),

    /// Development/Playground command (hidden)
    #[command(name = "dev", hide = true)]
    Dev(DevArgs),

    // ========================================================================
    // UNIMPLEMENTED
    // ========================================================================
    /// UNIMPLEMENTED
    #[command(name = "addo", hide = true)]
    Addo,

    /// UNIMPLEMENTED
    #[command(name = "translate", hide = true)]
    Translate,
}

#[derive(Debug, Parser, Clone)]
#[command(name = "rimraf", about = "rm-rf dirpath")]
pub struct RimrafArgs {
    /// dirpath to nuke
    #[arg(required = true)]
    pub dirpath: String,

    /// collect and print file sizes
    #[arg(required = false, long, action = clap::ArgAction::SetTrue)]
    pub(crate) size: bool,

    /// dryrun (don't actually rm)
    #[arg(required = false, short = 'n', long, action = clap::ArgAction::SetTrue)]
    pub(crate) dryrun: bool,

    #[arg(required = false, short, long, action = clap::ArgAction::SetTrue)]
    pub(crate) verbose: bool,
}

#[derive(Args, Debug)]
#[group(required = false, multiple = false, id = "minmaxzoom")]
pub struct MinMaxZoom {
    /// min zoom level (0-30)
    #[arg(long)]
    minzoom: Option<u8>,

    /// max zoom level (0-30)
    #[arg(long)]
    maxzoom: Option<u8>,
}

#[derive(Debug, Parser)]
pub struct ZoomArgGroup {
    /// Zoom level (0-30)
    #[arg(short, long, required = false, value_delimiter = ',', value_parser = zoom::parse_zooms)]
    pub zoom: Option<Vec<Vec<u8>>>,

    /// min zoom level (0-30)
    #[arg(long, conflicts_with = "zoom", aliases = ["min-zoom", "min-z"])]
    pub minzoom: Option<u8>,

    /// max zoom level (0-30)
    #[arg(long, conflicts_with = "zoom", aliases = ["max-zoom", "max-z"])]
    pub maxzoom: Option<u8>,
}

impl ZoomArgGroup {
    #[must_use]
    pub fn zooms(&self) -> Option<Vec<u8>> {
        match &self.zoom {
            Some(zooms) => Some(zooms.iter().flatten().copied().collect()),
            None => match (self.minzoom, self.maxzoom) {
                (Some(minzoom), Some(maxzoom)) => Some((minzoom..=maxzoom).collect()),
                (Some(minzoom), None) => Some((minzoom..=31).collect()),
                (None, Some(maxzoom)) => Some((0..=maxzoom).collect()),
                (None, None) => None,
            },
        }
    }
}

#[derive(
    Debug, Copy, Parser, Clone, clap::ValueEnum, strum::EnumString, AsRefStr, Default,
)]
#[strum(serialize_all = "kebab-case")]
pub enum ConflictStrategy {
    #[default]
    Undefined,
    Ignore,
    Replace,
    Abort,
    Fail,
}

impl From<ConflictStrategy> for InsertStrategy {
    fn from(cs: ConflictStrategy) -> Self {
        match cs {
            ConflictStrategy::Undefined => Self::None,
            ConflictStrategy::Ignore => Self::Ignore,
            ConflictStrategy::Replace => Self::Replace,
            ConflictStrategy::Abort => Self::Abort,
            ConflictStrategy::Fail => Self::Fail,
        }
    }
}

#[derive(Debug, Parser)]
#[command(name = "copy", about = "Copy tiles from src -> dst")]
pub struct CopyArgs {
    /// source dataset fspath (mbtiles, dirpath)
    #[arg(required = true)]
    pub src: String,

    /// destination dataset fspath (mbtiles, dirpath)
    #[arg(required = true)]
    pub dst: String,

    /// dryrun (don't actually copy)
    #[arg(required = false, long, short = 'n', action = clap::ArgAction::SetTrue)]
    pub dryrun: bool,

    /// force overwrite dst
    #[arg(required = false, long, short, action = clap::ArgAction::SetTrue)]
    pub force: bool,

    #[command(flatten)]
    pub zoom: Option<ZoomArgGroup>,

    /// bbox (west, south, east, north)
    #[arg(required = false, long, value_parser = parse_bbox_ext, allow_hyphen_values = true)]
    pub bbox: Option<BBox>,

    /// conflict strategy when copying tiles
    #[arg(required = false, long, short, default_value = "undefined")]
    pub conflict: ConflictStrategy,

    /// db-type to copy to (flat, hash, norm) defaults to dbtype of src
    #[arg(
          required = false,
          long = "dst-type",
          aliases = ["dbtype", "dsttype", "mbtype", "mbt-type", "mbtiles-type"]
    )]
    pub dst_type: Option<DbtypeOption>,

    /// hash to use for blob-id if copying to normal/hash db type
    #[arg(required = false, long)]
    pub hash: Option<HashType>,

    /// n-jobs ~ 0=ncpus (default: max(4, ncpus))
    #[arg(required = false, long, short)]
    pub jobs: Option<u8>,

    /// sqlite fast writing mode (default: false) WIP to use streams
    #[arg(required = false, long, hide = true, action = clap::ArgAction::SetTrue)]
    pub stream: bool,
}

impl CopyArgs {
    #[must_use]
    pub fn zooms(&self) -> Option<Vec<u8>> {
        match &self.zoom {
            Some(zoom) => zoom.zooms(),
            None => None,
        }
    }

    #[must_use]
    pub fn zoom_set(&self) -> Option<ZoomSet> {
        self.zooms().map(|zooms| ZoomSet::from_zooms(&zooms))
    }

    #[must_use]
    pub fn bboxes(&self) -> Option<Vec<BBox>> {
        self.bbox.as_ref().map(|bbox| vec![*bbox])
    }

    #[must_use]
    pub fn bounds(&self) -> Option<String> {
        if let Some(bboxes) = self.bboxes() {
            let new_bbox = geobbox_merge(&bboxes);
            Some(new_bbox.mbt_bounds())
        } else {
            None
        }
    }
}

impl From<&CopyArgs> for CopyConfig {
    fn from(args: &CopyArgs) -> Self {
        let dbtype = args.dst_type.as_ref().map(|dbtype| dbtype.into());
        Self {
            src: PathBuf::from(&args.src),
            dst: PathBuf::from(&args.dst),
            zset: args.zoom_set(),
            zooms: args.zooms(),
            verbose: true,
            bboxes: args.bboxes(),
            bounds_string: args.bounds(),
            force: false,
            dryrun: false,
            jobs: args.jobs,
            istrat: InsertStrategy::from(args.conflict),
            hash: args.hash,
            dst_type: dbtype,
            stream: args.stream,
        }
    }
}