Fast map matching  0.1.0
util.cpp
1 #include "util/util.hpp"
2 
3 #include <iostream>
4 #include <string>
5 #include <sys/stat.h>
6 #include <chrono>
7 #include <vector>
8 #include <ctime>
9 #include "mm/mm_type.hpp"
10 
11 namespace std {
12 
13 std::ostream &operator<<(std::ostream &os,
14  const FMM::MM::Traj_Candidates &tr_cs) {
15  os << "\nCandidate "
16  << std::fixed << std::setw(4) << "step" << ";"
17  << std::fixed << std::setw(6) << "index" << ";"
18  << std::fixed << std::setw(8) << "offset" << ";"
19  << std::fixed << std::setw(8) << "distance" << ";"
20  << std::fixed << std::setw(8) << "edge_id" << '\n';
21  for (auto tr_cs_iter = tr_cs.begin();
22  tr_cs_iter != tr_cs.end(); ++tr_cs_iter) {
23  for (auto p_cs_iter = tr_cs_iter->begin();
24  p_cs_iter != tr_cs_iter->end();
25  ++p_cs_iter) {
26  os << "Candidate "
27  << std::fixed << std::setw(4) << std::distance(tr_cs.begin(),
28  tr_cs_iter) << ";"
29  << std::fixed << std::setw(6) << p_cs_iter->index << ";"
30  << std::fixed << std::setw(8) << p_cs_iter->offset << ";"
31  << std::fixed << std::setw(8) << p_cs_iter->dist << ";"
32  << std::fixed << std::setw(8) << p_cs_iter->edge->id << '\n';
33  }
34  }
35  return os;
36 }
37 
38 std::ostream &operator<<(std::ostream &os,
39  const FMM::MM::OptCandidatePath &opath) {
40  for (int i = 0; i < opath.size(); ++i) {
41  // std::cout <<"Write edge "<< i <<" edge "<< opath[i]->edge->id <<"\n";
42  os << opath[i]->edge->id;
43  if (i != opath.size() - 1)
44  os << ",";
45  }
46  return os;
47 }
48 
49 std::ostream &operator<<(std::ostream &os,
50  const FMM::CORE::Point &geom) {
51  os << std::setprecision(12) << boost::geometry::wkt(geom);
52  return os;
53 }
54 
55 } // namespace std
56 
57 namespace FMM {
58 
59 namespace UTIL {
60 
61 bool file_exists(const char *filename) {
62  struct stat buf;
63  if (stat(filename, &buf) != -1) {
64  return true;
65  }
66  return false;
67 }
68 
69 bool file_exists(const std::string &filename) {
70  return file_exists(filename.c_str());
71 }
72 
73 std::vector<std::string> split_string(const std::string &str) {
74  char delim = ',';
75  std::vector<std::string> result;
76  std::stringstream ss(str);
77  std::string intermediate;
78  while (getline(ss, intermediate, delim)) {
79  result.push_back(intermediate);
80  }
81  return result;
82 }
83 
84 bool string2bool(const std::string &str) {
85  return str == "true" || str == "t" || str == "1";
86 }
87 
88 std::chrono::time_point<std::chrono::system_clock> get_current_time() {
89  return std::chrono::system_clock::now();
90 }
91 
92 // Print a timestamp
94  const std::chrono::time_point<std::chrono::system_clock> &start_time) {
95  std::time_t start_time_t = std::chrono::system_clock::to_time_t(start_time);
96  std::cout << "Time " << std::ctime(&start_time_t) << '\n';
97 }
98 
99 double get_duration(
100  const std::chrono::time_point<std::chrono::system_clock> &start_time,
101  const std::chrono::time_point<std::chrono::system_clock> &end_time) {
102  std::chrono::duration<double> elapsed_seconds = end_time - start_time;
103  return elapsed_seconds.count();
104 }
105 bool check_file_extension(const std::string &filename,
106  const std::string &extension_list_str) {
107  bool result = false;
108  std::stringstream ss;
109  std::string fn_extension = filename.substr(
110  filename.find_last_of('.') + 1);
111  std::vector<std::string> extensions =
112  split_string(extension_list_str);
113  for (const auto &extension:extensions) {
114  if (fn_extension == extension)
115  result = true;
116  }
117  return result;
118 }
119 
120 bool folder_exist(const std::string &folder_name) {
121  if (folder_name.empty()) return true;
122  struct stat sb;
123  if (stat(folder_name.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
124  return true;
125  }
126  return false;
127 }
128 
129 std::string get_file_directory(const std::string &fn) {
130  std::size_t found = fn.find_last_of("/");
131  if (found != std::string::npos) {
132  return fn.substr(0, found);
133  }
134  return {};
135 };
136 
137 } // Util
138 } // FMM
FMM::UTIL::get_current_time
std::chrono::time_point< std::chrono::system_clock > get_current_time()
Get current timestamp.
Definition: util.cpp:88
FMM::MM::Traj_Candidates
std::vector< Point_Candidates > Traj_Candidates
trajectory candidates
Definition: mm_type.hpp:38
FMM::UTIL::split_string
std::vector< std::string > split_string(const std::string &str)
Split a string containing string separated by , into a vector of string.
Definition: util.cpp:73
FMM::UTIL::get_duration
double get_duration(const std::chrono::time_point< std::chrono::system_clock > &start_time, const std::chrono::time_point< std::chrono::system_clock > &end_time)
Calculate the duration between two time points.
Definition: util.cpp:99
FMM::UTIL::check_file_extension
bool check_file_extension(const std::string &filename, const std::string &extension_list_str)
Check if the filename has an extension in the list.
Definition: util.cpp:105
FMM
Fast map matching.
Definition: geom_algorithm.hpp:17
FMM::UTIL::folder_exist
bool folder_exist(const std::string &folder_name)
Check if folder exists or not.
Definition: util.cpp:120
std::operator<<
std::ostream & operator<<(std::ostream &os, const FMM::MM::Traj_Candidates &tr_cs)
Write trajectory candidate to a stream.
Definition: util.cpp:13
FMM::UTIL::get_file_directory
std::string get_file_directory(const std::string &fn)
Get folder path from file path.
Definition: util.cpp:129
FMM::MM::OptCandidatePath
std::vector< const Candidate * > OptCandidatePath
Optimal candidates.
Definition: mm_type.hpp:41
std
Fast map matching.
Definition: util.cpp:11
FMM::UTIL::string2bool
bool string2bool(const std::string &str)
Convert string to bool.
Definition: util.cpp:84
FMM::CORE::Point
boost::geometry::model::point< double, 2, boost::geometry::cs::cartesian > Point
Point class.
Definition: geometry.hpp:28
FMM::UTIL::file_exists
bool file_exists(const char *filename)
Check if file exist or not.
Definition: util.cpp:61
FMM::UTIL::print_time
void print_time(const std::chrono::time_point< std::chrono::system_clock > &start_time)
Print a timestamp.
Definition: util.cpp:93