-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_closest_points.rs
74 lines (60 loc) · 2.47 KB
/
find_closest_points.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/// # find_closest_points.rs
/// This file contains functions for file manipulation.
///
/// ## Author
/// Tom Planche - <github.com/tomPlanche>
// IMPORTS =================================================================================================== IMPORTS
#[path = "gpx_utils.rs"]
mod gpx_utils;
#[path = "file_utils.rs"]
mod file_utils;
use std::path::PathBuf;
use crate::file_utils::{file_name_to_path_buf, read_gpx_file};
use crate::gpx_utils::{calc_distance, Coord};
// END IMPORTS ========================================================================================== END IMPORTS
// VARIABLES ================================================================================================ VARIABLE
// Type(s)
// Other(s)
// END VARIABLES ======================================================================================= END VARIABLES
// CODE ========================================================================================================= CODE
///
/// # find_closests_points
/// Find the n closest points to a given point in a given file.
///
/// ## Arguments
/// * `file_name` - The name of the file to read from.
/// * `point` - The point to compare to.
/// * `nb_points` - The number of points to return.
///
/// ## Returns
/// * `Vec<Coord>` - A vector of the n closest points.
fn find_closests_points(file_name: &str, point: Coord, nb_points: usize) -> Vec<Coord> {
let path_buff_from_file_1: PathBuf = file_name_to_path_buf(file_name);
let coords: Vec<Coord> = match read_gpx_file(&path_buff_from_file_1) {
Some(coords) => coords,
None => panic!("Could not read the file {:?}", file_name),
};
let mut indexes_distance: Vec<(usize, f64)> = coords
.iter()
.enumerate()
.map(|(i, coord)| (i, calc_distance(point, *coord, Some(true))))
.collect();
indexes_distance
.sort_by(|(_, dist_1), (_, dist_2)| dist_1.partial_cmp(dist_2).unwrap());
if indexes_distance[0].1 == 0.0 {
indexes_distance.remove(0);
}
indexes_distance
.iter()
.take(nb_points)
.map(|(i, _)| coords[*i])
.collect()
}
fn main() {
println!("{:?}", find_closests_points("puertoviejofenars.gpx", Coord{lat: 42.6782078, lon: 0.0856054}, 2));
todo!("Determinate the binary arguments and implement the function");
}
// END CODE ======================================================================================= END COMPONENT
//
// * End of file /find_closest_points.rs
//