-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.cpp
132 lines (113 loc) · 3.15 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
126
127
128
129
130
131
132
/*nodes.cpp*/
//
// A collection of nodes in the Open Street Map.
//
// References:
//
// TinyXML:
// files: https://github.com/leethomason/tinyxml2
// docs: http://leethomason.github.io/tinyxml2/
//
// OpenStreetMap: https://www.openstreetmap.org
// OpenStreetMap docs:
// https://wiki.openstreetmap.org/wiki/Main_Page
// https://wiki.openstreetmap.org/wiki/Map_Features
// https://wiki.openstreetmap.org/wiki/Node
// https://wiki.openstreetmap.org/wiki/Way
// https://wiki.openstreetmap.org/wiki/Relation
//
#include <iostream>
#include <string>
#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 creates an object then pushes copy into vector:
//
// Node N(id, latitude, longitude, entrance);
// this->MapNodes.push_back(N);
//
// This creates just one object "emplace":
//
this->MapNodes.emplace(id, Node(id, latitude, longitude, entrance));
//
// next node element in the XML doc:
//
node = node->NextSiblingElement("node");
}
}
//
// 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) const
{
auto ptr = this->MapNodes.find(id);
if (ptr == this->MapNodes.end()) {
return false;
}
else {
// get the info and store in addr provided
id = ptr->first;
lat = ptr->second.getLat();
lon = ptr->second.getLon();
isEntrance = ptr->second.getIsEntrance();
return true;
}
}
//
// accessors / getters
//
int Nodes::getNumMapNodes() {
return (int) this->MapNodes.size();
}