Line data Source code
1 : #include "data/SUMO/EdgeData.hpp" 2 : 3 : #include <spdlog/spdlog.h> 4 : 5 : #include <filesystem> 6 : #include <fstream> 7 : #include <iostream> 8 : 9 : #include "data/SUMO/Network.hpp" 10 : #include "utils/stringify.hpp" 11 : #include "utils/xml.hpp" 12 : 13 : using namespace std; 14 : using namespace SUMO; 15 : using namespace rapidxml; 16 : 17 : using utils::stringify::stringify; 18 : 19 : namespace fs = std::filesystem; 20 : namespace xml = utils::xml; 21 : 22 : typedef Network::Edge Edge; 23 : 24 0 : EdgeData::Interval::Edge::Lane::Lane(Network::Edge::Lane::ID id) { 25 0 : attributes.setAttribute("id", id); 26 0 : } 27 : 28 0 : EdgeData::Interval::Edge::Edge(Network::Edge::ID id) { 29 0 : attributes.setAttribute("id", id); 30 0 : } 31 : 32 0 : EdgeData::Interval::Interval(Time begin, Time end) { 33 0 : attributes.setAttribute("begin", begin); 34 0 : attributes.setAttribute("end", end); 35 0 : } 36 : 37 0 : EdgeData::Interval::Interval(Time begin, Time end, Interval::ID id) { 38 0 : attributes.setAttribute("begin", begin); 39 0 : attributes.setAttribute("end", end); 40 0 : attributes.setAttribute("id", id); 41 0 : } 42 : 43 0 : bool EdgeData::Interval::Edge::hasLane(Network::Edge::Lane::ID id) const { 44 0 : return lanes.find(id) != lanes.end(); 45 : } 46 : 47 0 : EdgeData::Interval::Edge::Lane &EdgeData::Interval::Edge::createLane(SUMO::Network::Edge::Lane::ID id) { 48 0 : lanes.emplace(id, Lane(id)); 49 0 : return lanes.at(id); 50 : } 51 : 52 0 : EdgeData::Interval::Edge::Lane &EdgeData::Interval::Edge::getLane(SUMO::Network::Edge::Lane::ID id) { 53 0 : return lanes.at(id); 54 : } 55 : 56 0 : bool EdgeData::Interval::hasEdge(Network::Edge::ID id) const { 57 0 : return edges.find(id) != edges.end(); 58 : } 59 : 60 0 : EdgeData::Interval::Edge &EdgeData::Interval::createEdge(Network::Edge::ID id) { 61 0 : edges.emplace(id, Edge(id)); 62 0 : return edges.at(id); 63 : } 64 : 65 0 : EdgeData::Interval::Edge &EdgeData::Interval::getEdge(Network::Edge::ID id) { 66 0 : return edges.at(id); 67 : } 68 : 69 0 : EdgeData::Interval &EdgeData::createInterval(Time begin, Time end) { 70 0 : intervals.emplace_back(Interval(begin, end)); 71 0 : return intervals.back(); 72 : } 73 : 74 0 : EdgeData::Interval &EdgeData::createInterval(Time begin, Time end, EdgeData::Interval::ID id) { 75 0 : intervals.emplace_back(Interval(begin, end, id)); 76 0 : idToInterval.emplace(id, &intervals.back()); 77 0 : return intervals.back(); 78 : } 79 : 80 0 : void EdgeData::saveToFile( 81 : const string &filePath 82 : ) const { 83 0 : xml_document<> doc; 84 0 : xml_node<> &meandata = *doc.allocate_node(node_element, "meandata"); 85 0 : doc.append_node(&meandata); 86 : 87 0 : for(const Interval &interval: intervals) { 88 0 : xml_node<> &intervalEl = *doc.allocate_node(node_element, "interval"); 89 0 : meandata.append_node(&intervalEl); 90 : 91 0 : for(const auto &[name, value]: interval.attributes.attributes) { 92 0 : xml::add_attribute(intervalEl, name, value); 93 : } 94 : 95 0 : for(const auto &[edgeID, edge]: interval.edges) { 96 0 : xml_node<> &edgeEl = *doc.allocate_node(node_element, "edge"); 97 0 : intervalEl.append_node(&edgeEl); 98 : 99 0 : for(const auto &[name, value]: edge.attributes.attributes) { 100 0 : xml::add_attribute(edgeEl, name, value); 101 : } 102 : 103 0 : for(const auto &[laneID, lane]: edge.lanes) { 104 0 : xml_node<> &laneEl = *doc.allocate_node(node_element, "lane"); 105 0 : edgeEl.append_node(&laneEl); 106 : 107 0 : for(const auto &[name, value]: lane.attributes.attributes) { 108 0 : xml::add_attribute(laneEl, name, value); 109 : } 110 : } 111 : } 112 : 113 0 : ofstream os; 114 0 : os.exceptions(ios_base::failbit | ios_base::badbit); 115 0 : fs::path p = fs::path(filePath).parent_path(); 116 0 : if(!fs::is_directory(p)) { 117 0 : spdlog::info("Creating directory {}", p.string()); 118 0 : if(!fs::create_directory(p)) { 119 0 : throw ios_base::failure("Could not create directory " + p.string()); 120 : } 121 : } 122 0 : try { 123 0 : os.open(filePath); 124 0 : } catch(const ios_base::failure &ex) { 125 0 : throw ios_base::failure("Could not open file " + filePath); 126 : } 127 0 : os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 128 0 : os << doc; 129 : } 130 0 : }