Fast map matching  0.1.0
mm_writer.cpp
1 
10 #include "io/mm_writer.hpp"
11 #include "util/util.hpp"
12 #include "util/debug.hpp"
13 #include "config/result_config.hpp"
14 
15 #include <sstream>
16 
17 namespace FMM {
18 
19 namespace IO {
20 
22  const std::string &result_file, const CONFIG::OutputConfig &config_arg) :
23  m_fstream(result_file), config_(config_arg) {
24  write_header();
25 }
26 
28  std::string header = "id";
29  if (config_.write_opath) header += ";opath";
30  if (config_.write_error) header += ";error";
31  if (config_.write_offset) header += ";offset";
32  if (config_.write_spdist) header += ";spdist";
33  if (config_.write_pgeom) header += ";pgeom";
34  if (config_.write_cpath) header += ";cpath";
35  if (config_.write_tpath) header += ";tpath";
36  if (config_.write_mgeom) header += ";mgeom";
37  if (config_.write_ep) header += ";ep";
38  if (config_.write_tp) header += ";tp";
39  if (config_.write_length) header += ";length";
40  m_fstream << header << '\n';
41 }
42 
44  std::stringstream buf;
45  buf << result.id;
46  if (config_.write_opath) {
47  buf << ";" << result.opath;
48  }
49  if (config_.write_error) {
50  buf << ";";
51  if (!result.opt_candidate_path.empty()) {
52  int N = result.opt_candidate_path.size();
53  for (int i = 0; i < N - 1; ++i) {
54  buf << result.opt_candidate_path[i].c.dist << ",";
55  }
56  buf << result.opt_candidate_path[N - 1].c.dist;
57  }
58  }
59  if (config_.write_offset) {
60  buf << ";";
61  if (!result.opt_candidate_path.empty()) {
62  int N = result.opt_candidate_path.size();
63  for (int i = 0; i < N - 1; ++i) {
64  buf << result.opt_candidate_path[i].c.offset << ",";
65  }
66  buf << result.opt_candidate_path[N - 1].c.offset;
67  }
68  }
69  if (config_.write_spdist) {
70  buf << ";";
71  if (!result.opt_candidate_path.empty()) {
72  int N = result.opt_candidate_path.size();
73  for (int i = 0; i < N - 1; ++i) {
74  buf << result.opt_candidate_path[i].sp_dist << ",";
75  }
76  buf << result.opt_candidate_path[N - 1].sp_dist;
77  }
78  }
79  if (config_.write_pgeom) {
80  buf << ";";
81  if (!result.opt_candidate_path.empty()) {
82  int N = result.opt_candidate_path.size();
84  for (int i = 0; i < N; ++i) {
85  const FMM::CORE::Point &point = result.opt_candidate_path[i].c.point;
86  pline.add_point(point);
87  }
88  buf << pline;
89  }
90  }
91  // Write fields related with cpath
92  if (config_.write_cpath) {
93  buf << ";" << result.cpath;
94  }
95  if (config_.write_tpath) {
96  buf << ";";
97  if (!result.cpath.empty()) {
98  // Iterate through consecutive indexes and write the traversed path
99  int J = result.indices.size();
100  for (int j = 0; j < J - 1; ++j) {
101  int a = result.indices[j];
102  int b = result.indices[j + 1];
103  for (int i = a; i < b; ++i) {
104  buf << result.cpath[i];
105  buf << ",";
106  }
107  buf << result.cpath[b];
108  if (j < J - 2) {
109  // Last element should not have a bar
110  buf << "|";
111  }
112  }
113  }
114  }
115  if (config_.write_mgeom) {
116  buf << ";" << result.mgeom;
117  }
118  if (config_.write_ep) {
119  buf << ";";
120  if (!result.opt_candidate_path.empty()) {
121  int N = result.opt_candidate_path.size();
122  for (int i = 0; i < N - 1; ++i) {
123  buf << result.opt_candidate_path[i].ep << ",";
124  }
125  buf << result.opt_candidate_path[N - 1].ep;
126  }
127  }
128  if (config_.write_tp) {
129  buf << ";";
130  if (!result.opt_candidate_path.empty()) {
131  int N = result.opt_candidate_path.size();
132  for (int i = 0; i < N - 1; ++i) {
133  buf << result.opt_candidate_path[i].tp << ",";
134  }
135  buf << result.opt_candidate_path[N - 1].tp;
136  }
137  }
138  if (config_.write_length) {
139  buf << ";";
140  if (!result.opt_candidate_path.empty()) {
141  int N = result.opt_candidate_path.size();
142  SPDLOG_TRACE("Write length {}",N);
143  for (int i = 0; i < N - 1; ++i) {
144  // SPDLOG_TRACE("Write length {}",i);
145  buf << result.opt_candidate_path[i].c.edge->length << ",";
146  }
147  // SPDLOG_TRACE("Write length {}",N-1);
148  buf << result.opt_candidate_path[N - 1].c.edge->length;
149  }
150  }
151  buf << '\n';
152  // Ensure that fstream is called corrected in OpenMP
153  #pragma omp critical
154  m_fstream << buf.rdbuf();
155 }
156 
157 } //IO
158 } //MM
FMM::CONFIG::OutputConfig::write_tpath
bool write_tpath
if true, tpath (the path traversed between each two consecutive observations) will be exported
Definition: result_config.hpp:34
FMM::CONFIG::OutputConfig::write_ep
bool write_ep
if true, ep (emission proability of each point) will be exported
Definition: result_config.hpp:44
FMM::CORE::LineString::add_point
void add_point(double x, double y)
Add a point to the end of the current line.
Definition: geometry.hpp:79
FMM::IO::CSVMatchResultWriter::write_result
void write_result(const FMM::MM::MatchResult &result)
Write match result.
Definition: mm_writer.cpp:43
FMM::CONFIG::OutputConfig::write_spdist
bool write_spdist
if true, spdist (the distance traversed between each two consecutive points) will be exported
Definition: result_config.hpp:39
FMM::CONFIG::OutputConfig::write_tp
bool write_tp
if true, tp (transition probability) will be exported
Definition: result_config.hpp:46
FMM::MM::MatchResult
Map matched result representation.
Definition: mm_type.hpp:69
FMM::IO::CSVMatchResultWriter::write_header
void write_header()
Write a header line for the fields exported.
Definition: mm_writer.cpp:27
FMM::CONFIG::OutputConfig::write_length
bool write_length
if true, length (length of each matched edge) will be exported
Definition: result_config.hpp:48
FMM::MM::MatchResult::cpath
C_Path cpath
the complete path, containing ids of a sequence of topologically connected edges traversed by the tra...
Definition: mm_type.hpp:77
FMM
Fast map matching.
Definition: geom_algorithm.hpp:17
FMM::MM::MatchResult::mgeom
CORE::LineString mgeom
the geometry of the matched path
Definition: mm_type.hpp:81
FMM::MM::MatchResult::id
int id
id of the trajectory to be matched
Definition: mm_type.hpp:70
FMM::CONFIG::OutputConfig::write_mgeom
bool write_mgeom
if true, mgeom (the geometry of the matched path) will be exported
Definition: result_config.hpp:37
FMM::CONFIG::OutputConfig::write_cpath
bool write_cpath
if true, cpath (a list of edge ID representing the matched path) will be exported
Definition: result_config.hpp:32
FMM::CORE::LineString
Linestring geometry class.
Definition: geometry.hpp:34
FMM::MM::MatchResult::opt_candidate_path
MatchedCandidatePath opt_candidate_path
A vector of candidate matched to each point of a trajectory.
Definition: mm_type.hpp:71
FMM::MM::MatchResult::opath
O_Path opath
the optimal path, containing id of edges matched to each point in a trajectory
Definition: mm_type.hpp:74
FMM::CONFIG::OutputConfig::write_opath
bool write_opath
if true, opath (edge id matched for each point) will be exported
Definition: result_config.hpp:26
FMM::MM::MatchResult::indices
std::vector< int > indices
index of opath edge in cpath
Definition: mm_type.hpp:80
FMM::CONFIG::OutputConfig::write_error
bool write_error
if true, gps error (distance from GPS point to the matched point) will be exported
Definition: result_config.hpp:30
FMM::IO::CSVMatchResultWriter::CSVMatchResultWriter
CSVMatchResultWriter(const std::string &result_file, const CONFIG::OutputConfig &config_arg)
Constructor.
Definition: mm_writer.cpp:21
FMM::CONFIG::OutputConfig
Output configuration class defining the fields exported for map matching.
Definition: result_config.hpp:25
FMM::CORE::Point
boost::geometry::model::point< double, 2, boost::geometry::cs::cartesian > Point
Point class.
Definition: geometry.hpp:28
FMM::CONFIG::OutputConfig::write_pgeom
bool write_pgeom
if true, pgeom (a linestring connecting the matched point) will be exported
Definition: result_config.hpp:42
FMM::CONFIG::OutputConfig::write_offset
bool write_offset
if true, offset (distance to the start point of a matched edge) will be exported
Definition: result_config.hpp:28