Skip to content

Commit

Permalink
Add search_dist to router configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
pka committed Aug 17, 2023
1 parent 570614b commit cf39a18
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
4 changes: 4 additions & 0 deletions bbox-routing-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Basic from/to request:

curl -s 'http://localhost:8080/routes/basic?profile=railway&from_pos=9.35213353,47.0935012&to_pos=9.3422712,47.1011887'

Zurich - Munich:

curl -s 'http://localhost:8080/routes/basic?profile=railway&from_pos=8.53636,47.37726&to_pos=11.56096,48.14019'


Valhalla endpoint (e.g. for Valhalla QGIS Plugin):

Expand Down
2 changes: 2 additions & 0 deletions bbox-routing-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct RoutingServerCfg {
#[serde(deny_unknown_fields)]
pub struct RoutingServiceCfg {
pub profile: Option<String>,
/// Node search distance
pub search_dist: Option<f64>,
pub gpkg: String,
pub postgis: Option<DsPostgisCfg>,
/// Edge table
Expand Down
8 changes: 5 additions & 3 deletions bbox-routing-server/src/ds.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::RoutingServiceCfg;
use crate::engine::NodeIndex;
use crate::engine::{NodeIndex, DEFAULT_SEARCH_DISTANCE};
use crate::error::Result;
use async_trait::async_trait;
use bbox_core::pg_ds::PgDatasource;
Expand Down Expand Up @@ -42,7 +42,8 @@ impl RouterDs for GpkgLinesDs {
/// Load from GeoPackage line geometries
async fn load(&self) -> Result<GraphData> {
info!("Reading routing graph from {}", self.0.gpkg);
let mut index = NodeIndex::new();
let dist = self.0.search_dist.unwrap_or(DEFAULT_SEARCH_DISTANCE);
let mut index = NodeIndex::new(dist);
let mut input_graph = InputGraph::new();

let geom = self.0.geom.as_str();
Expand Down Expand Up @@ -85,9 +86,10 @@ impl RouterDs for PgRouteTablesDs {
let node_id = self.0.node_id.as_ref().unwrap();
let node_src = self.0.node_src.as_ref().unwrap();
let node_dst = self.0.node_dst.as_ref().unwrap();
let dist = self.0.search_dist.unwrap_or(DEFAULT_SEARCH_DISTANCE);

info!("Reading routing graph from {url}");
let mut index = NodeIndex::new();
let mut index = NodeIndex::new(dist);
let mut input_graph = InputGraph::new();
let db = PgDatasource::new_pool(url).await.unwrap();
let sql = format!(
Expand Down
19 changes: 13 additions & 6 deletions bbox-routing-server/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::path::Path;
#[derive(Clone)]
pub struct NodeIndex {
tree: RTree<Node>,
search_dist: f64,
/// lookup by node id for route result output
nodes: NodeLookup,
/// node id generation
Expand All @@ -27,14 +28,16 @@ type NodeLookup = HashMap<usize, (f64, f64)>;
type Node = GeomWithData<[f64; 2], usize>;

impl NodeIndex {
pub fn new() -> Self {
pub fn new(search_dist: f64) -> Self {
NodeIndex {
tree: RTree::new(),
search_dist,
nodes: Default::default(),
next_node_id: 0,
}
}
fn bulk_load(nodes: NodeLookup) -> Self {

fn bulk_load(nodes: NodeLookup, search_dist: f64) -> Self {
let rtree_nodes = nodes
.iter()
.map(|(id, (x, y))| Node::new([*x, *y], *id))
Expand All @@ -43,6 +46,7 @@ impl NodeIndex {
let next_node_id = nodes.keys().max().unwrap_or(&0) + 1;
NodeIndex {
tree,
search_dist,
nodes,
next_node_id,
}
Expand Down Expand Up @@ -79,7 +83,7 @@ impl NodeIndex {
}
/// Find nearest node within max distance
fn find(&self, x: f64, y: f64) -> Option<usize> {
let max = 0.01; // ~ 10km CH
let max = self.search_dist;
self.tree
.nearest_neighbor_iter_with_distance_2(&[x, y])
.next()
Expand All @@ -88,6 +92,8 @@ impl NodeIndex {
}
}

pub const DEFAULT_SEARCH_DISTANCE: f64 = 0.01; // ~ 10km CH

/// Routing engine using contraction hierarchies
#[derive(Clone)]
pub struct Router {
Expand All @@ -98,9 +104,10 @@ pub struct Router {
impl Router {
pub async fn from_config(config: &RoutingServiceCfg) -> Result<Self> {
let ds = ds_from_config(config).await?;
let dist = config.search_dist.unwrap_or(DEFAULT_SEARCH_DISTANCE);
let cache_name = ds.cache_name().to_string();
let router = if Router::cache_exists(&cache_name) {
Router::from_disk(&cache_name)?
Router::from_disk(&cache_name, dist)?
} else {
let router = Router::from_ds(ds).await?;
router.save_to_disk(&cache_name).unwrap();
Expand All @@ -115,13 +122,13 @@ impl Router {
Path::new(&format!("{base_name}.nodes.bin")).exists()
}

fn from_disk(base_name: &str) -> Result<Self> {
fn from_disk(base_name: &str, search_dist: f64) -> Result<Self> {
let fname = format!("{base_name}.nodes.bin");
info!("Reading routing graph from {fname}");
let reader = BufReader::new(File::open(fname)?);
let nodes: NodeLookup = bincode::deserialize_from(reader).unwrap();

let index = NodeIndex::bulk_load(nodes);
let index = NodeIndex::bulk_load(nodes, search_dist);

let fname = format!("{base_name}.graph.bin");
let reader = BufReader::new(File::open(fname)?);
Expand Down

0 comments on commit cf39a18

Please sign in to comment.