-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.js
66 lines (54 loc) · 1.6 KB
/
mock.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
var fs = require('fs');
var Node = require('./models/node');
var Segment = require('./models/segment');
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
var data = JSON.parse(fs.readFileSync('vancouver.json', 'utf8'));
var nodes = {}, ways = {};
var trunNodes = {}, trunWays = {};
data.elements.forEach(function (row) {
if (row.type == "node") {
nodes[row.id] = row;
} else if (row.type = "way") {
ways[row.id] = row;
}
});
var wayCount = 0;
for (var wayId in ways) {
if (wayCount == 500)
break;
wayCount++;
trunWays[wayId] = ways[wayId];
}
for (var wayId in trunWays) {
for (var i = 0; i < trunWays[wayId].nodes.length - 1; i++) {
var aNode = Math.min(trunWays[wayId].nodes[i], trunWays[wayId].nodes[i+1]);
var bNode = Math.max(trunWays[wayId].nodes[i], trunWays[wayId].nodes[i+1]);
var rand = Math.random() * 25;
var segment = {
aNode: aNode,
bNode: bNode,
way: wayId,
speed: rand
}
Segment.findOneAndUpdateSegment(segment, function () {
console.log("segment inserted");
}, function () {
console.log("error");
});
Node.findOrInsertNode(nodes[aNode], function () {
console.log("aNode inserted");
}, function () {
console.log("error");
});
Node.findOrInsertNode(nodes[bNode], function () {
console.log("aNode inserted");
}, function () {
console.log("error");
})
}
}
res.json({ 'success': true });
});
module.exports = router;