-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
77 lines (65 loc) · 1.86 KB
/
index.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
const fs = require("fs");
const shst = require("sharedstreets");
const level = require("level");
const rimraf = require("rimraf");
const turf = require("@turf/turf");
const ss = require("simple-statistics");
async function run() {
const trips = fs
.readFileSync("./trips.json")
.toString()
.split("\n")
.filter(d => {
return d.length;
})
.map(JSON.parse);
var edges = {};
const envelope = turf.bboxPolygon([
-86.79611206054688,
36.14612299393171,
-86.7575740814209,
36.17460406472864
]).geometry;
const graphOpts = {
source: "osm/planet-181224",
tileHierarchy: 6
};
var graph = new shst.Graph(envelope, graphOpts);
await graph.buildGraph();
var k = 0;
for (let trip of trips) {
const coordinates = trip.route.features.map(f => {
return f.geometry.coordinates;
});
const line = turf.lineString(coordinates);
const match = await graph.matchTrace(line);
if (match && match.matchedPath) {
var segments = [];
var i = 0;
for (let segment of match.segments) {
var part = turf.lineString(match.matchedPath.geometry.coordinates[i], {
geometryId: segment.geometryId,
adjacent: {}
});
segments.push(part);
i++;
}
for (let segment of segments) {
var record = edges[segment.properties.geometryId];
if (record) {
segment = record;
}
for (let adjacent of segments) {
if (!segment.properties.adjacent[adjacent.properties.geometryId]) {
segment.properties.adjacent[adjacent.properties.geometryId] = 1;
} else {
segment.properties.adjacent[adjacent.properties.geometryId]++;
}
}
edges[segment.properties.geometryId] = segment;
}
}
}
fs.writeFileSync("./edges.json", JSON.stringify(edges));
}
run();