cleaned up the web UI code
This commit is contained in:
parent
f11dda3939
commit
32a81b3561
1 changed files with 51 additions and 37 deletions
|
|
@ -1,23 +1,24 @@
|
|||
#[macro_use] extern crate rocket;
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
use clap::Parser;
|
||||
use std::process::exit;
|
||||
use std::fs::File;
|
||||
use fapra_osm_2::gridgraph::{GridGraph, Route, NodeId};
|
||||
use fapra_osm_2::coordinates::{RadianCoordinate, DegreeCoordinate};
|
||||
use fapra_osm_2::astar::{AStar, estimate_haversine};
|
||||
use fapra_osm_2::alt::{LandmarkSet, LandmarkBestSet};
|
||||
use fapra_osm_2::alt::{LandmarkBestSet, LandmarkSet};
|
||||
use fapra_osm_2::astar::{estimate_haversine, AStar};
|
||||
use fapra_osm_2::coordinates::{DegreeCoordinate, RadianCoordinate};
|
||||
use fapra_osm_2::gridgraph::{GridGraph, NodeId, Route};
|
||||
use rand::seq::SliceRandom;
|
||||
use serde::Serialize;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use fapra_osm_2::utils::EARTH_RADIUS;
|
||||
use std::process::exit;
|
||||
use std::time::Instant;
|
||||
|
||||
use rocket::State;
|
||||
use rocket::response::status::BadRequest;
|
||||
use rocket::form::Form;
|
||||
use rocket::fs::FileServer;
|
||||
use rocket::response::status::BadRequest;
|
||||
use rocket::serde::json::Json;
|
||||
use rocket_dyn_templates::{Template, context, Engines};
|
||||
use rocket::State;
|
||||
use rocket_dyn_templates::{context, Engines, Template};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(author, version, about, long_about=None)]
|
||||
|
|
@ -58,8 +59,7 @@ struct RouteQuery<'r> {
|
|||
r#algorithm: &'r str,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Serialize)]
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct RouteResponse {
|
||||
success: bool,
|
||||
route: Option<Route>,
|
||||
|
|
@ -67,64 +67,74 @@ pub struct RouteResponse {
|
|||
}
|
||||
|
||||
#[post("/route", data = "<routequery>")]
|
||||
fn route(routequery: Form<RouteQuery<'_>>, graphwrapper: &State<GraphWrapper>) -> Result<Json<RouteResponse>, BadRequest<String>> {
|
||||
|
||||
fn route(
|
||||
routequery: Form<RouteQuery<'_>>,
|
||||
graphwrapper: &State<GraphWrapper>,
|
||||
) -> Result<Json<RouteResponse>, BadRequest<String>> {
|
||||
let from = match DegreeCoordinate::from_string_tuple(routequery.from) {
|
||||
Ok(from) => RadianCoordinate::from(from),
|
||||
Err(e) => {return Result::Err(BadRequest(Some(format!("Error while parsing from field: {:?}", e))));}
|
||||
Err(e) => {
|
||||
return Result::Err(BadRequest(Some(format!(
|
||||
"Error while parsing from field: {:?}",
|
||||
e
|
||||
))));
|
||||
}
|
||||
};
|
||||
let to = match DegreeCoordinate::from_string_tuple(routequery.to) {
|
||||
Ok(to) => RadianCoordinate::from(to),
|
||||
Err(e) => {return Result::Err(BadRequest(Some(format!("Error while parsing to field: {:?}", e))));}
|
||||
Err(e) => {
|
||||
return Result::Err(BadRequest(Some(format!(
|
||||
"Error while parsing to field: {:?}",
|
||||
e
|
||||
))));
|
||||
}
|
||||
};
|
||||
|
||||
let from = graphwrapper.graph.get_nearest_node(from).unwrap();
|
||||
let to = graphwrapper.graph.get_nearest_node(to).unwrap();
|
||||
|
||||
println!("working on route from {:?} to {:?}", from, to);
|
||||
let direct_distance = from.position.distance_to(&to.position) * EARTH_RADIUS;
|
||||
println!("haversine distance: {}", direct_distance);
|
||||
let graph_distance = graphwrapper.graph.shortest_path(from, to).unwrap().cost;
|
||||
println!("graph distance is: {}", graph_distance);
|
||||
println!("Working on a route from {:?} to {:?}", from, to);
|
||||
|
||||
let astar = AStar {
|
||||
graph: &graphwrapper.graph,
|
||||
};
|
||||
|
||||
let mut algorithm = routequery.algorithm;
|
||||
|
||||
let start = Instant::now();
|
||||
|
||||
let route = if algorithm == "astar-haversine" {
|
||||
println!("running A* with haversine distance");
|
||||
let astar = AStar{graph: &graphwrapper.graph};
|
||||
|
||||
astar.shortest_path(from, to, estimate_haversine)
|
||||
} else if algorithm == "alt" {
|
||||
println!("running ALT");
|
||||
|
||||
let mut best_landmarks = LandmarkBestSet::new(4, &graphwrapper.landmarks);
|
||||
|
||||
let astar = AStar{graph: &graphwrapper.graph};
|
||||
best_landmarks.select_best(from.index as usize, to.index as usize);
|
||||
|
||||
println!("initial estimate: {:?}", best_landmarks.estimate(from.index as NodeId, to.index as NodeId));
|
||||
|
||||
astar.shortest_path(from, to, |src, dest| {
|
||||
best_landmarks.estimate(src.index as NodeId, dest.index as NodeId)
|
||||
})
|
||||
|
||||
|
||||
} else {
|
||||
println!("running dijkstra");
|
||||
algorithm = "dijkstra";
|
||||
graphwrapper.graph.shortest_path(from, to)
|
||||
};
|
||||
|
||||
let time = start.elapsed();
|
||||
|
||||
println!("from: {:?}, to: {:?}", from, to);
|
||||
println!("The query took {} ms", time.as_millis());
|
||||
|
||||
let response = RouteResponse{success: route.is_some(), route, algorithm: algorithm.to_string()};
|
||||
let response = RouteResponse {
|
||||
success: route.is_some(),
|
||||
route,
|
||||
algorithm: algorithm.to_string(),
|
||||
};
|
||||
Ok(Json(response))
|
||||
}
|
||||
|
||||
struct GraphWrapper {
|
||||
graph: GridGraph,
|
||||
landmarks: LandmarkSet,
|
||||
landmarks: LandmarkSet,
|
||||
}
|
||||
|
||||
#[launch]
|
||||
|
|
@ -164,11 +174,15 @@ fn rocket() -> _ {
|
|||
let landmarks: LandmarkSet = bincode::deserialize_from(BufReader::new(landmarks)).unwrap();
|
||||
|
||||
rocket::build()
|
||||
.manage(GraphWrapper{graph: *graph, landmarks})
|
||||
.manage(GraphWrapper {
|
||||
graph: *graph,
|
||||
landmarks,
|
||||
})
|
||||
.mount("/", routes![index])
|
||||
.mount("/", routes![random_route])
|
||||
.mount("/", routes![route])
|
||||
.mount("/static", FileServer::from("static"))
|
||||
.attach(Template::custom(|engines: &mut Engines| {engines.handlebars.set_dev_mode(true);}))
|
||||
|
||||
.attach(Template::custom(|engines: &mut Engines| {
|
||||
engines.handlebars.set_dev_mode(true);
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue