rtz 0.8.0

A tool to easily work with geo lookups via a binary, a library, or a server.
Documentation
//! The [OpenStreetMap](https://www.openstreetmap.org/) admin lookup module.

use std::{collections::HashMap, sync::OnceLock};

use rtz_core::geo::{
    admin::osm::OsmAdmin,
    shared::{ConcreteVec, EncodableIds, RoundLngLat},
};

use crate::{
    geo::shared::{HasItemData, HasLookupData},
    CanPerformGeoLookup,
};

#[cfg(feature = "self-contained")]
use include_bytes_aligned::include_bytes_aligned;

// Trait impls.

impl HasItemData for OsmAdmin {
    fn get_mem_items() -> &'static ConcreteVec<OsmAdmin> {
        static TIMEZONES: OnceLock<ConcreteVec<OsmAdmin>> = OnceLock::new();

        #[cfg(feature = "self-contained")]
        {
            TIMEZONES.get_or_init(|| crate::geo::shared::decode_binary_data(ADMIN_BINCODE))
        }

        #[cfg(not(feature = "self-contained"))]
        {
            use rtz_core::geo::{admin::osm::get_geojson_features_from_source, shared::get_items_from_features};

            TIMEZONES.get_or_init(|| {
                let features = get_geojson_features_from_source();

                get_items_from_features(features)
            })
        }
    }
}

impl HasLookupData for OsmAdmin {
    type Lookup = EncodableIds;

    fn get_mem_lookup() -> &'static HashMap<RoundLngLat, Self::Lookup> {
        static CACHE: OnceLock<HashMap<RoundLngLat, EncodableIds>> = OnceLock::new();

        #[cfg(feature = "self-contained")]
        {
            CACHE.get_or_init(|| crate::geo::shared::decode_binary_data(LOOKUP_BINCODE))
        }

        #[cfg(not(feature = "self-contained"))]
        {
            use rtz_core::geo::shared::get_lookup_from_geometries;

            CACHE.get_or_init(|| {
                let cache = get_lookup_from_geometries(OsmAdmin::get_mem_items());

                cache
            })
        }
    }
}

impl CanPerformGeoLookup for OsmAdmin {}

// Statics.

#[cfg(all(host_family_unix, feature = "self-contained"))]
static ADMIN_BINCODE: &[u8] = include_bytes_aligned!(8, "../../../assets/osm_admins.bincode");
#[cfg(all(host_family_windows, feature = "self-contained"))]
static ADMIN_BINCODE: &[u8] = include_bytes_aligned!(8, "..\\..\\..\\assets\\osm_admins.bincode");

#[cfg(all(host_family_unix, feature = "self-contained"))]
static LOOKUP_BINCODE: &[u8] = include_bytes_aligned!(8, "../../../assets/osm_admin_lookup.bincode");
#[cfg(all(host_family_windows, feature = "self-contained"))]
static LOOKUP_BINCODE: &[u8] = include_bytes_aligned!(8, "..\\..\\..\\assets\\osm_admin_lookup.bincode");

// Tests.

#[cfg(test)]
mod tests {
    use crate::geo::shared::{CanPerformGeoLookup, HasItemData, MapIntoItems};

    use super::*;
    use pretty_assertions::assert_eq;
    use rayon::prelude::{IntoParallelIterator, ParallelIterator};
    use rtz_core::base::types::Float;

    #[test]
    fn can_get_timezones() {
        let admins = OsmAdmin::get_mem_items();
        // Sanity floor rather than an exact count: the admin set grows with every OSM refresh, so
        // pinning the exact size breaks each legitimate regen. A generous floor still catches a
        // truncated or empty regen.
        assert!(admins.len() > 300_000, "admin item count implausibly low: {}", admins.len());
    }

    #[test]
    fn can_get_lookup() {
        let cache = OsmAdmin::get_mem_lookup();
        assert_eq!(cache.len(), 64_800);
    }

    #[test]
    fn can_get_from_lookup() {
        let lookup = OsmAdmin::get_lookup_suggestions(-121, 46).unwrap();
        // A populated cell keeps many candidates; assert a floor rather than the exact count, which
        // shifts with the data.
        assert!(lookup.len() >= 10, "too few lookup suggestions: {}", lookup.len());
    }

    #[test]
    fn can_perform_exact_lookup() {
        assert_eq!(OsmAdmin::lookup_slow(-177.0, -15.0).len(), 0);

        // Assert set membership, not `[0]`: (-121, 46) is inside both the country and the state,
        // and the result order is not stable across regens.
        let admins = OsmAdmin::lookup_slow(-121.0, 46.0);
        let names = admins.iter().map(|a| a.name.as_ref()).collect::<Vec<&str>>();
        assert!(names.contains(&"United States"), "expected United States in {names:?}");
        assert!(names.contains(&"Washington"), "expected Washington in {names:?}");

        assert_eq!(OsmAdmin::lookup_slow(179.9968, -67.0959).len(), 0);
    }

    #[test]
    fn can_access_lookup() {
        let cache = OsmAdmin::get_mem_lookup();

        let tzs = cache.get(&(-177, -15)).map_into_items().unwrap() as Vec<&OsmAdmin>;
        assert_eq!(tzs.len(), 0);

        // Populated cells: assert a floor and set membership, not exact counts / ordering (both
        // shift with the data).
        let tzs = cache.get(&(-121, 46)).map_into_items().unwrap() as Vec<&OsmAdmin>;
        assert!(tzs.len() >= 10, "too few items in cell (-121, 46): {}", tzs.len());
        let names = tzs.iter().map(|t| t.name.as_ref()).collect::<Vec<&str>>();
        assert!(names.contains(&"United States"), "expected United States in {names:?}");

        let tzs = cache.get(&(-87, 38)).map_into_items().unwrap() as Vec<&OsmAdmin>;
        assert!(tzs.len() >= 10, "too few items in cell (-87, 38): {}", tzs.len());
    }

    #[test]
    fn can_verify_lookup_assisted_accuracy() {
        (0..100).into_par_iter().for_each(|_| {
            let x = rand::random::<Float>() * 360.0 - 180.0;
            let y = rand::random::<Float>() * 180.0 - 90.0;
            let full = OsmAdmin::lookup_slow(x, y);
            let lookup_assisted = OsmAdmin::lookup(x, y);

            assert_eq!(
                full.into_iter().map(|t| t.id).collect::<Vec<_>>(),
                lookup_assisted.into_iter().map(|t| t.id).collect::<Vec<_>>(),
                "({}, {})",
                x,
                y
            );
        });
    }
}