-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildings.cpp
133 lines (109 loc) · 2.66 KB
/
buildings.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
133
/*buildings.cpp*/
//
// A collection of buildings in the Open Street Map.
//
// Mark Fortes
// Northwestern University
// CS 211: Winter 2023
//
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include "buildings.h"
#include "nodes.h"
#include "footways.h"
#include "osm.h"
#include "tinyxml2.h"
using namespace std;
using namespace tinyxml2;
//
// readMapBuildings
//
// Given an XML document, reads through the document and
// stores all the buildings into the given vector.
//
void Buildings::readMapBuildings(XMLDocument& xmldoc)
{
XMLElement* osm = xmldoc.FirstChildElement("osm");
assert(osm != nullptr);
//
// Parse the XML document way by way, looking for university buildings:
//
XMLElement* way = osm->FirstChildElement("way");
while (way != nullptr)
{
const XMLAttribute* attr = way->FindAttribute("id");
assert(attr != nullptr);
//
// if this is a building, store info into vector:
//
if (osmContainsKeyValue(way, "building", "university"))
{
string name = osmGetKeyValue(way, "name");
string streetAddr = osmGetKeyValue(way, "addr:housenumber")
+ " "
+ osmGetKeyValue(way, "addr:street");
//
// create building object, then add the associated
// node ids to the object:
//
long long id = attr->Int64Value();
Building B(id, name, streetAddr);
XMLElement* nd = way->FirstChildElement("nd");
while (nd != nullptr)
{
const XMLAttribute* ndref = nd->FindAttribute("ref");
assert(ndref != nullptr);
long long id = ndref->Int64Value();
B.add(id);
// advance to next node ref:
nd = nd->NextSiblingElement("nd");
}
//
// add the building to the vector:
//
this->MapBuildings.push_back(B);
}//if
way = way->NextSiblingElement("way");
}//while
//
// done:
//
}
//
// print
//
// prints each building (id, name, address) to the console.
//
void Buildings::print()
{
for (Building& B : this->MapBuildings) {
cout << B.ID << ": " << B.Name << ", " << B.StreetAddress << endl;
}
}
//
// findAndPrint
//
// Prints each building that contains the given name.
//
void Buildings::findAndPrint(string name, Nodes& nodes, Footways& footways)
{
//
// find every building that contains this name:
//
for (Building& B : this->MapBuildings)
{
if (B.Name.find(name) != string::npos) { // contains name:
B.print(nodes);
// which footways intersect with this building?
footways.intersectWithBuilding(B);
}
}
}
//
// accessors / getters
//
int Buildings::getNumMapBuildings() {
return (int) this->MapBuildings.size();
}