use std::iter::zip;
use tile_grid::*;
#[cfg(feature = "projtransform")]
const DEFAULT_GRID_COUNT: usize = 8;
#[cfg(not(feature = "projtransform"))]
const DEFAULT_GRID_COUNT: usize = 3;
#[test]
fn test_default_grids() {
let registry = tms();
dbg!(registry.list().collect::<Vec<_>>());
assert_eq!(registry.list().count(), DEFAULT_GRID_COUNT);
}
#[test]
fn test_tms_properties() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
assert_eq!(tms.crs().to_urn(), "urn:ogc:def:crs:EPSG:0:3857");
assert_eq!(meters_per_unit(tms.crs()), 1.0);
assert_eq!(tms.minzoom(), 0);
assert_eq!(tms.maxzoom(), 24);
}
#[test]
fn test_tile_coordinates() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
assert_eq!(tms.tile(-179.0, 85.0, 5).unwrap(), Xyz::new(0, 0, 5));
assert_eq!(tms.tile(20.0, 15.0, 5).unwrap(), Xyz::new(17, 14, 5));
}
#[test]
fn test_bounds() {
let expected = [-9.140625, 53.12040528310657, -8.7890625, 53.33087298301705];
let tms = tms().lookup("WebMercatorQuad").unwrap();
let bbox = tms.bounds(&Xyz::new(486, 332, 10)).unwrap();
for (a, b) in zip(expected, [bbox.left, bbox.bottom, bbox.right, bbox.top]) {
assert_eq!(round_to_prec(a - b, 7).abs(), 0.0);
}
}
#[test]
fn test_xy_bounds() {
let expected = [
-1017529.7205322663,
7005300.768279833,
-978393.962050256,
7044436.526761846,
];
let tms = tms().lookup("WebMercatorQuad").unwrap();
let bounds = tms.xy_bounds(&Xyz::new(486, 332, 10));
for (a, b) in zip(
expected,
[bounds.left, bounds.bottom, bounds.right, bounds.top],
) {
assert_eq!(round_to_prec(a - b, 7).abs(), 0.0);
}
}
#[test]
fn test_ul_tile() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
let xy = tms.ul(&Xyz::new(486, 332, 10)).unwrap();
let expected = [-9.140625, 53.33087298301705];
for (a, b) in zip(expected, [xy.x, xy.y]) {
assert!(a - b < 1e-7);
}
}
fn round_to_prec(number: f64, precision: u8) -> f64 {
let factor = 10.0_f64.powi(precision as i32);
(number * factor).round() / factor
}
#[test]
fn test_projul_tile() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
let xy = tms.xy_ul(&Xyz::new(486, 332, 10));
let expected = [-1017529.7205322663, 7044436.526761846];
for (a, b) in zip(expected, [xy.x, xy.y]) {
assert_eq!(round_to_prec(a - b, 7).abs(), 0.0);
}
}
#[test]
fn test_projtile() {
}
#[test]
fn test_feature() {
}
#[test]
fn test_ul() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
let expected = [-9.140625, 53.33087298301705];
let lnglat = tms.ul(&Xyz::new(486, 332, 10)).unwrap();
for (a, b) in zip(expected, [lnglat.x, lnglat.y]) {
assert_eq!(round_to_prec(a - b, 7), 0.0);
}
}
#[test]
fn test_bbox() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
let expected = [-9.140625, 53.12040528310657, -8.7890625, 53.33087298301705];
let bbox = tms.bounds(&Xyz::new(486, 332, 10)).unwrap();
for (a, b) in zip(expected, [bbox.left, bbox.bottom, bbox.right, bbox.top]) {
assert_eq!(round_to_prec(a - b, 7).abs(), 0.0);
}
}
#[test]
fn test_xy_tile() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
let ul = tms.ul(&Xyz::new(486, 332, 10)).unwrap();
let xy = tms.xy(ul.x, ul.y).unwrap();
let expected = [-1017529.7205322663, 7044436.526761846];
for (a, b) in zip(expected, [xy.x, xy.y]) {
assert!((a - b).abs() < 0.0000001);
}
}
#[test]
fn test_xy_null_island() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
let xy = tms.xy(0.0, 0.0).unwrap();
let expected = [0.0, 0.0];
for (a, b) in zip(expected, [xy.x, xy.y]) {
assert!((a - b).abs() < 1e-7);
}
}
#[test]
fn test_xy_south_pole() {
}
#[test]
fn test_xy_north_pole() {
}
#[test]
#[cfg(feature = "projtransform")]
fn test_xy_truncate() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
assert_eq!(
tms.xy_truncated(-181.0, 0.0).unwrap(),
tms.xy(tms.bbox().unwrap().left, 0.0).unwrap()
);
}
#[test]
fn test_lnglat() {
}
#[test]
fn test_lnglat_gdal3() {
}
#[test]
fn test_lnglat_xy_roundtrip() {
}
#[test]
fn test_xy_bounds_mercantile() {
}
#[test]
fn test_tile_not_truncated() {
}
#[test]
fn test_tile_truncate() {
}
#[test]
#[cfg(feature = "projtransform")]
fn test_tiles() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
let bounds = (-105.0, 39.99, -104.99, 40.0);
let tiles = tms
.tiles(bounds.0, bounds.1, bounds.2, bounds.3, &vec![14], false)
.unwrap();
let expect = vec![Xyz::new(3413, 6202, 14), Xyz::new(3413, 6203, 14)];
assert_eq!(tiles.collect::<Vec<Xyz>>(), expect);
let bounds = (-105.0, 39.99, -104.99, 40.0);
let tiles = tms
.tiles(bounds.0, bounds.1, bounds.2, bounds.3, &vec![14], false)
.unwrap();
let expect = vec![Xyz::new(3413, 6202, 14), Xyz::new(3413, 6203, 14)];
assert_eq!(tiles.collect::<Vec<Xyz>>(), expect);
assert_eq!(
tms.tiles(-181.0, 0.0, -170.0, 10.0, &vec![2], true)
.unwrap()
.collect::<Vec<Xyz>>(),
tms.tiles(-180.0, 0.0, -170.0, 10.0, &vec![2], false)
.unwrap()
.collect::<Vec<Xyz>>()
);
assert_eq!(
tms.tiles(-180.0, -90.0, 180.0, 90.0, &vec![0], false)
.unwrap()
.collect::<Vec<Xyz>>(),
vec![Xyz::new(0, 0, 0)]
);
assert_eq!(
tms.tiles(-180.0, -90.0, 180.0, 90.0, &vec![0], false)
.unwrap()
.collect::<Vec<Xyz>>(),
vec![Xyz::new(0, 0, 0)]
);
let bounds = (175.0, 5.0, -175.0, 10.0);
assert_eq!(
tms.tiles(bounds.0, bounds.1, bounds.2, bounds.3, &vec![2], false)
.unwrap()
.count(),
2
);
}
#[test]
fn test_extend_zoom() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
let merc = tms.xy_bounds(&Xyz::new(1000, 1000, 25));
let more = tms.xy_bounds(&Xyz::new(1000, 1000, 25));
for (a, b) in zip(
[more.left, more.bottom, more.right, more.top],
[merc.left, merc.bottom, merc.right, merc.top],
) {
assert_eq!(round_to_prec(a - b, 7), 0.0);
}
let merc = tms.xy_bounds(&Xyz::new(2000, 2000, 26));
let more = tms.xy_bounds(&Xyz::new(2000, 2000, 26));
for (a, b) in zip(
[more.left, more.bottom, more.right, more.top],
[merc.left, merc.bottom, merc.right, merc.top],
) {
assert_eq!(round_to_prec(a - b, 7), 0.0);
}
let merc = tms.xy_bounds(&Xyz::new(2000, 2000, 27));
let more = tms.xy_bounds(&Xyz::new(2000, 2000, 27));
for (a, b) in zip(
[more.left, more.bottom, more.right, more.top],
[merc.left, merc.bottom, merc.right, merc.top],
) {
assert_eq!(round_to_prec(a - b, 7), 0.0);
}
let merc = tms.xy_bounds(&Xyz::new(2000, 2000, 30));
let more = tms.xy_bounds(&Xyz::new(2000, 2000, 30));
for (a, b) in zip(
[more.left, more.bottom, more.right, more.top],
[merc.left, merc.bottom, merc.right, merc.top],
) {
assert_eq!(round_to_prec(a - b, 7), 0.0);
}
}
#[test]
fn test_parent_multi() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
let parent = tms.parent(&Xyz::new(486, 332, 10), Some(8)).unwrap();
assert_eq!(parent[0], Xyz::new(121, 83, 8));
}
#[test]
fn test_children() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
let x = 243;
let y = 166;
let z = 9;
let children = tms.children(&Xyz::new(x, y, z), None).unwrap();
assert_eq!(children.len(), 4);
assert!(children.contains(&Xyz::new(2 * x, 2 * y, z + 1)));
assert!(children.contains(&Xyz::new(2 * x + 1, 2 * y, z + 1)));
assert!(children.contains(&Xyz::new(2 * x + 1, 2 * y + 1, z + 1)));
assert!(children.contains(&Xyz::new(2 * x, 2 * y + 1, z + 1)));
}
#[test]
fn test_children_multi() {
let tms = tms().lookup("WebMercatorQuad").unwrap();
let children = tms.children(&Xyz::new(243, 166, 9), Some(11)).unwrap();
assert_eq!(children.len(), 16);
let targets = [
Xyz::new(972, 664, 11),
Xyz::new(973, 664, 11),
Xyz::new(973, 665, 11),
Xyz::new(972, 665, 11),
Xyz::new(974, 664, 11),
Xyz::new(975, 664, 11),
Xyz::new(975, 665, 11),
Xyz::new(974, 665, 11),
Xyz::new(974, 666, 11),
Xyz::new(975, 666, 11),
Xyz::new(975, 667, 11),
Xyz::new(974, 667, 11),
Xyz::new(972, 666, 11),
Xyz::new(973, 666, 11),
Xyz::new(973, 667, 11),
Xyz::new(972, 667, 11),
];
for target in targets {
assert!(children.contains(&target));
}
}