use std::path::PathBuf;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList};
use crate::query;
#[pyclass]
struct Geocoder {
inner: query::Index,
poi: Option<query::Index>,
}
fn hit_to_dict<'py>(py: Python<'py>, h: &query::Hit) -> PyResult<Bound<'py, PyDict>> {
let d = PyDict::new(py);
d.set_item("lat", h.lat)?;
d.set_item("lon", h.lon)?;
d.set_item("precision", h.precision)?;
d.set_item("score", h.score)?;
d.set_item("street", &h.street)?;
if let Some(hn) = &h.housenumber {
d.set_item("housenumber", hn)?;
}
d.set_item("commune", &h.commune)?;
d.set_item("postcode", &h.postcode)?;
d.set_item("confidence", h.confidence)?;
if !h.flags.is_empty() {
d.set_item("flags", h.flags.clone())?;
}
if let Some(region) = &h.region {
d.set_item("region", region)?;
}
d.set_item("weak", query::hit_is_weak(h))?;
Ok(d)
}
fn to_python<'py, T: serde::Serialize>(py: Python<'py>, value: &T) -> PyResult<Bound<'py, PyAny>> {
let text = serde_json::to_string(value)
.map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
py.import("json")?.call_method1("loads", (text,))
}
#[pymethods]
impl Geocoder {
#[new]
#[pyo3(signature = (index_path, poi = None))]
fn new(index_path: PathBuf, poi: Option<PathBuf>) -> PyResult<Self> {
let inner = query::Index::open_address(&index_path)
.map_err(|e| pyo3::exceptions::PyIOError::new_err(e.to_string()))?;
let poi = match poi {
Some(p) => Some(
query::Index::open(&p)
.map_err(|e| pyo3::exceptions::PyIOError::new_err(e.to_string()))?,
),
None => None,
};
if let Some(pi) = &poi {
query::check_pair(&inner, pi).map_err(pyo3::exceptions::PyValueError::new_err)?;
}
Ok(Geocoder { inner, poi })
}
#[pyo3(signature = (q, k = 10))]
fn geocode<'py>(&self, py: Python<'py>, q: &str, k: usize) -> PyResult<Bound<'py, PyList>> {
let hits = py.detach(|| query::query_cascade(&self.inner, self.poi.as_ref(), q, k));
let out = PyList::empty(py);
for h in &hits {
out.append(hit_to_dict(py, h)?)?;
}
Ok(out)
}
fn parse<'py>(&self, py: Python<'py>, q: &str) -> PyResult<Bound<'py, PyAny>> {
let reading = py.detach(|| crate::compare::read(&self.inner, self.poi.as_ref(), q));
to_python(py, &reading)
}
fn same_address<'py>(&self, py: Python<'py>, a: &str, b: &str) -> PyResult<Bound<'py, PyAny>> {
let same = py.detach(|| crate::compare::same_address(&self.inner, self.poi.as_ref(), a, b));
to_python(py, &same)
}
#[pyo3(signature = (queries, k = 1))]
fn geocode_many<'py>(
&self,
py: Python<'py>,
queries: Vec<String>,
k: usize,
) -> PyResult<Bound<'py, PyList>> {
use rayon::prelude::*;
let pool = batch_pool();
const CHUNK: usize = 1024;
let out = PyList::empty(py);
for chunk in queries.chunks(CHUNK) {
py.check_signals()?;
let results: Vec<Vec<query::Hit>> = py.detach(|| {
pool.install(|| {
chunk
.par_iter()
.map(|q| query::query_cascade(&self.inner, self.poi.as_ref(), q, k))
.collect()
})
});
for hits in &results {
let row = PyList::empty(py);
for h in hits {
row.append(hit_to_dict(py, h)?)?;
}
out.append(row)?;
}
}
Ok(out)
}
#[pyo3(signature = (lat, lon, k = 3))]
fn reverse<'py>(
&self,
py: Python<'py>,
lat: f64,
lon: f64,
k: usize,
) -> PyResult<Bound<'py, PyList>> {
let hits = py
.detach(|| self.inner.try_reverse(lat, lon, k))
.map_err(pyo3::exceptions::PyValueError::new_err)?;
let out = PyList::empty(py);
for h in &hits {
let d = hit_to_dict(py, h)?;
if let Some(dm) = h.distance_m {
d.set_item("distance_m", dm)?;
}
out.append(d)?;
}
Ok(out)
}
}
fn batch_pool() -> std::sync::Arc<rayon::ThreadPool> {
use std::sync::{Arc, Mutex};
static POOL: Mutex<Option<(u32, Arc<rayon::ThreadPool>)>> = Mutex::new(None);
let pid = std::process::id();
let mut slot = POOL.lock().unwrap_or_else(|e| e.into_inner());
if let Some((owner, pool)) = slot.as_ref() {
if *owner == pid {
return Arc::clone(pool);
}
}
let n = std::env::var("GRIDPIN_THREADS")
.ok()
.and_then(|v| v.parse::<usize>().ok())
.map(|v| v.max(1)) .unwrap_or_else(|| {
std::thread::available_parallelism()
.map(|c| c.get().saturating_sub(2).max(1))
.unwrap_or(2)
});
let pool = Arc::new(
rayon::ThreadPoolBuilder::new()
.num_threads(n)
.build()
.unwrap_or_else(|_| {
rayon::ThreadPoolBuilder::new()
.num_threads(1)
.build()
.unwrap()
}),
);
*slot = Some((pid, Arc::clone(&pool)));
pool
}
fn sheet_of(country: &str) -> PyResult<&'static crate::sheets::Sheet> {
crate::sheets::resolve(country)
.map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))
}
fn io_err(e: anyhow::Error) -> PyErr {
pyo3::exceptions::PyOSError::new_err(format!("{e:#}"))
}
#[pyfunction]
fn _sheets_release() -> (String, String) {
let r = crate::sheets::registry();
(r.release.clone(), r.base_url.clone())
}
#[pyfunction]
fn _sheets_registry<'py>(py: Python<'py>) -> PyResult<Bound<'py, PyList>> {
let out = PyList::empty(py);
for s in &crate::sheets::registry().sheets {
let d = PyDict::new(py);
d.set_item("code", &s.code)?;
d.set_item("name", &s.name)?;
d.set_item("file", &s.file)?;
d.set_item("size", s.size)?;
d.set_item("sha256", &s.sha256)?;
d.set_item("url", s.url())?;
out.append(d)?;
}
Ok(out)
}
#[pyfunction]
fn _sheets_home() -> PyResult<PathBuf> {
crate::sheets::home().map_err(io_err)
}
#[pyfunction]
fn _sheets_path(country: &str) -> PyResult<PathBuf> {
crate::sheets::local_path(sheet_of(country)?).map_err(io_err)
}
#[pyfunction]
fn _sheets_downloaded(country: &str) -> PyResult<bool> {
crate::sheets::is_downloaded(sheet_of(country)?).map_err(io_err)
}
#[pyfunction]
#[pyo3(signature = (country, path = None))]
fn _sheets_verify(py: Python<'_>, country: &str, path: Option<PathBuf>) -> PyResult<&'static str> {
let sheet = sheet_of(country)?;
let path = match path {
Some(p) => p,
None => crate::sheets::local_path(sheet).map_err(io_err)?,
};
let verdict = py
.detach(|| crate::sheets::verify(&path, sheet))
.map_err(io_err)?;
Ok(match verdict {
crate::sheets::Verdict::Ok => "ok",
crate::sheets::Verdict::Missing => "missing",
crate::sheets::Verdict::WrongSize(_) => "wrong_size",
crate::sheets::Verdict::WrongDigest(_) => "wrong_sha256",
})
}
#[pyfunction]
#[pyo3(signature = (country, offline = false))]
fn _sheets_fetch(py: Python<'_>, country: &str, offline: bool) -> PyResult<PathBuf> {
let sheet = sheet_of(country)?;
let offline = offline || crate::sheets::offline_from_env();
py.detach(|| crate::sheets::fetch(sheet, offline))
.map_err(io_err)
}
#[pymodule]
fn _gridpin(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Geocoder>()?;
m.add_function(wrap_pyfunction!(_sheets_release, m)?)?;
m.add_function(wrap_pyfunction!(_sheets_registry, m)?)?;
m.add_function(wrap_pyfunction!(_sheets_home, m)?)?;
m.add_function(wrap_pyfunction!(_sheets_path, m)?)?;
m.add_function(wrap_pyfunction!(_sheets_downloaded, m)?)?;
m.add_function(wrap_pyfunction!(_sheets_verify, m)?)?;
m.add_function(wrap_pyfunction!(_sheets_fetch, m)?)?;
Ok(())
}