Skip to content

Commit 625d613

Browse files
committed
[web] make use of travel_type
1 parent 012f2eb commit 625d613

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

pre/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct Way {
2424
target: usize,
2525
speed: usize,
2626
distance: usize,
27-
kind: usize,
27+
travel_type: usize,
2828
}
2929

3030
#[derive(Serialize, Debug, Clone)]
@@ -166,8 +166,8 @@ fn main() {
166166
for way in groups::ways(&group, &block) {
167167
if way.tags.contains_key("highway") {
168168
let highway = way.tags.get("highway").unwrap().trim();
169-
let kind = get_street_kind(highway);
170-
if kind == 100 {
169+
let travel_type = get_street_kind(highway);
170+
if travel_type == 100 {
171171
continue;
172172
}
173173
let mut max_speed: &str = "";
@@ -201,7 +201,7 @@ fn main() {
201201
target: id,
202202
speed: speed,
203203
distance: 0,
204-
kind: kind,
204+
travel_type: travel_type,
205205
});
206206
prev_id = id;
207207
}

web/src/graph.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ impl Graph {
5252
}
5353

5454
/// returns closes point of given long & lat
55-
pub fn get_point_id(&self, lat: f32, long: f32) -> usize {
55+
pub fn get_point_id(&self, lat: f32, long: f32, travel_type: usize) -> usize {
56+
// TODO check if travel_type can be used
5657
let mut min_distance: f32 = std::f32::MAX;
5758
let mut min_distance_id: usize = 0;
5859
let adjacent_nodes = self.get_adjacent_node_ids(lat, long);
@@ -77,13 +78,14 @@ impl Graph {
7778
}
7879

7980
/// returns the edge weight from source to target
80-
fn get_edge_weight(&self, way: Way, use_distance: bool) -> usize {
81+
fn get_edge_weight(&self, way: Way, travel_type: usize, use_distance: bool) -> usize {
8182
if use_distance {
8283
return way.distance;
8384
} else {
8485
if way.speed == 0 {
8586
return way.distance;
8687
}
88+
// TODO fix speed with correct travel_type
8789
return way.distance / way.speed;
8890
}
8991
}
@@ -128,7 +130,7 @@ impl Graph {
128130
&self,
129131
start: usize,
130132
end: usize,
131-
is_car: bool,
133+
travel_type: usize,
132134
use_distance: bool,
133135
) -> Option<(Vec<usize>, f32)> {
134136
let mut dist = vec![(usize::MAX, None); self.nodes.len()];
@@ -152,17 +154,33 @@ impl Graph {
152154
path.reverse();
153155
return Some((path, cost as f32 / COST_MULTIPLICATOR as f32));
154156
}
155-
156157
if cost > dist[node].0 {
157158
continue;
158159
}
159160
for edge in self.offset[node]..self.offset[node + 1] {
160161
let current_way: Way = self.ways[edge];
161-
// TODO check if should skip due to is_car
162+
// skip way, if the type does not match
163+
match travel_type {
164+
0 => match current_way.travel_type {
165+
0 | 1 | 5 => (),
166+
_ => continue,
167+
},
168+
1 => match current_way.travel_type {
169+
1 | 2 | 3 | 5 => (),
170+
_ => continue,
171+
},
172+
2 => match current_way.travel_type {
173+
3 | 4 | 5 => (),
174+
_ => continue,
175+
},
176+
_ => unreachable!(),
177+
}
178+
// calculate costs
162179
let next = State {
163180
node: current_way.target,
164-
cost: cost + self.get_edge_weight(current_way, use_distance),
181+
cost: cost + self.get_edge_weight(current_way, travel_type, use_distance),
165182
};
183+
// add way to heap
166184
if next.cost < dist[next.node].0 {
167185
dist[next.node] = (next.cost, Some(node));
168186
heap.push(next);

web/src/main.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct Way {
2323
target: usize,
2424
speed: usize,
2525
distance: usize,
26-
kind: usize,
26+
travel_type: usize,
2727
}
2828

2929
#[derive(Copy, Clone, Deserialize, Serialize, Debug)]
@@ -44,7 +44,7 @@ struct Input {
4444
struct Query {
4545
start: Node,
4646
end: Node,
47-
use_car: bool,
47+
travel_type: String,
4848
by_distance: bool,
4949
}
5050

@@ -59,18 +59,23 @@ fn query(request: web::Json<Query>, dijkstra: web::Data<Graph>) -> web::Json<Res
5959
// extract points
6060
let start: &Node = &request.start;
6161
let end: &Node = &request.end;
62-
let use_car: bool = request.use_car;
62+
let travel_type = match request.travel_type.as_ref() {
63+
"car" => 0,
64+
"bicycle" => 1,
65+
"foot" => 2,
66+
_ => 0,
67+
};
6368
let by_distance: bool = request.by_distance;
6469
// println!("Start: {},{}", start.latitude, start.longitude);
6570
// println!("End: {},{}", end.latitude, end.longitude);
66-
// println!("use_car: {}, by_distance: {}", use_car, by_distance);
71+
// println!("travel_type: {}, by_distance: {}", travel_type, by_distance);
6772

6873
// search for clicked points
69-
let start_id: usize = dijkstra.get_point_id(start.latitude, start.longitude);
70-
let end_id: usize = dijkstra.get_point_id(end.latitude, end.longitude);
74+
let start_id: usize = dijkstra.get_point_id(start.latitude, start.longitude, travel_type);
75+
let end_id: usize = dijkstra.get_point_id(end.latitude, end.longitude, travel_type);
7176

7277
let timing = Instant::now();
73-
let tmp = dijkstra.find_path(start_id, end_id, use_car, by_distance);
78+
let tmp = dijkstra.find_path(start_id, end_id, travel_type, by_distance);
7479
println!("### duration for find_path(): {:?}", timing.elapsed());
7580

7681
let result: Vec<Node>;

web/static/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ <h4>Travel by</h4>
3030
</label-big>
3131
<select id="travel-type">
3232
<option value="car">car</option>
33+
<option value="bicycle">bicycle</option>
3334
<option value="foot">foot</option>
3435
</select>
3536
</div>

web/static/leaflet_script.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ function setEnd() {
7070
}
7171

7272
function query() {
73+
hide_result();
7374
hide_invalid_request();
7475
hide_no_path_found();
7576
hide_select_start_and_end();
@@ -101,7 +102,7 @@ function query() {
101102
}
102103
};
103104

104-
var travel_type = document.getElementById("travel-type").value == "car";
105+
var travel_type = document.getElementById("travel-type").value;
105106
var optimization = document.getElementById("optimization").value == "distance";
106107
var body = {
107108
"start": {
@@ -112,7 +113,7 @@ function query() {
112113
"latitude": endPoint.lat,
113114
"longitude": endPoint.lng
114115
},
115-
"use_car": travel_type,
116+
"travel_type": travel_type,
116117
"by_distance": optimization,
117118
};
118119
var data = JSON.stringify(body);

0 commit comments

Comments
 (0)