Function slippy_map_tilenames::lonlat2tile [] [src]

pub fn lonlat2tile(lon: f64, lat: f64, zoom: u8) -> (u32, u32)

Convert lon/lat coordinates to a slippy map tile, at a given zoom.

Arguments

  • lon - longitude coordinate (W-E), in degrees
  • lat - latitude coordinate (N-S), in degrees
  • zoom - zoomlevel of the resulting tile

Examples

extern crate slippy_map_tilenames as smt;

fn main() {
    let res = smt::lonlat2tile(14.016667, 42.683333, 13); // (4414, 3019)
    println!("lon 14.016667 E, lat 42.683333 N, at zoom 13: {:?}", res); 
}

Unexpected Behavior

The conversion relies on an equation, which does not check on the validity of the data in imput. For this reason, passing non valid coordinates still gets in output a result, albeit meaningless .

Therefore, care should be taken as to the input of the function in order to pass it valid tiles. Example:

extern crate slippy_map_tilenames as smt;
 
assert_eq!(smt::lonlat2tile(123456.789, 123456.789, 0), (343, 0));

Notice how at zoomlevel 0 there is really just a tile, (0, 0); Moreover, (123456.789, 123456.789) is not a valid coordinate; However, the function still calculates a meaningless result.

Cf. the following:

extern crate slippy_map_tilenames as smt;
use std::f64;
 
assert_eq!(smt::lonlat2tile(f64::INFINITY, f64::NEG_INFINITY, 0), (0, 0));