Skip to content

Commit dfc0976

Browse files
committed
[pre] add grid to output, fill grid with node ids [web] add grid to input and graph
1 parent 62f761a commit dfc0976

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

pre/src/main.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use std::path::Path;
1515

1616
// 2^18
1717
const COST_MULTIPLICATOR: usize = 262144;
18+
// First three digits of coordinates are used for the grid hashing
19+
const GRID_MULTIPLICATOR: usize = 100;
1820

1921
#[derive(Serialize, Debug, Clone)]
2022
struct Way {
@@ -36,6 +38,7 @@ struct Output {
3638
nodes: Vec<Node>,
3739
ways: Vec<Way>,
3840
offset: Vec<usize>,
41+
grid: HashMap<(usize, usize), Vec<usize>>
3942
}
4043

4144
fn parse_speed(max_speed: &str, highway: &str) -> usize {
@@ -96,6 +99,8 @@ fn main() {
9699
let mut ways = Vec::<Way>::new();
97100
let mut nodes = Vec::<Node>::new();
98101
let mut offset = Vec::<usize>::new();
102+
// stores node ids for a 2d grid e.g. (1,1) = [1,2,3,..]
103+
let mut grid = HashMap::<(usize, usize), Vec<usize>>::new();
99104

100105
let mut amount_nodes = 0;
101106

@@ -191,9 +196,25 @@ fn main() {
191196
// check if node in osm_id_mapping
192197
match osm_id_mapping.get(&node.id.0) {
193198
Some(our_id) => {
199+
let latitude = node.decimicro_lat as f32 / 10_000_000.0;
200+
let longitude = node.decimicro_lon as f32 / 10_000_000.0;
194201
nodes[*our_id] = Node {
195-
latitude: node.decimicro_lat as f32 / 10000000.0,
196-
longitude: node.decimicro_lon as f32 / 10000000.0,
202+
// https://github.com/rust-lang/rfcs/blob/master/text/1682-field-init-shorthand.md
203+
latitude,
204+
longitude,
205+
};
206+
let lat_grid = (latitude * GRID_MULTIPLICATOR as f32) as i32;
207+
let lng_grid = (longitude * GRID_MULTIPLICATOR as f32) as i32;
208+
let current_grid = grid.get_mut(&(lat_grid as usize, lng_grid as usize));
209+
match current_grid {
210+
Some(id_list) => {
211+
id_list.push(*our_id);
212+
},
213+
None => {
214+
let mut new_id_list = Vec::<usize>::new();
215+
new_id_list.push(*our_id);
216+
grid.insert((lat_grid as usize, lng_grid as usize), new_id_list);
217+
}
197218
}
198219
}
199220
None => continue,
@@ -233,9 +254,10 @@ fn main() {
233254

234255
// serialize everything
235256
let result = Output {
236-
nodes: nodes,
237-
ways: ways,
238-
offset: offset,
257+
nodes,
258+
ways,
259+
offset,
260+
grid
239261
};
240262

241263
let output_file = format!("{}{}", filename.into_string().unwrap(), ".fmi");

web/src/graph.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
// based on https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Rust
22
use std::cmp::Ordering;
3-
use std::collections::BinaryHeap;
3+
use std::collections::{BinaryHeap, HashMap};
44
use std::usize;
55

66
use Node;
77
use Way;
88

99
// 2^18
1010
const COST_MULTIPLICATOR: usize = 262144;
11+
// First three digits of coordinates are used for the grid hashing
12+
const GRID_MULTIPLICATOR: usize = 100;
1113

1214
#[derive(Clone)]
1315
pub struct Graph {
1416
nodes: Vec<Node>,
1517
ways: Vec<Way>,
1618
offset: Vec<usize>,
19+
grid: HashMap<(usize, usize), Vec<usize>>
1720
}
1821

1922
#[derive(Copy, Clone, Eq, PartialEq)]
@@ -34,16 +37,19 @@ impl PartialOrd for State {
3437
}
3538

3639
impl Graph {
37-
pub fn new(nodes: Vec<Node>, ways: Vec<Way>, offset: Vec<usize>) -> Self {
40+
pub fn new(nodes: Vec<Node>, ways: Vec<Way>, offset: Vec<usize>, grid: HashMap<(usize, usize), Vec<usize>>) -> Self {
3841
Graph {
39-
nodes: nodes,
40-
ways: ways,
41-
offset: offset,
42+
nodes,
43+
ways,
44+
offset,
45+
grid
4246
}
4347
}
4448

4549
/// returns closes point of given long & lat
4650
pub fn get_point_id(&self, lat: f32, long: f32) -> usize {
51+
let lat_grid = (lat * GRID_MULTIPLICATOR as f32) as i32;
52+
let lng_grid = (long * GRID_MULTIPLICATOR as f32) as i32;
4753
let mut min_distance: f32 = std::f32::MAX;
4854
let mut min_distance_id: usize = 0;
4955

@@ -75,6 +81,10 @@ impl Graph {
7581
}
7682
}
7783

84+
fn get_adjacent_grids(current_grid: (usize, usize), grid: &HashMap<(usize, usize), Vec<usize>>) {
85+
86+
}
87+
7888
/// executes dijkstra
7989
pub fn find_path(
8090
&self,

web/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::fs::File;
1515
use std::io::BufReader;
1616
use std::path::Path;
1717
use std::time::Instant;
18+
use std::collections::HashMap;
1819

1920
#[derive(Copy, Clone, Deserialize, Debug)]
2021
pub struct Way {
@@ -36,6 +37,7 @@ struct Input {
3637
nodes: Vec<Node>,
3738
ways: Vec<Way>,
3839
offset: Vec<usize>,
40+
grid: HashMap<(usize, usize), Vec<usize>>
3941
}
4042

4143
#[derive(Debug, Deserialize, Serialize)]

0 commit comments

Comments
 (0)