commit b7d1fe3b54f16443424fc027e65116f5d391ade1
parent 64d65a91d924f7530c6ea6e5531c1c52f9b42ee9
Author: Marcel <mtrnord1@gmail.com>
Date: Mon, 13 Jan 2020 17:08:29 +0100
Add another macro
Took 14 minutes
Diffstat:
3 files changed, 31 insertions(+), 28 deletions(-)
diff --git a/src/euroscope_ground.rs b/src/euroscope_ground.rs
@@ -8,7 +8,6 @@ use osmpbfreader::{groups, primitive_block_from_blob};
use crate::types::{Coords, LatLon, Refs};
use crate::types;
-use crate::utils::format_coordinate;
pub(crate) fn generate_ese_ground_taxiway<P: AsRef<Path> + Copy>(source_path: P, airport_name: &str) {
let out_path_string = format!("./out/{}.ese", airport_name);
@@ -37,7 +36,7 @@ pub(crate) fn generate_ese_ground_taxiway<P: AsRef<Path> + Copy>(source_path: P,
//println!("{:?}", ways);
parse_taxiway_nodes!(group, block, ways, nodes, taxiways);
}
- println!("{:?}", taxiways);
+ //println!("{:?}", taxiways);
write_taxiways(out_file, airport_name, taxiways);
out_file.sync_all().expect("unable to sync to file");
@@ -53,15 +52,8 @@ fn write_taxiways(out_file: &mut File, airport_name: &str, taxiways: FnvHashMap<
let local_coords: Vec<&LatLon> = coordinates.iter().clone().collect();
local_coords.iter().for_each(|latlon| {
- let lat_str = format_coordinate(latlon.lat);
-
- let lon_str = format_coordinate(latlon.lon);
-
- let coordinate_string: String;
-
- coordinate_string = format!("COORD:N{}:E{}\n", lat_str, lon_str);
out_file
- .write_all(coordinate_string.as_bytes())
+ .write_all(format_coordinate!(latlon.lat, latlon.lon))
.expect("failed to write file");
});
});
diff --git a/src/macros.rs b/src/macros.rs
@@ -65,3 +65,32 @@ macro_rules! parse_taxiway_nodes {
}
}};
}
+
+macro_rules! format_coordinate_part {
+ ($coordinate:expr) => {{
+ use crate::utils::to_2_digits;
+
+ let degree = $coordinate.trunc() as i64;
+ let minute = (($coordinate - degree as f64).abs() * 60.0).trunc() as i64;
+ let seconds = 3600.0 * ($coordinate - degree as f64).abs() - 60.0 * (minute as f64);
+ let decimal_part_string = format!("{:.2}", seconds - (seconds.trunc() as f64) as f64);
+ let decimal_part_split = decimal_part_string.split('.').collect::<Vec<&str>>();
+ let decimal_part = decimal_part_split.last().unwrap();
+
+ format!(
+ "{:03}.{:02}.{:02}.{:02}",
+ degree,
+ minute,
+ to_2_digits(seconds as i32),
+ decimal_part
+ )
+ }};
+}
+
+macro_rules! format_coordinate {
+ ($lat:expr, $lon:expr) => {{
+ let lat_str = format_coordinate_part!($lat);
+ let lon_str = format_coordinate_part!($lon);
+ format!("COORD:N{}:E{}\n", lat_str, lon_str).as_bytes()
+ }};
+}
diff --git a/src/utils.rs b/src/utils.rs
@@ -4,21 +4,3 @@ pub(crate) fn to_2_digits(n: i32) -> i32 {
let rounded = n / 10f32.powi(digit_offset);
rounded as i32
}
-
-pub(crate) fn format_coordinate(coordinate_raw: f64) -> String {
- let coordinate = coordinate_raw;
- let degree = coordinate.trunc() as i64;
- let minute = ((coordinate - degree as f64).abs() * 60.0).trunc() as i64;
- let seconds = 3600.0 * (coordinate - degree as f64).abs() - 60.0 * (minute as f64);
- let decimal_part_string = format!("{:.2}", seconds - (seconds.trunc() as f64) as f64);
- let decimal_part_split = decimal_part_string.split('.').collect::<Vec<&str>>();
- let decimal_part = decimal_part_split.last().unwrap();
-
- format!(
- "{:03}.{:02}.{:02}.{:02}",
- degree,
- minute,
- to_2_digits(seconds as i32),
- decimal_part
- )
-}