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

geosuggest-core

Suggest by name or find nearest by coordinates cities

13 unstable releases (3 breaking)

0.6.2 Mar 23, 2024
0.6.1 Feb 19, 2024
0.6.0 Jan 8, 2024
0.5.3 Dec 27, 2023
0.3.0 Aug 18, 2023

#120 in HTTP server

Download history 117/week @ 2023-12-19 152/week @ 2023-12-26 201/week @ 2024-01-02 57/week @ 2024-01-09 110/week @ 2024-01-16 80/week @ 2024-01-23 71/week @ 2024-01-30 81/week @ 2024-02-06 160/week @ 2024-02-13 142/week @ 2024-02-20 158/week @ 2024-02-27 121/week @ 2024-03-05 126/week @ 2024-03-12 270/week @ 2024-03-19 105/week @ 2024-03-26 144/week @ 2024-04-02

660 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–37MB
~513K SLoC