use std::f64::consts::PI;
pub const MAX_LAT: f64 = 85.051_128_779_806_59;
pub const MIN_LAT: f64 = -MAX_LAT;
#[inline]
#[must_use]
pub fn lon_to_world_x(lon: f64) -> f64 {
(lon + 180.0) / 360.0
}
#[inline]
#[must_use]
pub fn lat_to_world_y(lat: f64) -> f64 {
let lat_rad = lat.clamp(MIN_LAT, MAX_LAT).to_radians();
let y = (1.0 - (lat_rad.tan() + 1.0 / lat_rad.cos()).ln() / PI) / 2.0;
y.clamp(0.0, 1.0)
}
#[inline]
#[must_use]
pub fn world_x_to_lon(x: f64) -> f64 {
x * 360.0 - 180.0
}
#[inline]
#[must_use]
pub fn world_y_to_lat(y: f64) -> f64 {
let n = PI * (1.0 - 2.0 * y);
n.sinh().atan().to_degrees()
}
#[inline]
#[must_use]
pub fn wgs84_to_world(lon: f64, lat: f64) -> (f64, f64) {
(lon_to_world_x(lon), lat_to_world_y(lat))
}
#[inline]
#[must_use]
pub fn world_to_wgs84(x: f64, y: f64) -> (f64, f64) {
(world_x_to_lon(x), world_y_to_lat(y))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TileCoord {
pub z: u8,
pub x: u32,
pub y: u32,
}
impl TileCoord {
#[inline]
#[must_use]
pub const fn new(z: u8, x: u32, y: u32) -> Self {
Self { z, x, y }
}
}
#[inline]
#[must_use]
pub fn world_to_tile(x: f64, y: f64, zoom: u8) -> TileCoord {
let n = (1u32 << zoom) as f64;
let tx = (x * n).floor() as u32;
let ty = (y * n).floor() as u32;
let max = (1u32 << zoom) - 1;
TileCoord::new(zoom, tx.min(max), ty.min(max))
}
#[inline]
#[must_use]
pub fn world_to_tile_frac(x: f64, y: f64, zoom: u8) -> (f64, f64) {
let n = (1u32 << zoom) as f64;
(x * n, y * n)
}
#[inline]
#[must_use]
pub fn world_to_tile_pixel(x: f64, y: f64, tile: TileCoord, extent: u32) -> (f64, f64) {
let n = (1u32 << tile.z) as f64;
let px = (x * n - tile.x as f64) * extent as f64;
let py = (y * n - tile.y as f64) * extent as f64;
(px, py)
}
#[inline]
#[must_use]
pub fn simplify_tolerance(zoom: u8) -> f64 {
1.0 / (256.0 * (1u64 << zoom) as f64)
}
#[inline]
#[must_use]
pub fn tile_bounds_world(tile: TileCoord) -> (f64, f64, f64, f64) {
let n = (1u32 << tile.z) as f64;
let x0 = tile.x as f64 / n;
let y0 = tile.y as f64 / n;
let x1 = (tile.x + 1) as f64 / n;
let y1 = (tile.y + 1) as f64 / n;
(x0, y0, x1, y1)
}
#[must_use]
pub fn tile_bounds_wgs84(tile: TileCoord) -> (f64, f64, f64, f64) {
let (x0, y0, x1, y1) = tile_bounds_world(tile);
let (lon0, lat1) = world_to_wgs84(x0, y0); let (lon1, lat0) = world_to_wgs84(x1, y1); (lon0, lat0, lon1, lat1)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roundtrip_world_coords() {
let (lon, lat) = (-122.4194, 37.7749); let (x, y) = wgs84_to_world(lon, lat);
let (lon2, lat2) = world_to_wgs84(x, y);
assert!((lon - lon2).abs() < 1e-10);
assert!((lat - lat2).abs() < 1e-10);
}
#[test]
fn origin_is_top_left() {
let (x, y) = wgs84_to_world(-180.0, MAX_LAT);
assert!((x - 0.0).abs() < 1e-10);
assert!((y - 0.0).abs() < 1e-3);
}
#[test]
fn tile_at_zoom_0() {
let tile = world_to_tile(0.5, 0.5, 0);
assert_eq!(tile, TileCoord::new(0, 0, 0));
}
#[test]
fn tile_at_zoom_1() {
let tile = world_to_tile(0.75, 0.75, 1);
assert_eq!(tile, TileCoord::new(1, 1, 1));
}
#[test]
fn world_to_tile_pixel_center() {
let tile = TileCoord::new(0, 0, 0);
let (px, py) = world_to_tile_pixel(0.5, 0.5, tile, 4096);
assert!((px - 2048.0).abs() < 1e-10);
assert!((py - 2048.0).abs() < 1e-10);
}
#[test]
fn tile_bounds_zoom_0() {
let (x0, y0, x1, y1) = tile_bounds_world(TileCoord::new(0, 0, 0));
assert!((x0 - 0.0).abs() < 1e-10);
assert!((y0 - 0.0).abs() < 1e-10);
assert!((x1 - 1.0).abs() < 1e-10);
assert!((y1 - 1.0).abs() < 1e-10);
}
#[test]
fn simplify_tolerance_decreases_with_zoom() {
for z in 0..14 {
assert!(simplify_tolerance(z) > simplify_tolerance(z + 1));
}
}
}