-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.cpp
125 lines (107 loc) · 2.78 KB
/
nodes.cpp
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
118
119
120
121
122
123
124
125
/*nodes.cpp*/
//
// A collection of nodes in the Open Street Map.
//
// Mark Fortes
// Northwestern University
// CS 211: Winter 2023
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include <algorithm>
#include <cassert>
#include "nodes.h"
#include "osm.h"
#include "tinyxml2.h"
using namespace std;
using namespace tinyxml2;
//
// readMapNodes
//
// Given an XML document, reads through the document and
// stores all the nodes into the given vector. Each node
// is a point on the map, with a unique id along with
// (lat, lon) position. Some nodes are entrances to buildings,
// which we capture as well.
//
void Nodes::readMapNodes(XMLDocument& xmldoc)
{
XMLElement* osm = xmldoc.FirstChildElement("osm");
assert(osm != nullptr);
//
// Parse the XML document node by node:
//
XMLElement* node = osm->FirstChildElement("node");
while (node != nullptr)
{
const XMLAttribute* attrId = node->FindAttribute("id");
const XMLAttribute* attrLat = node->FindAttribute("lat");
const XMLAttribute* attrLon = node->FindAttribute("lon");
assert(attrId != nullptr);
assert(attrLat != nullptr);
assert(attrLon != nullptr);
long long id = attrId->Int64Value();
double latitude = attrLat->DoubleValue();
double longitude = attrLon->DoubleValue();
//
// is this node an entrance? Check for a
// standard entrance, the main entrance, or
// one-way entrance.
//
bool entrance = false;
if (osmContainsKeyValue(node, "entrance", "yes") ||
osmContainsKeyValue(node, "entrance", "main") ||
osmContainsKeyValue(node, "entrance", "entrance"))
{
entrance = true;
}
// Add node to vector:
this->MapNodes.emplace(id, Node(id, latitude, longitude, entrance));
// next node element in the XML doc:
node = node->NextSiblingElement("node");
}
}
//
// sortByID
//
// sorts the nodes into ascending order by ID.
//
void Nodes::sortByID()
{
}
//
// find
//
// Searches the nodes for the one with the matching ID, returning
// true if found and false if not. If found, a copy of the node
// is returned via the node parameter, which is passed by reference.
//
bool Nodes::find(long long id, double& lat, double& lon, bool& isEntrance)
{
auto ptr = this->MapNodes.find(id);
if (ptr == this->MapNodes.end()) { // not found:
return false;
}
else { // found:
lat = ptr->second.getLat();
lon = ptr->second.getLon();
isEntrance = ptr->second.getIsEntrance();
return true;
}
}
//
// accessors / getters
//
int Nodes::getNumMapNodes() {
return (int) this->MapNodes.size();
}
// allow foreach through the map
std::map<long long, Node>::iterator Nodes::begin()
{
return this->MapNodes.begin();
}
std::map<long long, Node>::iterator Nodes::end()
{
return this->MapNodes.end();
}