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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::{
configs::{parsing::nodes, writing},
defaults,
helpers::err,
io::writing::network::write_edges_to_file,
network::Graph,
};
use log::info;
use progressing::{self, bernoulli::Bar as BernoulliBar, Baring};
use std::{
fs::OpenOptions,
io::{BufWriter, Write},
};
pub struct Writer;
impl Writer {
pub fn new() -> Writer {
Writer {}
}
}
impl Writer {
pub fn write(
&self,
graph: &Graph,
writing_cfg: &writing::network::graph::Config,
) -> err::Feedback {
// prepare
let output_file = OpenOptions::new()
.write(true)
.create_new(true)
.open(&writing_cfg.map_file)?;
let mut writer = BufWriter::new(output_file);
let fwd_edges = graph.fwd_edges();
let nodes = graph.nodes();
// write header
writeln!(writer, "# edge-metric-count")?;
writeln!(writer, "# node-count")?;
writeln!(writer, "# edge-count")?;
writeln!(
writer,
"# nodes: {:?}",
writing_cfg
.nodes
.ids
.iter()
.map(|id| match id {
Some(id) => format!("{}", id.0),
None => format!("{}", defaults::writing::IGNORE_STR),
})
.collect::<Vec<_>>()
)?;
writeln!(
writer,
"# edges: {:?}",
writing_cfg
.edges
.ids
.iter()
.map(|id| match id {
Some(id) => format!("{}", id.0),
None => format!("{}", defaults::writing::IGNORE_STR),
})
.collect::<Vec<_>>()
)?;
writeln!(
writer,
"# Edges' metrics are {} by their mean.",
if !graph.cfg().edges.metrics.are_normalized || writing_cfg.edges.is_denormalizing {
"not normalized"
} else {
"normalized"
}
)?;
writeln!(writer, "")?;
// write counts
let dim = writing_cfg
.edges
.ids
.iter()
.filter_map(|id| id.as_ref())
.filter(|id| graph.cfg().edges.metrics.ids.contains(id))
.count();
writeln!(writer, "{}", dim)?;
writeln!(writer, "{}", nodes.count())?;
// only write non-shortcuts
writeln!(
writer,
"{}",
fwd_edges
.iter()
.filter(|&edge_idx| !fwd_edges.is_shortcut(edge_idx)
|| writing_cfg.edges.is_writing_shortcuts)
.count()
)?;
// write nodes
let mut progress_bar = BernoulliBar::with_goal(nodes.count()).timed();
info!("{}", progress_bar);
// for every node
for node_idx in &nodes {
// loop over graphs config
// and print respective data
// if id fits
// for every writing-cfg-item
for (i, next_id) in writing_cfg.nodes.ids.iter().enumerate() {
if let Some(next_id) = next_id {
let mut has_been_written = false;
// Now: Ignore-case is already covered
// look for category of same id
// and write data to file
for category in graph.cfg().nodes.categories.iter() {
match category {
nodes::Category::Meta { info, id } => {
if id != next_id {
continue;
}
let node = graph.nodes().create(node_idx);
match info {
nodes::MetaInfo::NodeId => write!(writer, "{}", node.id())?,
nodes::MetaInfo::NodeIdx => write!(writer, "{}", node.idx())?,
nodes::MetaInfo::CHLevel => {
write!(writer, "{}", node.ch_level())?
}
}
}
nodes::Category::Metric { unit, id } => {
if id != next_id {
continue;
}
let node = graph.nodes().create(node_idx);
match unit {
nodes::metrics::UnitInfo::Latitude => {
write!(writer, "{}", node.coord().lat)?
}
nodes::metrics::UnitInfo::Longitude => {
write!(writer, "{}", node.coord().lon)?
}
nodes::metrics::UnitInfo::Height => {
unimplemented!("Nodes' height is not supported yet.")
}
}
}
nodes::Category::Ignored => continue, // covered in else-case
}
// When here, no 'continue' has been called
// so sth has been written.
has_been_written = true;
break;
}
// if nothing has been written
// -> id is not in config
if !has_been_written {
return Err(format!(
"Writing-config has id {} which is not part of graph's node-data.",
next_id
)
.into());
}
} else {
// if id is None
// -> ignore column
write!(writer, "{}", defaults::writing::IGNORE_STR)?;
}
// Sth has been written, so
// write space if needed
if i < writing_cfg.nodes.ids.len() - 1 {
write!(writer, " ")?;
}
}
// write end of line
writeln!(writer, "")?;
// print progress
progress_bar.add(true);
if progress_bar.has_progressed_significantly() {
progress_bar.remember_significant_progress();
info!("{}", progress_bar);
}
}
// write edges
write_edges_to_file(
&mut writer,
&graph,
&writing::network::edges::Config::from(writing_cfg.clone()),
)?;
info!("FINISHED");
Ok(())
}
}