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
extern crate regex;

use regex::Regex;

#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub struct Tile {
    pub zoom: u8,
    pub x: u32,
    pub y: u32,
}

impl Tile {
    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
        }
    }

    pub fn from_tms(tms: &str) -> Option<Tile> {
        // FIXME statically compile this regex or regex macro
        let re = 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);
        if caps.is_none() {
            return None;
        }
        let caps = caps.unwrap();

        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();
        let x = x.unwrap();
        let y = y.unwrap();

        let zoom = zoom.parse();
        let x = x.parse();
        let y = y.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.

    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)
            }
        }
    }

    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}])
            }
        }
    }

    pub fn centre_point(&self) -> LatLon {
        tile_nw_lat_lon(self.zoom, (self.x as f32)+0.5, (self.y as f32)+0.5)
    }

    pub fn center_point(&self) -> LatLon {
        self.centre_point()
    }

    pub fn nw_corner(&self) -> LatLon {
        tile_nw_lat_lon(self.zoom, (self.x as f32), (self.y as f32))
    }

    pub fn ne_corner(&self) -> LatLon {
        tile_nw_lat_lon(self.zoom, (self.x as f32)+1.0, (self.y as f32))
    }

    pub fn sw_corner(&self) -> LatLon {
        tile_nw_lat_lon(self.zoom, (self.x as f32), (self.y as f32)+1.0)
    }

    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
    }

    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)
    }

    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)
    }

    pub fn all() -> AllTilesIterator {
        AllTilesIterator{ next_zoom: 0, next_x: 0, next_y: 0}
    }

    pub fn all_to_zoom(max_zoom: u8) -> AllTilesToZoomIterator {
        AllTilesToZoomIterator{ max_zoom: max_zoom, next_zoom: 0, next_x: 0, next_y: 0}
    }

    pub fn bbox(&self) -> BBox {
        let nw = self.nw_corner();
        let se = self.se_corner();

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

}

pub struct AllTilesIterator {
    next_zoom: u8,
    next_x: u32,
    next_y: u32,
}

impl Iterator for AllTilesIterator {
    type Item = Tile;

    fn next(&mut self) -> Option<Tile> {
        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
    }
}

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);
    if remaining_after_this_column.is_none() {
        return None;
    }
    let remaining_after_this_column = remaining_after_this_column.unwrap();


    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))
    }
}


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()
}

#[derive(PartialEq, Debug)]
pub struct LatLon {
    lat: f32,
    lon: f32,
}

impl LatLon {
    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
        }
    }

}

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

impl BBox {
    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
        }
    }

    pub fn new_from_points(topleft: &LatLon, bottomright: &LatLon) -> BBox {
        BBox{ top: topleft.lat, left: topleft.lon, bottom: bottomright.lat, right: bottomright.lon }
    }

    pub fn contains_point(&self, point: &LatLon) -> bool {
        (point.lat <= self.top && point.lat > self.bottom && point.lon >= self.left && point.lon < self.right)
    }

    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)
    }

    pub fn tiles(&self) -> BBoxTilesIterator {
        BBoxTilesIterator::new(&self)
    }

}

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].clone();
        self.tile_index += 1;
        Some(tile)
    }
}


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),
    ]
}

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),
    ]
}

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
    }
}

// TODO do mod_tile tile format

mod test {

    #[test]
    fn tc() {
        use super::xy_to_tc;

        let res = xy_to_tc(3, 4);
        assert_eq!(res[0], "000");
        assert_eq!(res[1], "000");
        assert_eq!(res[2], "003");
        assert_eq!(res[3], "000");
        assert_eq!(res[4], "000");
        assert_eq!(res[5], "004");
    }

    #[test]
    fn mp() {
        use super::xy_to_mp;

        let res = xy_to_mp(3, 4);
        assert_eq!(res[0], "0000");
        assert_eq!(res[1], "0003");
        assert_eq!(res[2], "0000");
        assert_eq!(res[3], "0004");
    }

    #[test]
    fn tiles_parsing() {
        use super::Tile;

        let tile = Tile::new(1, 5, 5);
        assert!(tile.is_none());

        assert!(Tile::new(4, 8, 9).is_some());

        let tile = Tile::new(1, 0, 0);
        assert!(tile.is_some());

        assert!(Tile::new(100, 0, 0).is_none());
    }

    #[test]
    fn tiles() {
        use super::{Tile, LatLon};
        let tile = Tile::new(1, 0, 0);

        let tile = tile.unwrap();
        let parent = tile.parent();
        assert!(parent.is_some());
        let parent = parent.unwrap();
        assert_eq!(parent, Tile::new(0, 0, 0).unwrap());

        assert_eq!(parent.centre_point(), LatLon::new(0f32, 0f32).unwrap());
        assert_eq!(parent.nw_corner(), LatLon::new(85.05112, -180.0).unwrap());
        assert_eq!(parent.ne_corner(), LatLon::new(85.05112, 180.0).unwrap());
        assert_eq!(parent.sw_corner(), LatLon::new(-85.05112, -180.0).unwrap());
        assert_eq!(parent.se_corner(), LatLon::new(-85.05112, 180.0).unwrap());

        assert_eq!(parent.top(), 85.05112);
        assert_eq!(parent.bottom(), -85.05112);
        assert_eq!(parent.left(), -180.0);
        assert_eq!(parent.right(), 180.0);


        assert_eq!(parent.tc_path("png"), "0/000/000/000/000/000/000.png");
        assert_eq!(parent.mp_path("png"), "0/0000/0000/0000/0000.png");

        let children = parent.subtiles();
        assert_eq!(children.is_none(), false);
        let children: [Tile; 4] = children.unwrap();
        assert_eq!(children[0], Tile::new(1, 0, 0).unwrap());
        assert_eq!(children[0].tc_path("png"), "1/000/000/000/000/000/000.png");

        assert_eq!(children[1], Tile::new(1, 1, 0).unwrap());
        assert_eq!(children[1].tc_path("png"), "1/000/000/001/000/000/000.png");

        assert_eq!(children[2], Tile::new(1, 0, 1).unwrap());
        assert_eq!(children[2].tc_path("png"), "1/000/000/000/000/000/001.png");

        assert_eq!(children[3], Tile::new(1, 1, 1).unwrap());
        assert_eq!(children[3].tc_path("png"), "1/000/000/001/000/000/001.png");
        
    }

    #[test]
    fn tile_from_tms() {
        use super::Tile;

        fn known_good(tms: &str, zoom: u8, x: u32, y: u32) {
            let tile = Tile::from_tms(tms);
            assert!(tile.is_some());
            let tile = tile.unwrap();
            assert_eq!(tile.zoom, zoom);
            assert_eq!(tile.x, x);
            assert_eq!(tile.y, y);
        }

        fn known_bad(tms: &str) {
            let tile = Tile::from_tms(tms);
            assert!(tile.is_none());
        }

        known_good("/0/0/0.png", 0, 0, 0);
        known_good("/17/1/1234.png", 17, 1, 1234);
        known_good("17/1/1234", 17, 1, 1234);
        known_good("17/1/1234.jpeg", 17, 1, 1234);
        known_good("/17/1/1234.jpeg", 17, 1, 1234);

        known_bad("foo");
        known_bad("/17/1/1234.jpegz");
        known_bad("/17z/1/1234.jpegz");
        known_bad("/0/1/1.png");
        known_bad("/100/1/1.png");
    }

    #[test]
    fn all_tiles() {
        use super::Tile;

        let mut it = Tile::all();

        assert_eq!(it.next(), Tile::new(0, 0, 0));
        assert_eq!(it.next(), Tile::new(1, 0, 0));
        assert_eq!(it.next(), Tile::new(1, 0, 1));
        assert_eq!(it.next(), Tile::new(1, 1, 0));
        assert_eq!(it.next(), Tile::new(1, 1, 1));
        assert_eq!(it.next(), Tile::new(2, 0, 0));
        assert_eq!(it.next(), Tile::new(2, 0, 1));
        assert_eq!(it.next(), Tile::new(2, 0, 2));
        assert_eq!(it.next(), Tile::new(2, 0, 3));
        assert_eq!(it.next(), Tile::new(2, 1, 0));

        let it = Tile::all();
        let z5_tiles: Vec<Tile> = it.skip_while(|t| { t.zoom < 5 }).take(1).collect();
        assert_eq!(z5_tiles[0], Tile::new(5, 0, 0).unwrap());

    }

    #[test]
    fn bbox_create() {
        use super::{BBox, LatLon};

        // left=5.53 bottom=47.23 right=15.38 top=54.96
        let b1: Option<BBox> = BBox::new(54.9, 5.5, 47.2, 15.38);
        assert!(b1.is_some());
        let b1 = b1.unwrap();
        assert_eq!(b1.top, 54.9);

        let p1 = LatLon::new(54.9, 5.5).unwrap();
        let p2 = LatLon::new(47.2, 15.38).unwrap();
        let b2: BBox = BBox::new_from_points(&p1, &p2);
        assert_eq!(b1, b2);
    }

    #[test]
    fn bbox_tile() {
        use super::{BBox, Tile};
        let t = Tile::new(0, 0, 0).unwrap();
        assert_eq!(t.bbox(), BBox::new(85.05112, -180., -85.05112, 180.).unwrap());
    }

    #[test]
    fn bbox_contains_point(){
        use super::{Tile, LatLon};
        // triangle from London, to Bristol to Birmingham
        let tile = Tile::new(7, 63, 42).unwrap();
        let bbox = tile.bbox();
        let point1 = LatLon::new(51.75193, -1.25781).unwrap();  // oxford
        let point2 = LatLon::new(48.7997, 2.4218).unwrap();     // paris

        assert!(bbox.contains_point(&point1));
        assert!(!bbox.contains_point(&point2));

        // only the top and left borders are included in the bbox
        let nw_corner = tile.nw_corner();
        assert!(bbox.contains_point(&nw_corner));

        // Create  new point on the top edge along to the right from the NW corner
        let nw_right = LatLon::new(nw_corner.lat, nw_corner.lon+0.001).unwrap();
        assert!(bbox.contains_point(&nw_right));

        
        assert!(!bbox.contains_point(&tile.sw_corner()));
        assert!(!bbox.contains_point(&tile.ne_corner()));
        assert!(!bbox.contains_point(&tile.se_corner()));
    }

    #[test]
    fn bbox_overlaps() {
        use super::Tile;

        let tile = Tile::new(7, 63, 42).unwrap();
        let parent_tile = tile.parent().unwrap();

        assert!(parent_tile.bbox().overlaps_bbox(&tile.bbox()));

        let tile2 = Tile::new(7, 63, 43).unwrap();
        assert!(!tile.bbox().overlaps_bbox(&tile2.bbox()));
    }

    #[test]
    fn bbox_tile_iter() {
        use super::{BBox, Tile};

        // left=-11.32 bottom=51.11 right=-4.97 top=55.7
        let ie_bbox = BBox::new(55.7, -11.32, 51.11, -4.97).unwrap();
        let mut tiles = ie_bbox.tiles();
        assert_eq!(tiles.next(), Tile::new(0, 0, 0));
        assert_eq!(tiles.next(), Tile::new(1, 0, 0));
        assert_eq!(tiles.next(), Tile::new(2, 1, 1));
        assert_eq!(tiles.next(), Tile::new(3, 3, 2));
        assert_eq!(tiles.next(), Tile::new(4, 7, 5));
        assert_eq!(tiles.next(), Tile::new(5, 14, 10));
        assert_eq!(tiles.next(), Tile::new(5, 15, 10));
        assert_eq!(tiles.next(), Tile::new(6, 29, 20));
        assert_eq!(tiles.next(), Tile::new(6, 29, 21));

    }

    #[test]
    fn test_num_tiles_in_zoom() {
        use super::num_tiles_in_zoom;

        assert_eq!(num_tiles_in_zoom(0), Some(1));
        assert_eq!(num_tiles_in_zoom(1), Some(4));
        assert_eq!(num_tiles_in_zoom(2), Some(16));
        assert_eq!(num_tiles_in_zoom(3), Some(256));
        assert_eq!(num_tiles_in_zoom(4), Some(65_536));
        assert_eq!(num_tiles_in_zoom(5), Some(4_294_967_296));

        assert_eq!(num_tiles_in_zoom(6), None);

        // Can't do these because the integers overflow
        //assert_eq!(num_tiles_in_zoom(17), 17_179_869_184);
        //assert_eq!(num_tiles_in_zoom(18), 68_719_476_736);
        //assert_eq!(num_tiles_in_zoom(19), 274_877_906_944);
    }

    #[test]
    fn test_remaining_in_zoom() {
        use super::remaining_in_this_zoom;

        assert_eq!(remaining_in_this_zoom(0, 0, 0), Some(1));

        assert_eq!(remaining_in_this_zoom(1, 0, 0), Some(4));
        assert_eq!(remaining_in_this_zoom(1, 0, 1), Some(3));
        assert_eq!(remaining_in_this_zoom(1, 1, 0), Some(2));
        assert_eq!(remaining_in_this_zoom(1, 1, 1), Some(1));

        assert_eq!(remaining_in_this_zoom(2, 0, 0), Some(16));
    }

    #[test]
    fn all_tiles_to_zoom_iter() {
        use super::Tile;

        let mut it = Tile::all_to_zoom(1);

        assert_eq!(it.next(), Tile::new(0, 0, 0));
        assert_eq!(it.next(), Tile::new(1, 0, 0));
        assert_eq!(it.next(), Tile::new(1, 0, 1));
        assert_eq!(it.next(), Tile::new(1, 1, 0));
        assert_eq!(it.next(), Tile::new(1, 1, 1));
        assert_eq!(it.next(), None);


        assert_eq!(Tile::all_to_zoom(0).count(), 1);
        assert_eq!(Tile::all_to_zoom(1).count(), 5);
        assert_eq!(Tile::all_to_zoom(2).count(), 21);
        assert_eq!(Tile::all_to_zoom(3).count(), 85);

        assert_eq!(Tile::all_to_zoom(2).last(), Tile::new(2, 3, 3));

        // check the size hints
        assert_eq!(Tile::all_to_zoom(0).size_hint(), (1, Some(1)));

        let mut it = Tile::all_to_zoom(1);
        assert_eq!(it.size_hint(), (5, Some(5)));
        assert!(it.next().is_some());
        assert_eq!(it.size_hint(), (4, Some(4)));
        assert!(it.next().is_some());
        assert_eq!(it.size_hint(), (3, Some(3)));
        assert!(it.next().is_some());
        assert_eq!(it.size_hint(), (2, Some(2)));
        assert!(it.next().is_some());
        assert_eq!(it.size_hint(), (1, Some(1)));
        assert!(it.next().is_some());
        assert_eq!(it.size_hint(), (0, Some(0)));
        assert!(it.next().is_none());

        assert_eq!(Tile::all_to_zoom(2).size_hint(), (21, Some(21)));

        assert_eq!(Tile::all_to_zoom(3).size_hint(), (277, Some(277)));
        assert_eq!(Tile::all_to_zoom(4).size_hint(), (65_813, Some(65_813)));
        assert_eq!(Tile::all_to_zoom(5).size_hint(), (4_295_033_109, Some(4_295_033_109)));
        assert_eq!(Tile::all_to_zoom(6).size_hint(), (18_446_744_073_709_551_615, None));
        assert_eq!(Tile::all_to_zoom(7).size_hint(), (18_446_744_073_709_551_615, None));

    }
}