osm-to-euroscope

A tool to generate Radar Screens from OSM Data
git clone git://archive.git.mtrnord.blog/MTRNord/osm-to-euroscope.git
Log | Files | Refs | README | LICENSE

commit 64d65a91d924f7530c6ea6e5531c1c52f9b42ee9
parent 9734fe5f516e11f26982a5935f5464dcd0ea3044
Author: Marcel <mtrnord1@gmail.com>
Date:   Mon, 13 Jan 2020 15:41:47 +0100

Switch to osmpbfreader
Add some macros

Took 1 hour 54 minutes

Diffstat:
MCargo.lock | 243++++++++++++++++++++++++++++++++++++++++---------------------------------------
MCargo.toml | 2+-
Msrc/euroscope_ground.rs | 99++++++++++++++++++-------------------------------------------------------------
Asrc/macros.rs | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main.rs | 2++
Msrc/types.rs | 1+
6 files changed, 215 insertions(+), 199 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -6,7 +6,7 @@ version = "0.1.0" dependencies = [ "clap", "fnv", - "osmpbf", + "osmpbfreader", ] [[package]] @@ -35,12 +35,6 @@ dependencies = [ ] [[package]] -name = "autocfg" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" - -[[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -83,57 +77,16 @@ dependencies = [ ] [[package]] -name = "crossbeam-deque" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3aa945d63861bfe624b55d153a39684da1e8c0bc8fba932f7ee3a3c16cea3ca" -dependencies = [ - "crossbeam-epoch", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5064ebdbf05ce3cb95e45c8b086f72263f4166b29b97f6baff7ef7fe047b55ac" -dependencies = [ - "autocfg", - "cfg-if", - "crossbeam-utils", - "lazy_static", - "memoffset", - "scopeguard", -] - -[[package]] -name = "crossbeam-queue" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c695eeca1e7173472a32221542ae469b3e9aac3a4fc81f7696bcad82029493db" -dependencies = [ - "cfg-if", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.7.0" +name = "flat_map" +version = "0.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce446db02cdc3165b94ae73111e570793400d0794e46125cc4056c81cbb039f4" +checksum = "1e755d8e793606696d7175b6a823db01a1e16d67ff1bed82c59a817084fd8878" dependencies = [ - "autocfg", - "cfg-if", - "lazy_static", + "serde", + "serde_derive", ] [[package]] -name = "either" -version = "1.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" - -[[package]] name = "flate2" version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -152,53 +105,35 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" [[package]] -name = "hermit-abi" -version = "0.1.5" +name = "futures" +version = "0.1.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f629dc602392d3ec14bfc8a09b5e644d7ffd725102b48b81e59f90f2633621d7" -dependencies = [ - "libc", -] +checksum = "1b980f2816d6ee8673b6517b52cb0e808a180efc92e5c19d02cdda79066703ef" [[package]] -name = "inflate" -version = "0.4.5" +name = "futures-cpupool" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff" +checksum = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" dependencies = [ - "adler32", + "futures", + "num_cpus", ] [[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - -[[package]] -name = "libc" -version = "0.2.66" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" - -[[package]] -name = "memmap" -version = "0.7.0" +name = "hermit-abi" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6585fd95e7bb50d6cc31e20d4cf9afb4e2ba16c5846fc76793f11218da9c475b" +checksum = "f629dc602392d3ec14bfc8a09b5e644d7ffd725102b48b81e59f90f2633621d7" dependencies = [ "libc", - "winapi", ] [[package]] -name = "memoffset" -version = "0.5.3" +name = "libc" +version = "0.2.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75189eb85871ea5c2e2c15abbdd541185f63b408415e5051f5cac122d8c774b9" -dependencies = [ - "rustc_version", -] +checksum = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" [[package]] name = "miniz_oxide" @@ -220,78 +155,127 @@ dependencies = [ ] [[package]] -name = "osmpbf" -version = "0.1.13" +name = "osmpbfreader" +version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52f8eed4edc2f1c5e720ff5cea4d872c88cfb81115096c3999f4a75e2d3c272a" +checksum = "c5df2225556337461734e5109b4d4ea9f94410fa7e15641a45c40372512f4a1c" dependencies = [ "byteorder", + "flat_map", "flate2", - "inflate", - "memmap", + "par-map", "protobuf", - "rayon", + "protobuf-codegen-pure", + "pub-iterator-type", + "rental", + "serde", + "serde_derive", +] + +[[package]] +name = "par-map" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f05b290991702bb8140cf70915b82b0ae1ec7fe478db97305af990048040095" +dependencies = [ + "futures", + "futures-cpupool", + "num_cpus", + "pub-iterator-type", +] + +[[package]] +name = "proc-macro2" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0319972dcae462681daf4da1adeeaa066e3ebd29c69be96c6abb1259d2ee2bcc" +dependencies = [ + "unicode-xid", ] [[package]] name = "protobuf" -version = "2.8.1" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40361836defdd5871ff7e84096c6f6444af7fc157f8ef1789f54f147687caa20" +checksum = "6686ddd96a8dbe2687b5f2a687b2cfb520854010ec480f2d74c32e7c9873d3c5" [[package]] -name = "rayon" -version = "1.3.0" +name = "protobuf-codegen" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6ce3297f9c85e16621bb8cca38a06779ffc31bb8184e1be4bed2be4678a098" +checksum = "6456421eecf7fc72905868cd760c3e35848ded3552e480cfe67726ed4dbd8d23" dependencies = [ - "crossbeam-deque", - "either", - "rayon-core", + "protobuf", ] [[package]] -name = "rayon-core" -version = "1.7.0" +name = "protobuf-codegen-pure" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08a89b46efaf957e52b18062fb2f4660f8b8a4dde1807ca002690868ef2c85a9" +checksum = "4a7cb42d5ab6073333be90208ab5ea6ab41c8f6803b35fd773a7572624cc15c9" dependencies = [ - "crossbeam-deque", - "crossbeam-queue", - "crossbeam-utils", - "lazy_static", - "num_cpus", + "protobuf", + "protobuf-codegen", ] [[package]] -name = "rustc_version" -version = "0.2.3" +name = "pub-iterator-type" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +checksum = "858afdbecdce657c6e32031348cf7326da7700c869c368a136d31565972f7018" + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rental" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8545debe98b2b139fb04cad8618b530e9b07c152d99a5de83c860b877d67847f" +dependencies = [ + "rental-impl", + "stable_deref_trait", +] + +[[package]] +name = "rental-impl" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "475e68978dc5b743f2f40d8e0a8fdc83f1c5e78cbf4b8fa5e74e73beebc340de" dependencies = [ - "semver", + "proc-macro2", + "quote", + "syn", ] [[package]] -name = "scopeguard" -version = "1.0.0" +name = "serde" +version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b42e15e59b18a828bbf5c58ea01debb36b9b096346de35d941dcb89009f24a0d" +checksum = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449" [[package]] -name = "semver" -version = "0.9.0" +name = "serde_derive" +version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +checksum = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64" dependencies = [ - "semver-parser", + "proc-macro2", + "quote", + "syn", ] [[package]] -name = "semver-parser" -version = "0.7.0" +name = "stable_deref_trait" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +checksum = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" [[package]] name = "strsim" @@ -300,6 +284,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] +name = "syn" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e4ff033220a41d1a57d8125eab57bf5263783dfdcc18688b1dacc6ce9651ef8" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] name = "textwrap" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -315,6 +310,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" [[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" + +[[package]] name = "vec_map" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml @@ -7,6 +7,6 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -osmpbf = "0.1" +osmpbfreader = "0.13.3" fnv = "1.0.6" clap = "2.31.2" diff --git a/src/euroscope_ground.rs b/src/euroscope_ground.rs @@ -1,10 +1,10 @@ use std::fs; use std::fs::File; -use std::io::Write; +use std::io::{BufReader, Write}; use std::path::Path; use fnv::FnvHashMap; -use osmpbf::*; +use osmpbfreader::{groups, primitive_block_from_blob}; use crate::types::{Coords, LatLon, Refs}; use crate::types; @@ -21,12 +21,27 @@ pub(crate) fn generate_ese_ground_taxiway<P: AsRef<Path> + Copy>(source_path: P, out_file .write_all(b"[GROUND]\n") .expect("failed to write file"); - let taxiways = get_taxiways(source_path); - let coords = get_taxiways_coords(source_path, taxiways); - write_taxiways(out_file, airport_name, coords); - - out_file.sync_all().expect("unable to sync to file"); + let file = File::open(source_path).expect("unable to open file"); + let buf_reader = BufReader::new(file); + let mut pbf = osmpbfreader::OsmPbfReader::new(buf_reader); + + for block in pbf.blobs().map(|b| primitive_block_from_blob(&b.unwrap())) { + let block: osmpbfreader::osmformat::PrimitiveBlock = block.unwrap(); + + let mut nodes: Vec<types::Node> = Vec::new(); + let mut ways: FnvHashMap<String, Refs> = FnvHashMap::default(); + let mut taxiways: FnvHashMap<String, Coords> = FnvHashMap::default(); + for group in block.get_primitivegroup().iter() { + parse_taxiway!(group, block, ways); + //println!("{:?}", ways); + parse_taxiway_nodes!(group, block, ways, nodes, taxiways); + } + println!("{:?}", taxiways); + write_taxiways(out_file, airport_name, taxiways); + + out_file.sync_all().expect("unable to sync to file"); + } } fn write_taxiways(out_file: &mut File, airport_name: &str, taxiways: FnvHashMap<String, Coords>) { @@ -51,73 +66,3 @@ fn write_taxiways(out_file: &mut File, airport_name: &str, taxiways: FnvHashMap< }); }); } - -fn get_taxiways_coords<P: AsRef<Path> + Copy>( - path: P, - refs: FnvHashMap<String, Refs>, -) -> FnvHashMap<String, Coords> { - let mut taxiways: FnvHashMap<String, Coords> = FnvHashMap::default(); - let mut nodes: Vec<types::Node> = Vec::new(); - - let reader = ElementReader::from_path(path).unwrap(); - reader - .for_each(|element| { - if let Element::DenseNode(node) = element { - let new_node = types::Node { - id: node.id, - lat: node.lat(), - lon: node.lon(), - }; - nodes.push(new_node); - } - }) - .unwrap(); - - for (key, val) in refs.iter() { - val.iter().for_each(|refval| { - if let Some(node) = nodes.iter().find(|x| x.id == *refval) { - let lat_lon = LatLon { - lat: node.lat, - lon: node.lon, - }; - - if taxiways.contains_key(key.as_str()) { - (*(taxiways.get_mut(key.as_str()).unwrap())).push(lat_lon); - } else { - let mut coords: Coords = Vec::new(); - coords.push(lat_lon); - taxiways.insert(key.clone(), coords); - } - } - }) - } - - taxiways -} - -fn get_taxiways<P: AsRef<Path> + Copy>(path: P) -> FnvHashMap<String, Refs> { - let mut taxiways: FnvHashMap<String, Refs> = FnvHashMap::default(); - - let reader = ElementReader::from_path(path).unwrap(); - reader - .for_each(|element| { - if let Element::Way(way) = element { - if way - .tags() - .any(|(key, value)| key == "aeroway" && value == "taxiway") - { - if let Some((_, value)) = way.tags().find(|(key, _)| *key == "ref") { - let mut new_vals = way.refs().collect::<Vec<i64>>(); - - if taxiways.contains_key(value) { - taxiways.get_mut(value).unwrap().append(&mut new_vals); - } else { - taxiways.insert(value.to_string(), new_vals); - } - } - } - } - }) - .unwrap(); - taxiways -} diff --git a/src/macros.rs b/src/macros.rs @@ -0,0 +1,67 @@ +macro_rules! parse_taxiway { + ($group:expr, $block:expr, $taxiways:expr) => {{ + use osmpbfreader::groups; + + for way in groups::ways(&$group, &$block) { + if way.tags.contains("aeroway", "taxiway") { + if way.tags.contains_key("ref") { + let value = way.tags.get("ref").unwrap(); + let mut nodes: Vec<i64> = way.nodes.iter().map(|x| { + x.0 + }).collect(); + + if $taxiways.contains_key(value) { + $taxiways.get_mut(value).unwrap().append(&mut nodes); + } else { + $taxiways.insert(value.to_string(), nodes); + } + } + } + } + }}; +} + +macro_rules! parse_taxiway_nodes { + ($group:expr, $block:expr, $needed_nodes:expr, $nodes:expr, $taxiways:expr) => {{ + use crate::types::LatLon; + + + for node in groups::simple_nodes(&$group, &$block) { + let new_node = types::Node { + id: node.id.0, + lat: node.lat(), + lon: node.lon(), + }; + $nodes.push(new_node); + } + for node in groups::dense_nodes(&$group, &$block) { + let new_node = types::Node { + id: node.id.0, + lat: node.lat(), + lon: node.lon(), + }; + $nodes.push(new_node); + } + + for (key, val) in $needed_nodes.iter() { + val.iter().for_each(|&refval| { + if let Some(node) = $nodes.iter().find(|&x| {x.id == refval}) { + let lat_lon = LatLon { + lat: node.lat, + lon: node.lon, + }; + + if $taxiways.contains_key(key.as_str()) { + (*($taxiways.get_mut(key.as_str()).unwrap())).push(lat_lon); + } else { + let mut coords: Coords = Vec::new(); + coords.push(lat_lon); + $taxiways.insert(key.clone(), coords); + } + } + + }); + + } + }}; +} diff --git a/src/main.rs b/src/main.rs @@ -11,6 +11,8 @@ use crate::euroscope_ground::generate_ese_ground_taxiway; mod types; mod utils; +#[macro_use] +mod macros; mod euroscope_ground; fn main() { diff --git a/src/types.rs b/src/types.rs @@ -4,6 +4,7 @@ pub(crate) struct LatLon { pub(crate) lon: f64, } +#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)] pub(crate) struct Node { pub(crate) id: i64, pub(crate) lat: f64,