-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgraph.cpp
More file actions
117 lines (98 loc) · 3.68 KB
/
graph.cpp
File metadata and controls
117 lines (98 loc) · 3.68 KB
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
#include <iostream>
#include <map> // BST
#include <unordered_map> // hash table
#include <vector> // dynamic array
#include <string> //
#include "tinyxml2.h"
using namespace tinyxml2;
struct Node {
//long long id;
double lat, lon;
};
struct Way {
std::vector<long long> node_refs;
bool is_highway = false;
bool is_one_way = false;
std::string name; // To store the road name if available
};
struct Edge {
long long node1, node2;
std::string label;
};
std::map<long long, Node> nodes;
std::vector<Way> ways;
std::unordered_map<long long, std::unordered_map<long long, std::string>> graph; // Adjacency list
double min_lat = 1e9, max_lat = -1e9;
double min_lon = 1e9, max_lon = -1e9;
void parse_osm(const char* filename) {
XMLDocument doc;
if (doc.LoadFile(filename) != XML_SUCCESS)
throw std::runtime_error("Failed to read OSM file");
XMLElement* root = doc.RootElement();
for (XMLElement* elem = root->FirstChildElement(); elem; elem = elem->NextSiblingElement()) {
std::string tagName = elem->Name();
if (tagName == "node") {
long long id = std::stoll(elem->Attribute("id"));
double lat = std::stod(elem->Attribute("lat"));
double lon = std::stod(elem->Attribute("lon"));
nodes[id] = {lat, lon};
min_lat = std::min(min_lat, lat);
max_lat = std::max(max_lat, lat);
min_lon = std::min(min_lon, lon);
max_lon = std::max(max_lon, lon);
} else if (tagName == "way") {
Way way;
bool is_hw = false;
for (XMLElement* child = elem->FirstChildElement(); child; child = child->NextSiblingElement()) {
std::string childName = child->Name();
if (childName == "nd") {
long long ref = std::stoll(child->Attribute("ref"));
way.node_refs.push_back(ref);
} else if (childName == "tag") {
const char* k = child->Attribute("k");
const char* v = child->Attribute("v");
if (k && std::string(k) == "highway") {
is_hw = true;
}
if (k && std::string(k) == "name") {
way.name = v ? v : "";
}
if (k && std::string(k) == "oneway" && std::string(v)=="yes") {
way.is_one_way=true;
}
}
}
way.is_highway = is_hw;
if(way.is_highway){
ways.push_back(way);
// Add edges to the graph (undirected)
for (size_t i = 1; i < way.node_refs.size(); ++i) {
long long node1 = way.node_refs[i - 1];
long long node2 = way.node_refs[i];
graph[node1][node2] = way.name;
if (!way.is_one_way)
graph[node2][node1] = way.name; // Since roads are usually bidirectional
}//for
}// is_highway
}//way
} //main loop
}
void print_graph() {
for (const auto& node : graph) {
std::cout << "Node" << nodes[node.first].lat << " " << nodes[node.first].lon <<"\n";
for (const auto& edge : node.second) {
std::cout << "Edge: " << node.first << " <-> " << edge.first << " (Road: " << edge.second << ")\n";
}
}
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <input.osm> \n";
return 1;
}
const char* input_file = argv[1];
parse_osm(input_file);
// Print the graph with edge labels
print_graph();
return 0;
}