Skip to main content

pysochrone/
lib.rs

1#![allow(dead_code)]
2
3mod graph;
4mod isochrone;
5mod overpass;
6mod utils;
7mod cache;
8mod simplify;
9mod error;
10mod tests;
11
12use pyo3::prelude::*;
13
14/// Calculates isochrones from a point
15#[pyfunction]
16fn calc_isochrones(
17    lat: f64,
18    lon: f64,
19    max_dist: f64,
20    time_limits: Vec<f64>,
21    network_type: String,
22    hull_type: String,
23    retain_all: Option<bool>,
24) -> PyResult<Vec<String>> {
25    let network_type_enum = match network_type.as_str() {
26        "Drive" => overpass::NetworkType::Drive,
27        "DriveService" => overpass::NetworkType::DriveService,
28        "Walk" => overpass::NetworkType::Walk,
29        "Bike" => overpass::NetworkType::Bike,
30        "All" => overpass::NetworkType::All,
31        "AllPrivate" => overpass::NetworkType::AllPrivate,
32        _ => return Err(pyo3::exceptions::PyValueError::new_err(
33            format!("Invalid network type '{}'. Expected one of: Drive, DriveService, Walk, Bike, All, AllPrivate", network_type)
34        )),
35    };
36
37    let hull_type_enum = match hull_type.as_str() {
38        "Convex" => isochrone::HullType::Convex,
39        "FastConcave" => isochrone::HullType::FastConcave,
40        "Concave" => isochrone::HullType::Concave,
41        _ => return Err(pyo3::exceptions::PyValueError::new_err(
42            format!("Invalid hull type '{}'. Expected one of: Convex, FastConcave, Concave", hull_type)
43        )),
44    };
45
46    let rt = tokio::runtime::Runtime::new()
47        .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))?;
48
49    let (isochrones, _) = rt
50        .block_on(isochrone::calculate_isochrones_from_point(
51            lat,
52            lon,
53            max_dist,
54            time_limits,
55            network_type_enum,
56            hull_type_enum,
57            retain_all.unwrap_or(false),
58        ))?;
59
60    Ok(isochrones
61        .iter()
62        .map(|iso| utils::polygon_to_geojson_string(iso))
63        .collect())
64}
65
66/// Python module for quickly creating isochrones
67#[pymodule]
68fn pysochrone(_py: Python, m: &PyModule) -> pyo3::PyResult<()> {
69    m.add_function(wrap_pyfunction!(calc_isochrones, m)?)?;
70    Ok(())
71}