-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOsmLogger.hpp
91 lines (76 loc) · 2.7 KB
/
OsmLogger.hpp
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
#pragma once
#include "Eigen/Core"
#include "pugixml.hpp"
#include <vector>
#include <iostream>
#include <map>
class OsmLogger
{
using IdType = std::size_t;
public:
static OsmLogger &instance()
{
static OsmLogger instance;
return instance;
}
OsmLogger(const OsmLogger &) = delete;
void operator=(const OsmLogger &) = delete;
void dump(const std::string &filename)
{
osm_xml.save_file(filename.c_str());
}
/*containers of points*/
template <typename C, typename T = typename C::value_type, typename std::enable_if_t<!std::is_arithmetic<T>::value> * = nullptr>
void log(const C &container)
{
std::vector<IdType> osm_node_ids;
for (const T &val : container)
osm_node_ids.push_back(log(val));
add_way(osm_node_ids.data(), osm_node_ids.size());
}
/*point containers*/
template <typename T, std::size_t S, typename std::enable_if_t<std::is_arithmetic<T>::value> * = nullptr>
IdType log(const std::array<T, S> &arr)
{
static_assert(S > 1, "array size must be > 1");
return log(arr.data());
}
template <typename P, typename T = typename P::value_type, typename std::enable_if_t<std::is_arithmetic<T>::value> * = nullptr>
IdType log(const P &pt)
{
assert(pt.size() > 1);
return log(pt.data());
}
template <typename T, int S, typename std::enable_if_t<std::is_arithmetic<T>::value> * = nullptr>
IdType log(const Eigen::Matrix<T, S, 1> &vec)
{
static_assert(S > 1, "vector size must be > 1");
return log(vec.data());
}
pugi::xml_document osm_xml;
private:
OsmLogger()
{
osm_xml.append_child("osm").append_attribute("version") = "0.6";
}
template <typename T, typename std::enable_if_t<std::is_arithmetic<T>::value> * = nullptr>
IdType log(const T *const data)
{
pugi::xml_node osm_node = osm_xml.child("osm").append_child("node");
osm_node.append_attribute("id") = std::to_string(cur_id).c_str();
osm_node.append_attribute("version") = 1;
osm_node.append_attribute("lat") = std::to_string(data[0]).c_str();
osm_node.append_attribute("lon") = std::to_string(data[1]).c_str();
return cur_id++;
}
IdType add_way(const IdType *const begin, const std::size_t size)
{
pugi::xml_node osm_way = osm_xml.child("osm").append_child("way");
osm_way.append_attribute("id") = cur_id;
osm_way.append_attribute("version") = 1;
for (std::size_t idx = 0; idx < size; idx++)
osm_way.append_child("nd").append_attribute("ref") = std::to_string(begin[idx]).c_str();
return cur_id++;
}
IdType cur_id = 1000;
};