1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
//! Abstractions and functions for working with OpenStreetMap (etc.) tiles
//!
//! # Examples
//! ```
//! use slippy_map_tiles::Tile;
//!
//! let t = Tile::new(6, 35, 23).unwrap();
//!
//! ```
//!
//! You cannot create invalid tiles
//! ```
//! # use slippy_map_tiles::Tile;
//! assert!(Tile::new(0, 3, 3).is_none());
//! ```
#[macro_use]
extern crate lazy_static;
extern crate regex;

#[cfg(feature = "world_file")]
extern crate world_image_file;

use regex::Regex;
use std::borrow::Borrow;
use std::convert::TryFrom;
use std::fs::File;
use std::io::{BufRead, BufReader, Seek, SeekFrom};
use std::ops::Deref;
use std::str::FromStr;

#[cfg(feature = "world_file")]
use world_image_file::WorldFile;

#[cfg(test)]
mod tests;

/// A single tile.
#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash)]
pub struct Tile {
    zoom: u8,
    x: u32,
    y: u32,
}

impl Tile {
    /// Constucts a Tile with the following zoom, x and y values.
    ///
    /// Returns None if the x/y are invalid for that zoom level, or if the zoom is >= 100.
    /// # Examples
    /// ```
    /// # use slippy_map_tiles::Tile;
    /// assert!(Tile::new(0, 3, 3).is_none());
    /// ```
    pub fn new(zoom: u8, x: u32, y: u32) -> Option<Tile> {
        if zoom >= 100 {
            None
        } else if x < 2u32.pow(zoom as u32) && y < 2u32.pow(zoom as u32) {
            Some(Tile {
                zoom: zoom,
                x: x,
                y: y,
            })
        } else {
            None
        }
    }

    /// zoom of this tile
    pub fn zoom(&self) -> u8 {
        self.zoom
    }

    /// X value of this tile
    pub fn x(&self) -> u32 {
        self.x
    }

    /// Y value of tile
    pub fn y(&self) -> u32 {
        self.y
    }

    /// Constucts a Tile with the following zoom, x and y values based on a TMS URL.
    /// Returns None if the TMS url is invalid, or those
    ///
    /// # Examples
    /// ```
    /// # use slippy_map_tiles::Tile;
    /// let t = Tile::from_tms("/10/547/380.png");
    /// assert_eq!(t, Tile::new(10, 547, 380));
    /// assert_eq!(Tile::from_tms("foobar"), None);
    /// ```
    pub fn from_tms(tms: &str) -> Option<Tile> {
        lazy_static! {
            static ref RE: Regex = Regex::new(
                "/?(?P<zoom>[0-9]?[0-9])/(?P<x>[0-9]{1,10})/(?P<y>[0-9]{1,10})(\\.[a-zA-Z]{3,4})?$"
            )
            .unwrap();
        }

        let caps = RE.captures(tms)?;

        let zoom = caps.name("zoom");
        let x = caps.name("x");
        let y = caps.name("y");
        if zoom.is_none() || x.is_none() || y.is_none() {
            return None;
        }
        let zoom = zoom.unwrap().as_str().parse();
        let x = x.unwrap().as_str().parse();
        let y = y.unwrap().as_str().parse();

        if zoom.is_err() || x.is_err() || y.is_err() {
            return None;
        }
        let zoom: u8 = zoom.unwrap();
        let x: u32 = x.unwrap();
        let y: u32 = y.unwrap();

        Tile::new(zoom, x, y)
    }

    // TODO Add from_tc to parse the directory hiearchy so we can turn a filename in to a tile.
    // TODO Add from_ts to parse the directory hiearchy so we can turn a filename in to a tile.

    /// Returns the parent tile for this tile, i.e. the tile at the `zoom-1` that this tile is
    /// inside.
    ///
    /// ```
    /// # use slippy_map_tiles::Tile;
    /// assert_eq!(Tile::new(1, 0, 0).unwrap().parent(), Tile::new(0, 0, 0));
    /// ```
    /// None if there is no parent, which is at zoom 0.
    ///
    /// ```
    /// # use slippy_map_tiles::Tile;
    /// assert_eq!(Tile::new(0, 0, 0).unwrap().parent(), None);
    /// ```
    pub fn parent(&self) -> Option<Tile> {
        match self.zoom {
            0 => {
                // zoom 0, no parent
                None
            }
            _ => Tile::new(self.zoom - 1, self.x / 2, self.y / 2),
        }
    }

    /// Returns the subtiles (child) tiles for this tile. The 4 tiles at zoom+1 which cover this
    /// tile. Returns None if this is at the maximum permissable zoom level, and hence there are no
    /// subtiles.
    ///
    /// ```
    /// # use slippy_map_tiles::Tile;
    /// let t = Tile::new(0, 0, 0).unwrap();
    /// let subtiles: [Tile; 4] = t.subtiles().unwrap();
    /// assert_eq!(subtiles[0], Tile::new(1, 0, 0).unwrap());
    /// assert_eq!(subtiles[1], Tile::new(1, 1, 0).unwrap());
    /// assert_eq!(subtiles[2], Tile::new(1, 0, 1).unwrap());
    /// assert_eq!(subtiles[3], Tile::new(1, 1, 1).unwrap());
    /// ```
    pub fn subtiles(&self) -> Option<[Tile; 4]> {
        match self.zoom {
            std::u8::MAX => None,
            _ => {
                let z = self.zoom + 1;
                let x = 2 * self.x;
                let y = 2 * self.y;
                Some([
                    Tile {
                        zoom: z,
                        x: x,
                        y: y,
                    },
                    Tile {
                        zoom: z,
                        x: x + 1,
                        y: y,
                    },
                    Tile {
                        zoom: z,
                        x: x,
                        y: y + 1,
                    },
                    Tile {
                        zoom: z,
                        x: x + 1,
                        y: y + 1,
                    },
                ])
            }
        }
    }

    /// Iterate on all child tiles of this tile
    pub fn all_subtiles_iter(&self) -> AllSubTilesIterator {
        AllSubTilesIterator::new_from_tile(&self)
    }

    /// Returns the LatLon for the centre of this tile.
    pub fn centre_point(&self) -> LatLon {
        tile_nw_lat_lon(self.zoom, (self.x as f32) + 0.5, (self.y as f32) + 0.5)
    }

    /// Returns the LatLon for the centre of this tile.
    pub fn center_point(&self) -> LatLon {
        self.centre_point()
    }

    /// Returns the LatLon of the top left, i.e. north west corner, of this tile.
    pub fn nw_corner(&self) -> LatLon {
        tile_nw_lat_lon(self.zoom, self.x as f32, self.y as f32)
    }

    /// Returns the LatLon of the top right, i.e. north east corner, of this tile.
    pub fn ne_corner(&self) -> LatLon {
        tile_nw_lat_lon(self.zoom, (self.x as f32) + 1.0, self.y as f32)
    }

    /// Returns the LatLon of the bottom left, i.e. south west corner, of this tile.
    pub fn sw_corner(&self) -> LatLon {
        tile_nw_lat_lon(self.zoom, self.x as f32, (self.y as f32) + 1.0)
    }

    /// Returns the LatLon of the bottom right, i.e. south east corner, of this tile.
    pub fn se_corner(&self) -> LatLon {
        tile_nw_lat_lon(self.zoom, (self.x as f32) + 1.0, (self.y as f32) + 1.0)
    }

    pub fn top(&self) -> f32 {
        self.nw_corner().lat
    }
    pub fn bottom(&self) -> f32 {
        self.sw_corner().lat
    }
    pub fn left(&self) -> f32 {
        self.nw_corner().lon
    }
    pub fn right(&self) -> f32 {
        self.se_corner().lon
    }

    /// Returns the TC (TileCache) path for storing this tile.
    pub fn tc_path<T: std::fmt::Display>(&self, ext: T) -> String {
        let tc = xy_to_tc(self.x, self.y);
        format!(
            "{}/{}/{}/{}/{}/{}/{}.{}",
            self.zoom, tc[0], tc[1], tc[2], tc[3], tc[4], tc[5], ext
        )
    }

    /// Returns the MP (MapProxy) path for storing this tile.
    pub fn mp_path<T: std::fmt::Display>(&self, ext: T) -> String {
        let mp = xy_to_mp(self.x, self.y);
        format!(
            "{}/{}/{}/{}/{}.{}",
            self.zoom, mp[0], mp[1], mp[2], mp[3], ext
        )
    }

    /// Returns the TS (TileStash safe) path for storing this tile.
    pub fn ts_path<T: std::fmt::Display>(&self, ext: T) -> String {
        let ts = xy_to_ts(self.x, self.y);
        format!(
            "{}/{}/{}/{}/{}.{}",
            self.zoom, ts[0], ts[1], ts[2], ts[3], ext
        )
    }

    /// Returns the Z/X/Y representation of this tile
    pub fn zxy(&self) -> String {
        format!("{}/{}/{}", self.zoom, self.x, self.y)
    }

    /// Returns the ZXY path for storing this tile.
    pub fn zxy_path<T: std::fmt::Display>(&self, ext: T) -> String {
        format!("{}/{}/{}.{}", self.zoom, self.x, self.y, ext)
    }

    /// Returns the ModTileMetatile path for storing this tile
    pub fn mt_path<T: std::fmt::Display>(&self, ext: T) -> String {
        let tc = xy_to_mt(self.x, self.y);
        format!(
            "{}/{}/{}/{}/{}/{}.{}",
            self.zoom, tc[0], tc[1], tc[2], tc[3], tc[4], ext
        )
    }

    /// Returns an iterator that yields all the tiles possible, starting from `0/0/0`. Tiles are
    /// generated in a breath first manner, with all zoom 1 tiles before zoom 2 etc.
    ///
    /// ```
    /// # use slippy_map_tiles::Tile;
    /// let mut all_tiles_iter = Tile::all();
    /// ```
    pub fn all() -> AllTilesIterator {
        AllTilesIterator {
            next_zoom: 0,
            next_zorder: 0,
        }
    }

    /// Returns an iterator that yields all the tiles from zoom 0 down to, and including, all the
    /// tiles at `max_zoom` zoom level.  Tiles are
    /// generated in a breath first manner, with all zoom 1 tiles before zoom 2 etc.
    pub fn all_to_zoom(max_zoom: u8) -> AllTilesToZoomIterator {
        AllTilesToZoomIterator {
            max_zoom: max_zoom,
            next_zoom: 0,
            next_x: 0,
            next_y: 0,
        }
    }

    /// The BBox for this tile.
    pub fn bbox(&self) -> BBox {
        let nw = self.nw_corner();
        let se = self.se_corner();

        BBox::new_from_points(&nw, &se)
    }

    pub fn metatile(&self, scale: u8) -> Option<Metatile> {
        Metatile::new(scale, self.zoom(), self.x(), self.y())
    }

    pub fn modtile_metatile(&self) -> Option<ModTileMetatile> {
        ModTileMetatile::new(self.zoom(), self.x(), self.y())
    }

    #[cfg(feature = "world_file")]
    /// Return the World File (in EPSG:3857 / Web Mercator SRID) for this tile
    pub fn world_file(&self) -> WorldFile {
        let total_merc_width = 20037508.342789244;
        let tile_merc_width = (2. * total_merc_width) / 2f64.powi(self.zoom as i32);
        let scale = tile_merc_width / 256.;

        WorldFile {
            x_scale: scale,
            y_scale: -scale,

            x_skew: 0.,
            y_skew: 0.,

            x_coord: tile_merc_width * (self.x as f64) - total_merc_width,
            y_coord: -tile_merc_width * (self.y as f64) + total_merc_width,
        }
    }
}

impl FromStr for Tile {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        lazy_static! {
            static ref TILE_RE: Regex =
                Regex::new("^(?P<zoom>[0-9]?[0-9])/(?P<x>[0-9]{1,10})/(?P<y>[0-9]{1,10})$")
                    .unwrap();
        }

        let caps = TILE_RE.captures(s);

        if caps.is_none() {
            return Err("Tile Z/X/Y regex didn't match");
        }
        let caps = caps.unwrap();

        // If the regex matches, then none of these should fail, right?
        let zoom = caps.name("zoom").unwrap().as_str().parse().unwrap();
        let x = caps.name("x").unwrap().as_str().parse().unwrap();
        let y = caps.name("y").unwrap().as_str().parse().unwrap();

        match Tile::new(zoom, x, y) {
            None => {
                // Invalid x or y for the zoom
                Err("Invalid X or Y for this zoom")
            }
            Some(t) => Ok(t),
        }
    }
}

/// Iterates over all the tiles in the world.
pub struct AllTilesIterator {
    next_zoom: u8,
    next_zorder: u64,
}

impl Iterator for AllTilesIterator {
    type Item = Tile;

    fn next(&mut self) -> Option<Tile> {
        let zoom = self.next_zoom;
        let (x, y) = zorder_to_xy(self.next_zorder);
        let tile = Tile::new(zoom, x, y);

        let max_tile_no = 2u32.pow(zoom as u32) - 1;
        if x == max_tile_no && y == max_tile_no {
            // we're at the end
            self.next_zoom = zoom + 1;
            self.next_zorder = 0;
        } else {
            self.next_zorder += 1;
        }

        tile
    }
}

/// Iterates over all the tiles from 0/0/0 up to, and including, `max_zoom`.
pub struct AllTilesToZoomIterator {
    max_zoom: u8,
    next_zoom: u8,
    next_x: u32,
    next_y: u32,
}

fn remaining_in_this_zoom(next_zoom: u8, next_x: u32, next_y: u32) -> Option<usize> {
    if next_zoom == 0 && next_x == 0 && next_y == 0 {
        return Some(1);
    }

    let max_tile_no = 2u32.pow(next_zoom as u32);
    let remaining_in_column = max_tile_no - next_y;
    let remaining_in_column = remaining_in_column as usize;
    let remaining_rows = max_tile_no - next_x - 1;
    let remaining_rows = remaining_rows as usize;

    let remaining_after_this_column = remaining_rows.checked_mul(max_tile_no as usize)?;

    remaining_in_column.checked_add(remaining_after_this_column)
}

impl Iterator for AllTilesToZoomIterator {
    type Item = Tile;

    fn next(&mut self) -> Option<Tile> {
        if self.next_zoom > self.max_zoom {
            return None;
        }
        let tile = Tile::new(self.next_zoom, self.next_x, self.next_y);
        let max_tile_no = 2u32.pow(self.next_zoom as u32) - 1;
        if self.next_y < max_tile_no {
            self.next_y += 1;
        } else if self.next_x < max_tile_no {
            self.next_x += 1;
            self.next_y = 0;
        } else if self.next_zoom < std::u8::MAX {
            self.next_zoom += 1;
            self.next_x = 0;
            self.next_y = 0;
        }

        tile
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.next_zoom > self.max_zoom {
            return (0, Some(0));
        }

        let remaining_in_this_level =
            remaining_in_this_zoom(self.next_zoom, self.next_x, self.next_y);
        if remaining_in_this_level.is_none() {
            return (std::usize::MAX, None);
        }
        let remaining_in_this_level = remaining_in_this_level.unwrap();

        let mut total: usize = remaining_in_this_level as usize;
        for i in (self.next_zoom + 1)..(self.max_zoom + 1) {
            let tiles_this_zoom = num_tiles_in_zoom(i);
            if tiles_this_zoom.is_none() {
                return (std::usize::MAX, None);
            }

            let tiles_this_zoom = tiles_this_zoom.unwrap();

            let new_total = total.checked_add(tiles_this_zoom);
            if new_total.is_none() {
                return (std::usize::MAX, None);
            }
            total = new_total.unwrap();
        }

        // If we've got to here, we know how big it is
        (total, Some(total))
    }
}

pub struct AllSubTilesIterator {
    _tiles: Vec<Tile>,
}

impl AllSubTilesIterator {
    pub fn new_from_tile(base_tile: &Tile) -> Self {
        let new_tiles = match base_tile.subtiles() {
            None => Vec::new(),
            Some(t) => vec![t[0], t[1], t[2], t[3]],
        };
        AllSubTilesIterator { _tiles: new_tiles }
    }
}

impl Iterator for AllSubTilesIterator {
    type Item = Tile;

    fn next(&mut self) -> Option<Tile> {
        if self._tiles.is_empty() {
            return None;
        }
        let next = self._tiles.remove(0);
        if let Some(subtiles) = next.subtiles() {
            self._tiles.extend_from_slice(&subtiles);
        }
        Some(next)
    }
}

/// Metatiles are NxN tiles
#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash)]
pub struct Metatile {
    scale: u8,
    zoom: u8,
    x: u32,
    y: u32,
}

impl Metatile {
    pub fn new(scale: u8, zoom: u8, x: u32, y: u32) -> Option<Self> {
        if !scale.is_power_of_two() {
            return None;
        }
        if zoom >= 100 {
            None
        } else if x < 2u32.pow(zoom as u32) && y < 2u32.pow(zoom as u32) {
            let s = scale as u32;
            let x = (x / s) * s;
            let y = (y / s) * s;
            Some(Metatile {
                scale: scale,
                zoom: zoom,
                x: x,
                y: y,
            })
        } else {
            None
        }
    }

    pub fn scale(&self) -> u8 {
        self.scale
    }

    pub fn zoom(&self) -> u8 {
        self.zoom
    }

    /// What is the width or height of this metatile. For small zoom numbers (e.g. z1), there will
    /// not be the full `scale` tiles across.
    pub fn size(&self) -> u8 {
        let num_tiles_in_zoom = 2u32.pow(self.zoom as u32);
        if num_tiles_in_zoom < (self.scale as u32) {
            num_tiles_in_zoom as u8
        } else {
            self.scale
        }
    }

    /// Returns the LatLon for the centre of this metatile.
    pub fn centre_point(&self) -> LatLon {
        tile_nw_lat_lon(
            self.zoom,
            (self.x as f32) + (self.size() as f32) / 2.,
            (self.y as f32) + (self.size() as f32) / 2.,
        )
    }

    /// Returns the LatLon for the centre of this metatile.
    pub fn center_point(&self) -> LatLon {
        self.centre_point()
    }

    /// Returns the LatLon of the top left, i.e. north west corner, of this metatile.
    pub fn nw_corner(&self) -> LatLon {
        tile_nw_lat_lon(self.zoom, self.x as f32, self.y as f32)
    }

    /// Returns the LatLon of the top right, i.e. north east corner, of this metatile.
    pub fn ne_corner(&self) -> LatLon {
        tile_nw_lat_lon(
            self.zoom,
            (self.x + self.size() as u32) as f32,
            self.y as f32,
        )
    }

    /// Returns the LatLon of the bottom left, i.e. south west corner, of this metatile.
    pub fn sw_corner(&self) -> LatLon {
        tile_nw_lat_lon(
            self.zoom,
            self.x as f32,
            (self.y + self.size() as u32) as f32,
        )
    }

    /// Returns the LatLon of the bottom right, i.e. south east corner, of this metatile.
    pub fn se_corner(&self) -> LatLon {
        tile_nw_lat_lon(
            self.zoom,
            (self.x + self.size() as u32) as f32,
            (self.y + self.size() as u32) as f32,
        )
    }

    /// X value of this metatile
    pub fn x(&self) -> u32 {
        self.x
    }

    /// Y value of metatile
    pub fn y(&self) -> u32 {
        self.y
    }

    pub fn tiles(&self) -> Vec<Tile> {
        let size = self.size() as u32;
        (0..(size * size))
            .map(|n| {
                // oh for a divmod
                let (i, j) = (n / size, n % size);
                // being cheeky and skipping the usuall Tile::new checks here, since we know it's valid
                Tile {
                    zoom: self.zoom,
                    x: self.x + i,
                    y: self.y + j,
                }
            })
            .collect()
    }

    pub fn all(scale: u8) -> MetatilesIterator {
        assert!(scale.is_power_of_two());
        MetatilesIterator::all(scale)
    }
}

impl FromStr for Metatile {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        lazy_static! {
            static ref METATILE_RE: Regex = Regex::new(
                "^(?P<scale>[0-9]+) (?P<zoom>[0-9]?[0-9])/(?P<x>[0-9]{1,10})/(?P<y>[0-9]{1,10})$"
            )
            .unwrap();
        }

        let caps = METATILE_RE.captures(s);

        if caps.is_none() {
            return Err(());
        }
        let caps = caps.unwrap();

        // If the regex matches, then none of these should fail, right?
        let scale = caps.name("scale").unwrap().as_str().parse().unwrap();
        let zoom = caps.name("zoom").unwrap().as_str().parse().unwrap();
        let x = caps.name("x").unwrap().as_str().parse().unwrap();
        let y = caps.name("y").unwrap().as_str().parse().unwrap();

        match Metatile::new(scale, zoom, x, y) {
            None => {
                // Invalid x or y for the zoom
                Err(())
            }
            Some(mt) => Ok(mt),
        }
    }
}

/// Iterates over all the metatiles in the world.
#[derive(Debug)]
pub struct MetatilesIterator {
    scale: u8,
    curr_zoom: u8,
    maxzoom: u8,
    curr_zorder: u64,
    bbox: Option<BBox>,

    // In metatile coords, i.e. x/scale
    curr_zoom_width_height: Option<(u32, u32)>,
    curr_zoom_start_xy: Option<(u32, u32)>,

    // If we're reading from a file
    total: Option<usize>,
    tile_list_file: Option<BufReader<File>>,
}

impl MetatilesIterator {
    pub fn all(scale: u8) -> Self {
        MetatilesIterator {
            scale: scale,
            curr_zoom: 0,
            curr_zorder: 0,
            bbox: None,
            maxzoom: 32,
            curr_zoom_width_height: None,
            curr_zoom_start_xy: None,
            total: None,
            tile_list_file: None,
        }
    }

    pub fn new_for_bbox(scale: u8, bbox: &BBox) -> Self {
        MetatilesIterator::new_for_bbox_zoom(scale, &Some(bbox.clone()), 0, 32)
    }

    /// `None` for bbox means 'whole world'
    pub fn new_for_bbox_zoom(scale: u8, bbox: &Option<BBox>, minzoom: u8, maxzoom: u8) -> Self {
        let mut it = MetatilesIterator {
            scale: scale,
            curr_zoom: minzoom,
            curr_zorder: 0,
            bbox: bbox.clone(),
            maxzoom: maxzoom,
            curr_zoom_width_height: None,
            curr_zoom_start_xy: None,
            total: None,
            tile_list_file: None,
        };
        it.set_zoom_width_height();
        it.set_zoom_start_xy();

        it
    }

    pub fn new_from_filelist(filename: String) -> Self {
        let mut file = BufReader::new(File::open(&filename).unwrap());
        file.seek(SeekFrom::Start(0)).unwrap();

        // we're intentionally ignore usize overflow. If you have that many lines in a file,
        // you're probably doing something wrong.
        let total = file.lines().count();

        let file = BufReader::new(File::open(filename).unwrap());

        MetatilesIterator {
            scale: 0,
            curr_zoom: 0,
            curr_zorder: 0,
            bbox: None,
            maxzoom: 0,
            curr_zoom_width_height: None,
            curr_zoom_start_xy: None,
            total: Some(total),
            tile_list_file: Some(file),
        }
    }

    /// Update the `self.curr_zoom_width_height` variable with the correct value for this zoom
    /// (`self.curr_zoom`)
    fn set_zoom_width_height(&mut self) {
        if let Some(ref bbox) = self.bbox {
            let scale = self.scale as u32;
            let zoom = self.curr_zoom;
            // TODO is this x/y lat/lon the right way around?
            let (x1, y1) = lat_lon_to_tile(bbox.top, bbox.left, zoom);
            let (x1, y1) = (x1 / scale, y1 / scale);
            let (x2, y2) = lat_lon_to_tile(bbox.bottom, bbox.right, zoom);
            let (x2, y2) = (x2 / scale, y2 / scale);

            let width = x2 - x1 + 1;
            let height = y2 - y1 + 1;

            self.curr_zoom_width_height = Some((width, height));
        }
    }

    fn set_zoom_start_xy(&mut self) {
        if self.bbox.is_none() {
            return;
        }

        let top = match self.bbox {
            None => 90.,
            Some(ref b) => b.top,
        };
        let left = match self.bbox {
            None => -180.,
            Some(ref b) => b.left,
        };
        // TODO is this x/y lat/lon the right way around?
        let (x1, y1) = lat_lon_to_tile(top, left, self.curr_zoom);
        self.curr_zoom_start_xy = Some((x1 / self.scale as u32, y1 / self.scale as u32));
    }

    fn next_from_zorder(&mut self) -> Option<Metatile> {
        // have to set a value, but we're never going to read it
        #[allow(unused_assignments)]
        let mut zoom = 0;
        #[allow(unused_assignments)]
        let mut x = 0;
        #[allow(unused_assignments)]
        let mut y = 0;

        let scale = self.scale as u32;

        loop {
            if self.curr_zoom > self.maxzoom {
                // We're finished
                return None;
            }

            zoom = self.curr_zoom;
            let (width, height) = match self.curr_zoom_width_height {
                None => {
                    let max_num = 2u32.pow(zoom as u32);
                    let mut max = max_num / scale;
                    if max_num % scale > 0 {
                        max += 1
                    }
                    (max, max)
                }
                Some((width, height)) => (width, height),
            };

            let max_zorder_for_zoom = xy_to_zorder(width - 1, height - 1);

            let (i, j) = zorder_to_xy(self.curr_zorder);
            let bits = match self.curr_zoom_start_xy {
                None => (i, j),
                Some(start) => (start.0 + i, start.1 + j),
            };
            x = bits.0;
            y = bits.1;

            if self.curr_zorder > max_zorder_for_zoom {
                // Next zoom
                // we're at the end
                self.curr_zoom = zoom + 1;
                self.curr_zorder = 0;
                self.set_zoom_start_xy();
                self.set_zoom_width_height();
            } else if i > width || j > height {
                // If the bbox is non-square, there will be X (or Y) tiles which are outside
                // the bbox. Rather than go to the next zoom level, we want to contine to look at
                // the next tile in order, and keep going until we get a tile that's inside the
                // bbox.  to the next tile
                self.curr_zorder += 1;
                continue;
            } else {
                // This z order is OK
                self.curr_zorder += 1;
                break;
            }
        }

        let (x, y) = (x * scale, y * scale);
        Metatile::new(self.scale, zoom, x, y)
    }

    fn next_from_file(&mut self) -> Option<Metatile> {
        let mut s = String::new();
        if let Some(ref mut file) = self.tile_list_file {
            file.read_line(&mut s).unwrap();
        }
        // remove trailing newline
        let s = s.trim_end();

        s.parse().ok()
    }

    pub fn total(&self) -> Option<usize> {
        self.total
    }
}

impl Iterator for MetatilesIterator {
    type Item = Metatile;

    fn next(&mut self) -> Option<Self::Item> {
        if self.tile_list_file.is_some() {
            self.next_from_file()
        } else {
            self.next_from_zorder()
        }
    }
}

/// Metatiles as found by mod_tile, always 8x8
#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash)]
pub struct ModTileMetatile {
    inner: Metatile,
}

impl ModTileMetatile {
    pub fn new(zoom: u8, x: u32, y: u32) -> Option<Self> {
        match Metatile::new(8, zoom, x, y) {
            None => None,
            Some(inner) => Some(ModTileMetatile { inner: inner }),
        }
    }

    /// Returns the mod_tile path for storing this metatile
    pub fn path<T: std::fmt::Display>(&self, ext: T) -> String {
        let mt = xy_to_mt(self.inner.x, self.inner.y);
        format!(
            "{}/{}/{}/{}/{}/{}.{}",
            self.inner.zoom, mt[0], mt[1], mt[2], mt[3], mt[4], ext
        )
    }

    /// X value of this metatile
    pub fn x(&self) -> u32 {
        self.inner.x
    }

    /// Y value of metatile
    pub fn y(&self) -> u32 {
        self.inner.y
    }

    /// Zoom value of metatile
    pub fn zoom(&self) -> u8 {
        self.inner.zoom
    }

    /// What is the width or height of this metatile. For small zoom numbers (e.g. z1), there will
    /// not be the full `scale` tiles across.
    pub fn size(self) -> u8 {
        self.inner.size()
    }
}

impl From<ModTileMetatile> for Metatile {
    fn from(mt: ModTileMetatile) -> Self {
        mt.inner
    }
}

impl TryFrom<Metatile> for ModTileMetatile {
    type Error = &'static str;

    fn try_from(mt: Metatile) -> Result<Self, Self::Error> {
        if mt.scale == 8 {
            Ok(ModTileMetatile { inner: mt })
        } else {
            Err("Can only convert scale 8 metatiles into ModTileMetatile")
        }
    }
}

impl Borrow<Metatile> for ModTileMetatile {
    fn borrow(&self) -> &Metatile {
        &self.inner
    }
}

impl Deref for ModTileMetatile {
    type Target = Metatile;

    fn deref(&self) -> &Metatile {
        &self.inner
    }
}

fn tile_nw_lat_lon(zoom: u8, x: f32, y: f32) -> LatLon {
    let n: f32 = 2f32.powi(zoom as i32);
    let lon_deg: f32 = (x as f32) / n * 360f32 - 180f32;
    let lat_rad: f32 = ((1f32 - 2f32 * (y as f32) / n) * std::f32::consts::PI)
        .sinh()
        .atan();
    let lat_deg: f32 = lat_rad * 180f32 * std::f32::consts::FRAC_1_PI;

    // FIXME figure out the unwrapping here....
    // Do we always know it's valid?
    LatLon::new(lat_deg, lon_deg).unwrap()
}

/// Return the x,y of a tile which has this lat/lon for this zoom level
pub fn lat_lon_to_tile(lat: f32, lon: f32, zoom: u8) -> (u32, u32) {
    // TODO do this at compile time?
    #[allow(non_snake_case)]
    let MAX_LAT: f64 = std::f64::consts::PI.sinh().atan();

    let lat: f64 = lat as f64;
    let lat = lat.to_radians();

    let lon: f64 = lon as f64;

    // Clip the latitude to the max & min (~85.0511)
    let lat = if lat > MAX_LAT {
        MAX_LAT
    } else if lat < -MAX_LAT {
        -MAX_LAT
    } else {
        lat
    };

    let n: f64 = 2f64.powi(zoom as i32);
    let xtile: u32 = (n * ((lon + 180.) / 360.)).trunc() as u32;
    let ytile: u32 = (n * (1. - ((lat.tan() + (1. / lat.cos())).ln() / std::f64::consts::PI)) / 2.)
        .trunc() as u32;

    (xtile, ytile)
}

/// Return the x,y of a tile which (for this zoom) has this web mercator 3857 x/y, and then the x,y
/// of the pixel within that image (presuming a 256x256 image)
pub fn merc_location_to_tile_coords(x: f64, y: f64, zoom: u8) -> ((u32, u32), (u32, u32)) {
    let num_tiles = 2u32.pow(zoom as u32) as f64;
    let global_extent = 20_037_508.342789244;
    let tile_width = (2. * global_extent) / num_tiles;

    (
        // location within the tile
        (
            ((x + global_extent) / tile_width) as u32,
            ((y + global_extent) / tile_width) as u32,
        ),
        // Tile x/y
        (
            (((x + global_extent) % tile_width) / tile_width * 256.) as u32,
            (num_tiles - ((y + global_extent) % tile_width) / tile_width * 256. - 1.) as u32,
        ),
    )
}

/// How many tiles does this bbox cover at this zoom
/// If there is an overflow for usize, `None` is returned, if not, a `Some(...)`
pub fn size_bbox_zoom(bbox: &BBox, zoom: u8) -> Option<usize> {
    let top_left_tile = lat_lon_to_tile(bbox.top(), bbox.left(), zoom);
    let bottom_right_tile = lat_lon_to_tile(bbox.bottom(), bbox.right(), zoom);
    let height = (bottom_right_tile.0 - top_left_tile.0) as usize + 1;
    let width = (bottom_right_tile.1 - top_left_tile.1) as usize + 1;

    height.checked_mul(width)
}

/// How many metatiles, of this scale, does this bbox cover at this zoom
/// If there is an overflow for usize, `None` is returned, if not, a `Some(...)`
/// This is less likely to overflow than `size_bbox_zoom` because metatiles are larger
pub fn size_bbox_zoom_metatiles(bbox: &BBox, zoom: u8, metatile_scale: u8) -> Option<usize> {
    let metatile_scale = metatile_scale as u32;
    let top_left_tile = lat_lon_to_tile(bbox.top(), bbox.left(), zoom);
    let bottom_right_tile = lat_lon_to_tile(bbox.bottom(), bbox.right(), zoom);
    let bottom = (bottom_right_tile.0 / metatile_scale) * metatile_scale;
    let top = (top_left_tile.0 / metatile_scale) * metatile_scale;
    let left = (top_left_tile.1 / metatile_scale) * metatile_scale;
    let right = (bottom_right_tile.1 / metatile_scale) * metatile_scale;

    let height = ((bottom - top) / metatile_scale as u32) as usize + 1;
    let width = ((right - left) / metatile_scale as u32) as usize + 1;

    height.checked_mul(width)
}

/// A single point in the world.
///
/// Since OSM uses up to 7 decimal places, this stores the lat/lon as `f32` which is enough
/// precision of that
#[derive(PartialEq, Debug, Clone)]
pub struct LatLon {
    lat: f32,
    lon: f32,
}

impl LatLon {
    /// Constructs a LatLon from a given `lat` and `lon`. Returns `None` if the lat or lon is
    /// invalid, e.g. a lat of 100.
    pub fn new(lat: f32, lon: f32) -> Option<LatLon> {
        if lat <= 90f32 && lat >= -90f32 && lon <= 180f32 && lon >= -180f32 {
            Some(LatLon { lat: lat, lon: lon })
        } else {
            None
        }
    }

    /// Latitude
    pub fn lat(&self) -> f32 {
        self.lat
    }
    /// Longitude
    pub fn lon(&self) -> f32 {
        self.lon
    }

    /// Convert to Web Mercator format (SRID 3857)
    pub fn to_3857(&self) -> (f32, f32) {
        let x = self.lon() * 20037508.34 / 180.;
        let pi = std::f32::consts::PI;
        let y = ((90. + self.lat()) * pi / 360.).tan().ln() / (pi / 180.);
        let y = y * 20037508.34 / 180.;

        (x, y)
    }

    /// What tile is this point at on this zoom level
    pub fn tile(&self, zoom: u8) -> Tile {
        let (x, y) = lat_lon_to_tile(self.lat, self.lon, zoom);
        Tile::new(zoom, x, y).unwrap()
    }
}

/// A Bounding box
#[derive(PartialEq, Debug, Clone)]
pub struct BBox {
    top: f32,
    left: f32,
    bottom: f32,
    right: f32,
}

impl BBox {
    /// Construct a new BBox from the given max and min latitude and longitude. Returns `None` if
    /// the lat or lon is invalid, e.g. a lon of 200
    pub fn new(top: f32, left: f32, bottom: f32, right: f32) -> Option<BBox> {
        //let top = if top > bottom { top } else { bottom };
        //let bottom = if top > bottom { bottom } else { top };
        //let left = if right > left { left } else { right };
        //let right = if right > left { right } else { left };

        if top <= 90.
            && top >= -90.
            && bottom <= 90.
            && bottom >= -90.
            && left <= 180.
            && left >= -180.
            && right <= 180.
            && right >= -180.
        {
            Some(BBox {
                top: top,
                left: left,
                bottom: bottom,
                right: right,
            })
        } else {
            None
        }
    }

    /// Given two points, return the bounding box specified by those 2 points
    pub fn new_from_points(topleft: &LatLon, bottomright: &LatLon) -> BBox {
        BBox {
            top: topleft.lat,
            left: topleft.lon,
            bottom: bottomright.lat,
            right: bottomright.lon,
        }
    }

    /// Construct a BBox from a tile
    pub fn new_from_tile(tile: &Tile) -> Self {
        tile.bbox()
    }

    /// Return true iff this point is in this bbox
    pub fn contains_point(&self, point: &LatLon) -> bool {
        point.lat <= self.top
            && point.lat > self.bottom
            && point.lon >= self.left
            && point.lon < self.right
    }

    /// Returns true iff this bbox and `other` share at least one point
    pub fn overlaps_bbox(&self, other: &BBox) -> bool {
        // FXME check top & left edges
        self.left < other.right
            && self.right > other.left
            && self.top > other.bottom
            && self.bottom < other.top
    }

    /// Iterate over all the tiles from z0 onwards that this bbox is in
    pub fn tiles(&self) -> BBoxTilesIterator {
        BBoxTilesIterator::new(&self)
    }

    /// Iterate over all the metatiles from z0 onwards that this bbox is in
    pub fn metatiles(&self, scale: u8) -> MetatilesIterator {
        let bbox: BBox = (*self).clone();
        MetatilesIterator {
            curr_zoom: 0,
            maxzoom: 32,
            bbox: Some(bbox),
            curr_zorder: 0,
            scale: scale,
            curr_zoom_width_height: None,
            curr_zoom_start_xy: None,
            total: None,
            tile_list_file: None,
        }
    }

    /// Return the top value of this bbox
    pub fn top(&self) -> f32 {
        self.top
    }

    /// Return the bottom value of this bbox
    pub fn bottom(&self) -> f32 {
        self.bottom
    }

    /// Return the left value of this bbox
    pub fn left(&self) -> f32 {
        self.left
    }

    /// Return the right value of this bbox
    pub fn right(&self) -> f32 {
        self.right
    }

    /// For this zoom level, return all the tiles that cover this bbox
    pub fn tiles_for_zoom(&self, zoom: u8) -> impl Iterator<Item = Tile> {
        let top_left_tile = lat_lon_to_tile(self.top, self.left, zoom);
        let bottom_right_tile = lat_lon_to_tile(self.bottom, self.right, zoom);

        (top_left_tile.0..=bottom_right_tile.0)
            .flat_map(move |x| {
                (top_left_tile.1..=bottom_right_tile.1)
                    .map(move |y| (x, y))
            })
            .map(move |(x, y)| Tile::new(zoom, x, y).unwrap())
    }

    /// Returns the LatLon for the centre of this bbox
    pub fn centre_point(&self) -> LatLon {
        LatLon::new((self.top + self.bottom) / 2., (self.left + self.right) / 2.).unwrap()
    }

    /// Returns the LatLon for the centre of this bbox
    pub fn center_point(&self) -> LatLon {
        self.centre_point()
    }

    /// Returns the LatLon of the top left, i.e. north west corner, of this bbot
    pub fn nw_corner(&self) -> LatLon {
        LatLon::new(self.top, self.left).unwrap()
    }

    /// Returns the LatLon of the top right, i.e. north east corner, of this bbox
    pub fn ne_corner(&self) -> LatLon {
        LatLon::new(self.top, self.right).unwrap()
    }

    /// Returns the LatLon of the bottom left, i.e. south west corner, of this bbox
    pub fn sw_corner(&self) -> LatLon {
        LatLon::new(self.bottom, self.left).unwrap()
    }

    /// Returns the LatLon of the bottom right, i.e. south east corner, of this bbox.
    pub fn se_corner(&self) -> LatLon {
        LatLon::new(self.bottom, self.right).unwrap()
    }
}

impl FromStr for BBox {
    type Err = &'static str;

    /// Given a string like "$MINLON $MINLAT $MAXLON $MAXLAT" parse that into a BBox. Returns None
    /// if there is no match.
    fn from_str(string: &str) -> Result<Self, Self::Err> {
        lazy_static! {
            //static ref num_regex: &'static str = r"-?[0-9]{1,3}(\.[0-9]{1,10})?";
            static ref SIMPLE_COPY_SPACE: Regex = Regex::new(r"^(?P<minlon>-?[0-9]{1,3}(\.[0-9]{1,10})?) (?P<minlat>-?[0-9]{1,3}(\.[0-9]{1,10})?) (?P<maxlon>-?[0-9]{1,3}(\.[0-9]{1,10})?) (?P<maxlat>-?[0-9]{1,3}(\.[0-9]{1,10})?)$").unwrap();
            static ref SIMPLE_COPY_COMMA: Regex = Regex::new(r"^(?P<minlon>-?[0-9]{1,3}(\.[0-9]{1,10})?),(?P<minlat>-?[0-9]{1,3}(\.[0-9]{1,10})?),(?P<maxlon>-?[0-9]{1,3}(\.[0-9]{1,10})?),(?P<maxlat>-?[0-9]{1,3}(\.[0-9]{1,10})?)$").unwrap();
        }
        let caps = SIMPLE_COPY_SPACE
            .captures(string)
            .or_else(|| SIMPLE_COPY_COMMA.captures(string));
        if caps.is_none() {
            return Err("regex not match");
        }
        let caps = caps.unwrap();

        let minlat = caps.name("minlat");
        let maxlat = caps.name("maxlat");
        let minlon = caps.name("minlon");
        let maxlon = caps.name("maxlon");

        if minlat.is_none() || maxlat.is_none() || minlon.is_none() || maxlon.is_none() {
            return Err("bad lat/lon");
        }

        let minlat = minlat.unwrap().as_str().parse();
        let maxlat = maxlat.unwrap().as_str().parse();
        let minlon = minlon.unwrap().as_str().parse();
        let maxlon = maxlon.unwrap().as_str().parse();

        if minlat.is_err() || maxlat.is_err() || minlon.is_err() || maxlon.is_err() {
            return Err("bad lat/lon");
        }

        let minlat = minlat.unwrap();
        let maxlat = maxlat.unwrap();
        let minlon = minlon.unwrap();
        let maxlon = maxlon.unwrap();

        BBox::new(maxlat, minlon, minlat, maxlon).ok_or("bad lat/lon")
    }
}

pub struct BBoxTilesIterator<'a> {
    bbox: &'a BBox,
    tiles: Vec<Tile>,
    tile_index: usize,
}

impl<'a> BBoxTilesIterator<'a> {
    pub fn new(bbox: &'a BBox) -> BBoxTilesIterator<'a> {
        // Everything is in 0/0/0, so start with that.
        BBoxTilesIterator {
            bbox: bbox,
            tiles: vec![Tile::new(0, 0, 0).unwrap()],
            tile_index: 0,
        }
    }
}

impl<'a> Iterator for BBoxTilesIterator<'a> {
    type Item = Tile;

    fn next(&mut self) -> Option<Tile> {
        if self.tile_index >= self.tiles.len() {
            // We've sent off all the existing tiles, so start looking at the children
            let mut new_tiles: Vec<Tile> = Vec::with_capacity(self.tiles.len() * 4);
            for t in self.tiles.iter() {
                match t.subtiles() {
                    None => {}
                    Some(sub) => {
                        if self.bbox.overlaps_bbox(&sub[0].bbox()) {
                            new_tiles.push(sub[0]);
                        }
                        if self.bbox.overlaps_bbox(&sub[1].bbox()) {
                            new_tiles.push(sub[1]);
                        }
                        if self.bbox.overlaps_bbox(&sub[2].bbox()) {
                            new_tiles.push(sub[2]);
                        }
                        if self.bbox.overlaps_bbox(&sub[3].bbox()) {
                            new_tiles.push(sub[3]);
                        }
                    }
                }
            }

            new_tiles.shrink_to_fit();
            self.tiles = new_tiles;
            self.tile_index = 0;
        }

        let tile = self.tiles[self.tile_index];
        self.tile_index += 1;
        Some(tile)
    }
}

/// Convert x & y to a TileCache (tc) directory parts
fn xy_to_tc(x: u32, y: u32) -> [String; 6] {
    [
        format!("{:03}", x / 1_000_000),
        format!("{:03}", (x / 1_000) % 1_000),
        format!("{:03}", x % 1_000),
        format!("{:03}", y / 1_000_000),
        format!("{:03}", (y / 1_000) % 1_000),
        format!("{:03}", y % 1_000),
    ]
}

/// Convert x & y to a MapProxy (mp) directory parts
fn xy_to_mp(x: u32, y: u32) -> [String; 4] {
    [
        format!("{:04}", x / 10_000),
        format!("{:04}", x % 10_000),
        format!("{:04}", y / 10_000),
        format!("{:04}", y % 10_000),
    ]
}

/// Convert x & y to a TileStash (ts) safe directory parts
fn xy_to_ts(x: u32, y: u32) -> [String; 4] {
    [
        format!("{:03}", x / 1_000),
        format!("{:03}", x % 1_000),
        format!("{:03}", y / 1_000),
        format!("{:03}", y % 1_000),
    ]
}

/// Convert x & y to a ModTile metatile directory parts
fn xy_to_mt(x: u32, y: u32) -> [String; 5] {
    // /[Z]/[xxxxyyyy]/[xxxxyyyy]/[xxxxyyyy]/[xxxxyyyy]/[xxxxyyyy].png
    // i.e. /[Z]/a/b/c/d/e.png

    let mut x = x;
    let mut y = y;

    let e = (((x & 0x0f) << 4) | (y & 0x0f)) as u8;
    x >>= 4;
    y >>= 4;

    let d = (((x & 0x0f) << 4) | (y & 0x0f)) as u8;
    x >>= 4;
    y >>= 4;

    let c = (((x & 0b000_1111 as u32) << 4) | (y & 0b000_1111 as u32)) as u8;
    x >>= 4;
    y >>= 4;

    let b = (((x & 0b000_1111 as u32) << 4) | (y & 0b000_1111 as u32)) as u8;
    x >>= 4;
    y >>= 4;

    let a = (((x & 0b000_1111 as u32) << 4) | (y & 0b000_1111 as u32)) as u8;
    //x >>= 4;
    //y >>= 4;

    [
        format!("{}", a),
        format!("{}", b),
        format!("{}", c),
        format!("{}", d),
        format!("{}", e),
    ]
}

/// How many times are in this soom level? Returns None if there would be a usize overflow
fn num_tiles_in_zoom(zoom: u8) -> Option<usize> {
    // From experience it looks like you can't calc above zoom >= 6
    if zoom == 0 {
        // Special case of known value
        Some(1)
    } else if zoom <= 5 {
        Some(2u64.pow(2u32.pow(zoom as u32)) as usize)
    } else {
        None
    }
}

pub fn xy_to_zorder(x: u32, y: u32) -> u64 {
    let mut res: u64 = 0;
    for i in 0..32 {
        let x_set: bool = (x >> i) & 1 == 1;
        let y_set: bool = (y >> i) & 1 == 1;
        if x_set {
            res |= 1 << (i * 2);
        }
        if y_set {
            res |= 1 << (i * 2) + 1;
        }
    }

    res
}

pub fn zorder_to_xy(zorder: u64) -> (u32, u32) {
    let mut x: u32 = 0;
    let mut y: u32 = 0;

    for i in 0..32 {
        let x_bit_set = (zorder >> (i * 2)) & 1 == 1;
        let y_bit_set = (zorder >> ((i * 2) + 1)) & 1 == 1;

        if x_bit_set {
            x |= 1 << i;
        }
        if y_bit_set {
            y |= 1 << i;
        }
    }

    (x, y)
}