-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
318 lines (279 loc) · 9.26 KB
/
map.js
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
class MyMap {
constructor() {
this.IconWidth = 32;
this.IconHeight = 52;
this.dotSize = 10;
// red Icon for source marker
this.srcIcon = L.icon({
iconUrl: 'img/red_marker.png',
iconSize: [this.IconWidth, this.IconHeight], // size of the icon
iconAnchor: [this.IconWidth / 2, this.IconHeight],
});
// green Icon for destination marker
this.dstIcon = L.icon({
iconUrl: 'img/green_marker.png',
iconSize: [this.IconWidth, this.IconHeight], // size of the icon
iconAnchor: [this.IconWidth / 2, this.IconHeight],
});
[this.nodesCoords, this.nodesMap] = this.getNodes();
this.graph = this.getGraph();
this.landmark_num = 32;
this.landmark_used_num = 2;
this.graph.graphPreprocessing(this.landmark_num);
this.landmarks = [];
this.map = this.renderMap();
this.defaultSrcCoords = [30.6078668,114.3850297];
this.defaultDstCoords = [30.6253647,114.301978];
this.srcCoords = this.defaultSrcCoords;
this.dstCoords = this.defaultDstCoords;
this.srcNode = this.nodesMap[this.srcCoords];
this.dstNode = this.nodesMap[this.dstCoords];
this.srcMarker = this.renderSrcMarker();
this.dstMarker = this.renderDstMarker();
this.lmMarkers = [];
this.srcMarker.on('dragend', this.setSrcPos.bind(this));
this.dstMarker.on('dragend', this.setDstPos.bind(this));
this.pathLines = new Array();
this.extPathsLines = new Array();
}
getNodes() {
let nodesCoords = new Array();
let nodesMap = new Map();
let node, coords;
// get nodesCoords array
for (const nodeFeatures of nodesFeatures) {
coords = [
Number(nodeFeatures.properties.lat),
Number(nodeFeatures.properties.lon),
];
nodesCoords.push(coords);
node = new Module.Node(coords[0], coords[1]);
nodesMap[coords] = node;
}
return [nodesCoords, nodesMap];
}
getGraph() {
const graph = new Module.Graph();
let srcNode, dstNode;
for (let i = 0; i < roadsFeatures.length; i++) {
srcNode =
this.nodesMap[
[Number(roadsFeatures[i].properties.src_la), Number(roadsFeatures[i].properties.src_lo)]
];
dstNode =
this.nodesMap[
[Number(roadsFeatures[i].properties.dst_la), Number(roadsFeatures[i].properties.dst_lo)]
];
graph.addEdge(
srcNode,
dstNode,
roadsFeatures[i].properties.length,
roadsFeatures[i].properties.oneway ? false : true
);
}
return graph;
}
getLandmarks(improved){
let data = this.graph.getLandmarks(this.landmark_used_num, improved);
let landmarks = [];
for(let i = 0; i < data.size(); i+=2){
landmarks.push([data.get(i), data.get(i+1)]);
}
return landmarks;
}
renderMap() {
// const coords = [latitude, longitude];
const coords = [30.579557, 114.306074];
const map = L.map('map').setView(coords, 12);
map.options.minZoom = 12; // Set minimum zoom
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
// Set maximum bounds to Wuhan
map.setMaxBounds(
new L.LatLngBounds(
new L.LatLng(30.408, 113.993),
new L.LatLng(30.823, 114.648)
)
);
// load geojson data
L.geoJSON(boundsFeatures, {
style: function setStyle(feature) {
return {
color: 'pink',
weight: 0,
};
},
}).addTo(map);
return map;
}
renderMarker(coords, icon=null) {
let marker;
if(icon){
marker = L.marker(coords, { icon: icon, draggable: true }).addTo(this.map);
}
else{
marker = L.marker(coords).addTo(this.map);
}
return marker;
}
renderSrcMarker() {
return this.renderMarker(this.srcCoords, this.srcIcon);
}
renderDstMarker() {
return this.renderMarker(this.dstCoords, this.dstIcon);
}
renderLandmarks(improved){
this.landmarks = this.getLandmarks(improved);
for(let i = 0; i < this.landmarks.length; i ++){
this.lmMarkers.push(this.renderMarker(this.landmarks[i]));
}
}
setSrcPos() {
let pos = this.srcMarker.getLatLng();
let closest = this.nodesCoords.reduce((a, b) =>
Module.distance(a[0], a[1], pos.lat, pos.lng, 'K') <
Module.distance(b[0], b[1], pos.lat, pos.lng, 'K') ? a: b
);
this.srcMarker.setLatLng(closest);
this.srcCoords = closest;
// console.log(`the current coordinates of start node is:${this.srcCoords}`);
this.srcNode = this.nodesMap[this.srcCoords];
this.removePaths();
}
setDstPos() {
let pos = this.dstMarker.getLatLng();
let closest = this.nodesCoords.reduce((a, b) =>
Module.distance(a[0], a[1], pos.lat, pos.lng, 'K') <
Module.distance(b[0], b[1], pos.lat, pos.lng, 'K') ? a: b
);
this.dstMarker.setLatLng(closest);
this.dstCoords = closest;
// console.log(`the current coordinates of target node is:${this.dstCoords}`);
this.dstNode = this.nodesMap[this.dstCoords];
this.removePaths();
}
renderLine(srcCoords, dstCoords, color) {
let lineString = L.polyline([srcCoords, dstCoords]).addTo(this.map);
lineString.setStyle({ color: color });
return lineString;
}
renderAllPaths(algo){
this.removePaths();
let success;
this.removeLmMarkers();
const start = Date.now();
switch(algo){
case "Uniform Cost Search":
success = this.graph.UCSearch(this.srcNode, this.dstNode);
break;
case "A* Search":
success = this.graph.AStarSearch(this.srcNode, this.dstNode);
break;
case "ALT Search":
success = this.graph.ALTSearch(this.srcNode, this.dstNode, this.landmark_used_num, false);
break;
case "ALT Search Improved":
success = this.graph.ALTSearch(this.srcNode, this.dstNode, this.landmark_used_num, true);
break;
default:
break;
}
const end = Date.now();
window.confirm(`Execution time: ${(end - start)/1000} s`);
if(algo==="ALT Search")
this.renderLandmarks(false);
if(algo==="ALT Search Improved")
this.renderLandmarks(true);
// for(let i = 0; i < this.landmarks.length; i ++){
// console.log(`{${this.landmarks[i][0].toFixed(7)}, ${this.landmarks[i][1].toFixed(7)}},`);
// }
this.renderExtPaths("blue");
if(success)
this.renderFinalPath("red");
}
renderExtPaths(color){
let extPaths = this.graph.getExtPaths();
let srcCoords, dstCoords;
let extPathCoords = [];
for (let path = 0; path < extPaths.size(); path+=4){
srcCoords = [extPaths.get(path), extPaths.get(path+1)];
dstCoords = [extPaths.get(path+2), extPaths.get(path+3)];
extPathCoords.push([srcCoords, dstCoords]);
}
extPaths.delete();
for(const pathCoords of extPathCoords)
this.extPathsLines.push(L.polyline(pathCoords, { color: color}).addTo(this.map));
}
renderFinalPath(color) {
this.finalPath = this.graph.getPath(this.srcNode, this.dstNode);
let srcCoords, dstCoords;
for (let i = 0; i <= this.finalPath.size() - 4; i+=2) {
srcCoords = [this.finalPath.get(i), this.finalPath.get(i+1)];
dstCoords = [this.finalPath.get(i+2), this.finalPath.get(i+3)];
this.pathLines.push(L.polyline([srcCoords, dstCoords], { color: color}).addTo(this.map));
}
this.finalPath.delete();
}
removePaths() {
this.extPathsLines.forEach((val) => {val.remove(); val = null;});
this.extPathsLines = [];
this.pathLines.forEach((val) => {val.remove(); val = null;});
this.pathLines = [];
}
removeLmMarkers(){
this.lmMarkers.forEach((val) => {val.remove(); val = null;});
this.lmMarkers = [];
}
resetMap(){
// set start and end marker and node
this.srcMarker.setLatLng(this.defaultSrcCoords);
this.dstMarker.setLatLng(this.defaultDstCoords);
this.srcNode = this.nodesMap[this.defaultSrcCoords];
this.dstNode = this.nodesMap[this.defaultDstCoords];
// set map view
const coords = [30.579557, 114.306074];
this.map.setView(coords, 12);
}
getUniqueEl(data){
data = new Set(data.map(JSON.stringify));
data = Array.from(data).map(JSON.parse);
return data;
}
}
function testEmbind() {
console.log('Start testing embind code');
let nodes = new Array();
let graph = new Module.Graph();
const num = 180000;
for (let i = 0; i < num; i++) {
nodes[i] = new Module.Node(i, i * i);
}
for (let i = 0; i < num - 1; i++) {
graph.addEdge(nodes[i], nodes[i + 1], 1, true);
}
for(let n = 0; n < 200; n++){
if (graph.UCSearch(nodes[0], nodes[num - 1])) {
console.log(n);
let extPaths = graph.getExtPaths();
let path = graph.getPath(nodes[0], nodes[num - 1]);
// for (let i = 0; i < path.size()-1; i+=2){
// console.log(path.get(i), path.get(i+1));
// }
extPaths.delete();
path.delete();
}
if (graph.AStarSearch(nodes[0], nodes[num - 1])) {
console.log(n);
let extPaths = graph.getExtPaths();
let path = graph.getPath(nodes[0], nodes[num - 1]);
// for (let i = 0; i < path.size()-1; i+=2){
// console.log(path.get(i), path.get(i+1));
// }
extPaths.delete();
path.delete();
}
}
console.log('Testing embind code completed');
}