#geocoding #http-service #service #coordinates #nearest #find #name

geosuggest-core

Suggest by name or find nearest by coordinates cities

15 releases

0.6.4 Sep 20, 2024
0.6.3 Jun 15, 2024
0.6.2 Mar 23, 2024
0.5.3 Dec 27, 2023
0.5.1 Sep 28, 2023

#128 in HTTP server

Download history 361/week @ 2024-07-24 177/week @ 2024-07-31 184/week @ 2024-08-07 319/week @ 2024-08-14 216/week @ 2024-08-21 194/week @ 2024-08-28 178/week @ 2024-09-04 161/week @ 2024-09-11 355/week @ 2024-09-18 199/week @ 2024-09-25 152/week @ 2024-10-02 181/week @ 2024-10-09 183/week @ 2024-10-16 202/week @ 2024-10-23 174/week @ 2024-10-30 144/week @ 2024-11-06

716 downloads per month
Used in 2 crates

MIT license

52KB
1K SLoC

geosuggest-core

Library to suggest and to find nearest by coordinates cities

Live demo with sources

HTTP service

Examples

Usage example

use tokio;
use anyhow::Result;

use geosuggest_core::{Engine, storage::{self, IndexStorage}};
use geosuggest_utils::{IndexUpdater, IndexUpdaterSettings};

#[tokio::main]
async fn main() -> Result<()> {
    println!("Build index...");
    let engine = load_engine().await?;

    println!(
        "Suggest result: {:#?}",
        engine.suggest::<&str>("Beverley", 1, None, Some(&["us"]))
    );
    println!(
        "Reverse result: {:#?}",
        engine.reverse::<&str>((11.138298, 57.510973), 1, None, None)
    );

    Ok(())
}

async fn load_engine() -> Result<Engine> {
    let index_file = std::path::Path::new("/tmp/geosuggest-index.bincode");

    let updater = IndexUpdater::new(IndexUpdaterSettings {
        names: None, // no multilang support
        ..Default::default()
    })?;

    let storage = storage::bincode::Storage::new();

    Ok(if index_file.exists() {
        // load existed index
        let metadata = storage
            .read_metadata(index_file)
            .map_err(|e| anyhow::anyhow!("On load index metadata from {index_file:?}: {e}"))?;

        match metadata {
            Some(m) if updater.has_updates(&m).await? => {
                let engine = updater.build().await?;
                storage
                    .dump_to(index_file, &engine)
                    .map_err(|e| anyhow::anyhow!("Failed dump to {index_file:?}: {e}"))?;
                engine
            }
            _ => storage
                .load_from(index_file)
                .map_err(|e| anyhow::anyhow!("On load index from {index_file:?}: {e}"))?,
        }
    } else {
        // initial
        let engine = updater.build().await?;
        storage
            .dump_to(index_file, &engine)
            .map_err(|e| anyhow::anyhow!("Failed dump to {index_file:?}: {e}"))?;
        engine
    })

}

Dependencies

~7–31MB
~477K SLoC