-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointMap.cpp
390 lines (313 loc) · 11.8 KB
/
PointMap.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "PointMap.h"
#include <algorithm>
#include <unordered_map>
#include <iostream>
#include <limits>
#include <chrono>
#include <cfloat>
void simulateSlope(std::shared_ptr<Connection> connection) {
for (int initialSpeed = MINIMUM_SPEED_KMH; initialSpeed <= MAXIMUM_SPEED_KMH; initialSpeed++) {
double speed = initialSpeed / 3.6;
double distance = 0;
while (distance < connection->horizontalDistance/connection->cosSlope && speed >= MINIMUM_SPEED_KMH / 3.6) {
distance += speed * TIME_STEP;
speed += (-GRAVITY_CONSTANT * connection->sinSlope - DRAG_COEFFICIENT * speed * speed) * TIME_STEP;
}
if (speed < MINIMUM_SPEED_KMH / 3.6) {
speed = -1;
}
if (speed > MAXIMUM_SPEED_KMH / 3.6) {
speed = MAXIMUM_SPEED_KMH / 3.6;
}
connection->speeds[initialSpeed - MINIMUM_SPEED_KMH] = speed;
}
}
PointMap::PointMap() : srcEpsg(0), transformation(nullptr) {
minimumX = DBL_MAX;
maximumX = -DBL_MAX;
minimumY = DBL_MAX;
maximumY = -DBL_MAX;
minimumZ = DBL_MAX;
maximumZ = -DBL_MAX;
}
void PointMap::loadDataset(GDALDataset *dataset) {
loadDataset(dataset, [](Progress progress){printf("\r%s: %d%%", progress.message.c_str(), progress.percent); std::cout << std::flush;});
}
void PointMap::loadDataset(GDALDataset* dataset, std::function<void(Progress progress)> progressCallback) {
// First, find the speed limits
OGRLayer *speedLimits;
speedLimits = dataset->GetLayerByName("Fartsgrense");
std::unordered_map<int, int> linkIdToSpeedLimitMap;
if (progressCallback) {
Progress currentProgress;
currentProgress.message = "Finding speed limits";
currentProgress.percent = 0;
progressCallback(currentProgress);
}
for (auto& feature : speedLimits) {
int idField = feature->GetFieldIndex("lineærposisjon|LineærPosisjonStrekning|lenkesekvens|Identifikasjon|lokalId");
int speedLimitField = feature->GetFieldIndex("fartsgrenseVerdi");
int id = feature->GetFieldAsInteger(idField);
int speedLimit = feature->GetFieldAsInteger(speedLimitField);
linkIdToSpeedLimitMap[id] = speedLimit;
}
// Find the roads
OGRLayer *roads;
// Veglenke is the feature layer in the ElVeg2.0 dataset that contains all the roads.
// Veglenke means "road chain" in Norwegian
roads = dataset->GetLayerByName("Veglenke");
if (progressCallback) {
Progress currentProgress;
currentProgress.message = "Finding extents of road network";
currentProgress.percent = 0;
progressCallback(currentProgress);
}
minimumX = DBL_MAX;
minimumY = DBL_MAX;
minimumZ = DBL_MAX;
maximumX = -DBL_MAX;
maximumY = -DBL_MAX;
maximumZ = -DBL_MAX;
// Find the extents that the PointMap needs to extend to
int numberOfRoads = 0;
for (auto& road : roads) {
// Assume all geometries are Linestrings, which I think they will be
OGRLineString *geometry = road->GetGeometryRef()->toLineString();
for (auto& point : geometry) {
minimumX = std::min(point.getX(), minimumX);
minimumY = std::min(point.getY(), minimumY);
minimumZ = std::min(point.getZ(), minimumZ);
maximumX = std::max(point.getX(), maximumX);
maximumY = std::max(point.getY(), maximumY);
maximumZ = std::max(point.getZ(), maximumZ);
}
numberOfRoads++;
}
minimumX -= 1;
minimumY -= 1;
minimumZ -= 1;
maximumX += 1;
maximumY += 1;
maximumZ += 1;
rebuildPointMap();
// Next, create the point map with its connections
if (progressCallback) {
Progress currentProgress;
currentProgress.message = "Creating graph of road network";
currentProgress.percent = 0;
progressCallback(currentProgress);
}
auto lastUpdateTime = std::chrono::system_clock::now();
int counter = 0;
for (auto& road : roads) {
if (progressCallback) {
counter++;
auto now = std::chrono::system_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastUpdateTime).count() > 200) {
Progress currentProgress;
currentProgress.message = "Creating graph of road network";
currentProgress.percent = (int) (counter * 100.0 / numberOfRoads);
currentProgress.loadedRoads = loadedRoads;
progressCallback(currentProgress);
lastUpdateTime = now;
}
}
// First, check if speed limit is too high
int roadIdFieldIndex = road->GetFieldIndex("lenkesekvens|Lenkesekvensreferanse|identifikasjon|Identifikasjon|lokalId");
int roadId = road->GetFieldAsInteger(roadIdFieldIndex);
// If no speed limit, just assume it is a tiny road and cyclable
if (linkIdToSpeedLimitMap.count(roadId)) {
int speedLimit = linkIdToSpeedLimitMap[roadId];
if (speedLimit > MAXIMUM_ROAD_SPEED_LIMIT_KMH) {
continue;
}
}
// If speed limit is not too high, go through the points on the road
// Find out which directions it is possible to move
bool forwardsPossible = false;
bool backwardsPossible = false;
bool forwardsBikelane = false;
bool backwardsBikelane = false;
int typeIndex = road->GetFieldIndex("typeVeg");
const char* roadType = road->GetFieldAsString(typeIndex);
// If this road is a staircase (which it might apparently be), we don't want to bike it
if (
strcmp(roadType, "trapp") == 0
|| strcmp(roadType, "motorveg") == 0
|| strcmp(roadType, "motortrafikkveg") == 0
|| strcmp(roadType, "bilferje") == 0
|| strcmp(roadType, "passasjerferje") == 0
) {
continue;
}
if (strcmp(roadType, "gangOgSykkelveg") == 0 || strcmp(roadType, "sykkelveg") == 0) {
forwardsBikelane = true;
backwardsBikelane = true;
}
// If there are odd numbered lanes, they are going forward. Even numbered lanes are going backwards
// Lanes marked with an S at the end are bike lanes
// The token # is used delimit lanes
int lanesIndex = road->GetFieldIndex("feltoversikt");
std::string lanes = road->GetFieldAsString(lanesIndex);
size_t pos = 0;
while (lanes.length() > 0) {
pos = lanes.find("#");
if (pos == std::string::npos) {
pos = lanes.length();
}
int laneNumber = atoi(lanes.substr(0,pos).c_str());
bool forward = (laneNumber % 2) == 1;
bool bikeLane = lanes.substr(0,pos).find("S") != std::string::npos;
if (forward) {
forwardsPossible = true;
if (bikeLane) {
forwardsBikelane = true;
}
} else {
backwardsPossible = true;
if (bikeLane) {
backwardsBikelane = true;
}
}
lanes.erase(0, pos+1);
}
// Go through all the points and create road points and connections
OGRLineString *lineString = road->GetGeometryRef()->toLineString();
std::vector<std::shared_ptr<RoadPoint>> loadedRoad;
for (int i = 0; i < lineString->getNumPoints() - 1; i++) {
OGRPoint thisOgrPoint;
lineString->getPoint(i, &thisOgrPoint);
OGRPoint nextOgrPoint;
lineString->getPoint(i+1, &nextOgrPoint);
std::shared_ptr<RoadPoint> thisPoint = getPoint(thisOgrPoint.getX(), thisOgrPoint.getY(), thisOgrPoint.getZ(), road->GetGeometryRef()->getSpatialReference());
std::shared_ptr<RoadPoint> nextPoint = getPoint(nextOgrPoint.getX(), nextOgrPoint.getY(), nextOgrPoint.getZ(), road->GetGeometryRef()->getSpatialReference());
loadedRoad.push_back(thisPoint);
if (i == lineString->getNumPoints() - 2) {
loadedRoad.push_back(nextPoint);
}
double distance = sqrt(pow(nextPoint->x - thisPoint->x, 2) + pow(nextPoint->y - thisPoint->y, 2));
double heightDifference = nextPoint->z - thisPoint->z;
double slope = atan2(heightDifference, distance);
double sinSlope = sin(slope);
double cosSlope = cos(slope);
if (forwardsPossible) {
std::shared_ptr<Connection> connection(new Connection);
connection->connectedPoint = nextPoint;
connection->horizontalDistance = distance;
connection->heightDifference = heightDifference;
connection->sinSlope = sinSlope;
connection->cosSlope = cosSlope;
connection->hasBikeLane = forwardsBikelane;
simulateSlope(connection);
thisPoint->connections.push_back(connection);
}
if (backwardsPossible) {
std::shared_ptr<Connection> connection(new Connection);
connection->connectedPoint = thisPoint;
connection->horizontalDistance = distance;
connection->heightDifference = -heightDifference;
connection->sinSlope = -sinSlope;
connection->cosSlope = cosSlope;
connection->hasBikeLane = backwardsBikelane;
simulateSlope(connection);
nextPoint->connections.push_back(connection);
}
}
loadedRoads.push_back(loadedRoad);
}
}
void PointMap::rebuildPointMap() {
pointMap.clear();
loadedRoads.clear();
for (std::shared_ptr<RoadPoint> roadPoint : roadPoints) {
int xBin = (int) ((roadPoint->x - minimumX) * numberOfBins / (maximumX - minimumX));
int yBin = (int) ((roadPoint->y - minimumY) * numberOfBins / (maximumY - minimumY));
int zBin = (int) ((roadPoint->z - minimumZ) * numberOfBins / (maximumZ - minimumZ));
if (xBin < 0 || xBin >= NUMBER_OF_BINS || yBin < 0 || yBin >= NUMBER_OF_BINS || zBin < 0 || zBin >= NUMBER_OF_BINS) {
continue;
}
int bin = (xBin * numberOfBins + yBin) * numberOfBins + zBin;
if (pointMap.find(bin) == pointMap.end()) {
pointMap[bin] = std::shared_ptr<std::vector<std::shared_ptr<RoadPoint>>>(new std::vector<std::shared_ptr<RoadPoint>>);
}
pointMap[bin]->push_back(roadPoint);
}
}
std::shared_ptr<RoadPoint> PointMap::getPoint(double x, double y, double z, OGRSpatialReference *ref) {
double xValue = ((x - minimumX) * numberOfBins / (maximumX - minimumX));
double yValue = ((y - minimumY) * numberOfBins / (maximumY - minimumY));
double zValue = ((z - minimumZ) * numberOfBins / (maximumZ - minimumZ));
std::vector<int> xValues;
std::vector<int> yValues;
std::vector<int> zValues;
double xThreshold = pointThreshold * numberOfBins / (maximumX - minimumX);
xValues.push_back((int) xValue);
if (xValue - (int) xValue <= xThreshold) {
xValues.push_back((int) xValue - 1);
}
if ((int) xValue + 1 - xValue <= xThreshold) {
xValues.push_back((int) xValue + 1);
}
double yThreshold = pointThreshold * numberOfBins / (maximumY - minimumY);
yValues.push_back((int) yValue);
if (yValue - (int) yValue <= yThreshold) {
yValues.push_back((int) yValue - 1);
}
if ((int) yValue + 1 - yValue <= yThreshold) {
yValues.push_back((int) yValue + 1);
}
double zThreshold = pointThreshold * numberOfBins / (maximumZ - minimumZ);
zValues.push_back((int) zValue);
if (zValue - (int) zValue <= zThreshold) {
zValues.push_back((int) zValue - 1);
}
if ((int) zValue + 1 - zValue <= zThreshold) {
zValues.push_back((int) zValue + 1);
}
for (int xBin : xValues) {
for (int yBin : yValues) {
for (int zBin : zValues) {
int bin = (xBin * numberOfBins + yBin) * numberOfBins + zBin;
if (pointMap.find(bin) == pointMap.end()) {
pointMap[bin] = std::shared_ptr<std::vector<std::shared_ptr<RoadPoint>>>(new std::vector<std::shared_ptr<RoadPoint>>);
}
for (std::shared_ptr<RoadPoint> roadPoint : *pointMap[bin]) {
if (std::abs(roadPoint->x - x) <= pointThreshold
&& std::abs(roadPoint->y - y) <= pointThreshold
&& std::abs(roadPoint->z - z) <= pointThreshold) {
return roadPoint;
}
}
}
}
}
// If no point was found, generate a new one
std::shared_ptr<RoadPoint> newPoint = std::shared_ptr<RoadPoint>(new RoadPoint);
newPoint->x = x;
newPoint->y = y;
newPoint->z = z;
// Calculate lat, lon and alt
double newX = x;
double newY = y;
double newZ = z;
if (ref->GetEPSGGeogCS() != srcEpsg) {
OGRSpatialReference targetReference;
targetReference.importFromEPSG(4326);
transformation = OGRCreateCoordinateTransformation(ref, &targetReference);
srcEpsg = ref->GetEPSGGeogCS();
}
transformation->Transform(1, &newX, &newY, &newZ);
newPoint->lat = newX;
newPoint->lon = newY;
newPoint->alt = newZ;
int bin = ((int) xValue * numberOfBins + (int) yValue) * numberOfBins + (int) zValue;
pointMap[bin]->push_back(newPoint);
roadPoints.push_back(newPoint);
return newPoint;
}
std::vector<std::shared_ptr<RoadPoint>> PointMap::getAllPoints() {
return roadPoints;
}
std::vector<std::vector<std::shared_ptr<RoadPoint>>> PointMap::getLoadedRoads() {
return loadedRoads;
}