Skip to content

Instantly share code, notes, and snippets.

@austenadler
austenadler / wrap.rs
Last active March 11, 2023 21:26 — forked from missinglink/wrap.js
wrap latitude and longitude values around the poles in order to normalize their ranges
/// Normalize co-ordinates that lie outside of the normal ranges.
/// Longitude wrapping simply requires adding +- 360 to the value until it comes into range.
///
/// For the latitude values, we need to flip the longitude whenever the latitude crosses a pole.
///
/// Description and original source was ported from https://gist.github.com/missinglink/d0a085188a8eab2ca66db385bb7c023a to rust
pub fn wrap_latlon(mut lat: f64, mut lon: f64) -> (f64, f64) {
let quadrant = ((lat.abs() / 90_f64).floor() % 4_f64) as i8;
let pole = if lat > 0_f64 { 90_f64 } else { -90_f64 };
let offset = lat % 90_f64;