-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosm.cpp
322 lines (257 loc) · 8.11 KB
/
osm.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*osm.cpp*/
//
// Prof. Joe Hummel
// U. of Illinois, Chicago
// CS 251: Spring 2020
// Project #07: open street maps, graphs, and Dijkstra's alg
//
// References:
// TinyXML: https://github.com/leethomason/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 <vector>
#include <map>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include "tinyxml2.h"
#include "osm.h"
using namespace std;
using namespace tinyxml2;
//
// LoadOpenStreetMap
//
bool LoadOpenStreetMap(string filename, XMLDocument &xmldoc)
{
//
// load the XML document:
//
xmldoc.LoadFile(filename.c_str());
if (xmldoc.ErrorID() != 0) // failed:
{
cout << "**ERROR: unable to open map file '" << filename << "'." << endl;
return false;
}
//
// top-level element should be "osm" if the file is a valid open
// street map:
//
XMLElement *osm = xmldoc.FirstChildElement("osm");
if (osm == nullptr)
{
cout << "**ERROR: unable to find top-level 'osm' XML element." << endl;
return false;
}
//
// success:
//
return true;
}
//
// ReadMapNodes
//
int ReadMapNodes(XMLDocument &xmldoc, map<long long, Coordinates> &Nodes)
{
XMLElement *osm = xmldoc.FirstChildElement("osm");
assert(osm != nullptr);
//
// Parse the XML document node by node:
//
int nodeCount = 0;
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();
nodeCount++;
//
// store node in the map:
//
Nodes[id] = Coordinates(id, latitude, longitude);
//
// next node element in the XML doc:
//
node = node->NextSiblingElement("node");
}
//
// done:
//
return nodeCount;
}
//
// ReadFootways
//
int ReadFootways(XMLDocument &xmldoc, vector<FootwayInfo> &Footways)
{
XMLElement *osm = xmldoc.FirstChildElement("osm");
assert(osm != nullptr);
//
// Parse the XML document way by way, looking for footways:
//
int footwayCount = 0;
XMLElement *way = osm->FirstChildElement("way");
while (way != nullptr)
{
const XMLAttribute *attr = way->FindAttribute("id");
assert(attr != nullptr);
long long id = attr->Int64Value();
//
// we have to loop through all the tag attributes and
// see if this is a footway:
//
bool isFootway = false;
XMLElement *tag = way->FirstChildElement("tag");
while (tag != nullptr)
{
const XMLAttribute *attrk = tag->FindAttribute("k");
const XMLAttribute *attrv = tag->FindAttribute("v");
if (attrk != nullptr && attrv != nullptr)
{
const char *k_value = attrk->Value();
const char *v_value = attrv->Value();
if ((strcmp(k_value, "highway") == 0) && (strcmp(v_value, "footway") == 0))
{
footwayCount++;
isFootway = true;
break;
}
}
tag = tag->NextSiblingElement("tag");
}
//
// if this is a footway, collect the node ids and store another
// footway object in the vector:
//
if (isFootway)
{
FootwayInfo footway(id);
XMLElement *nd = way->FirstChildElement("nd");
while (nd != nullptr)
{
const XMLAttribute *ndref = nd->FindAttribute("ref");
assert(ndref != nullptr);
long long id = ndref->Int64Value();
footway.Nodes.push_back(id);
// advance to next node ref:
nd = nd->NextSiblingElement("nd");
}
Footways.push_back(footway);
} //if
way = way->NextSiblingElement("way");
} //while
//
// done:
//
return footwayCount;
}
//
// ReadUniversityBuildings
//
int ReadUniversityBuildings(XMLDocument &xmldoc,
map<long long, Coordinates> &Nodes,
vector<BuildingInfo> &Buildings)
{
XMLElement *osm = xmldoc.FirstChildElement("osm");
assert(osm != nullptr);
//
// Parse the XML document way by way, looking for university buildings:
//
int buildingCount = 0;
XMLElement *way = osm->FirstChildElement("way");
while (way != nullptr)
{
const XMLAttribute *attr = way->FindAttribute("id");
assert(attr != nullptr);
long long id = attr->Int64Value();
bool isBuilding = false;
const char *buildingName = nullptr;
XMLElement *tag = way->FirstChildElement("tag");
while (tag != nullptr)
{
const XMLAttribute *attrk = tag->FindAttribute("k");
const XMLAttribute *attrv = tag->FindAttribute("v");
if (attrk != nullptr && attrv != nullptr)
{
const char *k_value = attrk->Value();
const char *v_value = attrv->Value();
if ((strcmp(k_value, "building") == 0) && (strcmp(v_value, "university") == 0))
{
buildingCount++;
isBuilding = true;
}
if (strcmp(k_value, "name") == 0)
{
buildingName = v_value;
}
}
tag = tag->NextSiblingElement("tag");
}
//
// if this is a building, store info into vector:
//
if (isBuilding)
{
XMLElement *nd = way->FirstChildElement("nd");
//
// we need to compute a (lat, lon) for the building, so we compute
// the average based on the nodes that define the perimiter to the
// building. We would be better if the XML defined the position
// of the door(s)?
//
double totalLat = 0.0;
double totalLon = 0.0;
int numNodes = 0;
while (nd != nullptr)
{
const XMLAttribute *ndref = nd->FindAttribute("ref");
assert(ndref != nullptr);
long long id = ndref->Int64Value();
assert(Nodes.find(id) != Nodes.end());
totalLat += Nodes[id].Lat;
totalLon += Nodes[id].Lon;
numNodes++;
// advance to next node ref:
nd = nd->NextSiblingElement("nd");
} //while
//
// compute average to get a rough position of building, and store
// building info into vector:
//
double lat = totalLat / numNodes;
double lon = totalLon / numNodes;
assert(buildingName != nullptr);
string fullname(buildingName);
//
// do we have an abbreviation? Appears as "... (SEO)" in the string:
//
string abbrev = "?";
size_t left = fullname.find('(');
size_t right = fullname.find(')');
if (left != string::npos && right != string::npos && left < right)
{
abbrev = fullname.substr(left + 1, right - left - 1);
}
Buildings.push_back(BuildingInfo(fullname, abbrev, id, lat, lon));
} //if
way = way->NextSiblingElement("way");
} //while
//
// done:
//
return buildingCount;
}