Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Fix offline reverse geocoding for cities at longitude exactly 180 (#2061)

NetterSilver55898271782c
diff --git a/crates/geocode/src/lib.rs b/crates/geocode/src/lib.rs
index 89fe86822..de0bea5c9 100644
--- a/crates/geocode/src/lib.rs
+++ b/crates/geocode/src/lib.rs
@@ -104,7 +104,8 @@ pub struct GeoNames {
fn cell(p: LatLon) -> (i32, i32) {
// valid positions only: |lat| ≤ 90, |lon| ≤ 180, so the casts are in range
- (p.lat.floor() as i32, p.lon.floor() as i32)
+ // longitude 180 is the same meridian as -180: key it as column -180, the wrap `reverse` probes
+ (p.lat.floor() as i32, (p.lon.floor() as i32 + 180).rem_euclid(360) - 180)
}
fn short(s: &str) -> String {
@@ -253,6 +254,14 @@ mod tests {
assert_eq!(fiji.reverse(LatLon::new(-17.0, -179.95)).unwrap().city, "X");
}
+ #[test]
+ fn city_on_the_antimeridian_is_found() {
+ let g = GeoNames::parse("1\tTaveuni\tTaveuni\t\t0.0\t180.0\tP\tPPL\tFJ\t\t01\n", None, None);
+ for lon in [179.9, -179.9, 180.0, -180.0] {
+ assert_eq!(g.reverse(LatLon::new(0.0, lon)).map(|p| p.city).as_deref(), Some("Taveuni"), "from {lon}");
+ }
+ }
+
#[test]
fn high_latitude_cities_two_cells_away() {
let city = |lat: f64, lon: f64| GeoNames::parse(&format!("1\tX\tX\t\t{lat}\t{lon}\tP\tP\tNO\t\t\n"), None, None);