forked from francois-baptiste/geojson-path-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (148 loc) · 6.26 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
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
'use strict';
const findPath = require('./dijkstra'),
findIsochronePoints = require('./isochrone'),
preprocess = require('./preprocessor'),
compactor = require('./compactor'),
WeightFunctions = require('./weight-functions'),
roundCoord = require('./round-coord'),
distance = require('@turf/distance').default,
point = require('turf-point'),
helpers = require('@turf/helpers'),
concave = require('@turf/concave').default;
module.exports = {
PathFinder,
WeightFunctions,
};
function PathFinder(graph, options) {
options = options || {};
if (!graph.compactedVertices) {
graph = preprocess(graph, options);
}
this._graph = graph;
this._keyFn = options.keyFn || function(c) {
return c.join(',');
};
this._precision = options.precision || 1e-5;
this._options = options;
if (Object.keys(this._graph.compactedVertices).filter(function(k) { return k !== 'edgeData'; }).length === 0) {
throw new Error('Compacted graph contains no forks (topology has no intersections).');
}
}
PathFinder.prototype = {
findPointsAround: function(a, b) {
const start = this._keyFn(roundCoord(a.geometry.coordinates, this._precision));
// We can't find a path if start isn't in the
// set of non-compacted vertices
if (!this._graph.vertices[start]) {
return null;
}
const costs = findIsochronePoints(this._graph.vertices, start, b);
return Object.keys(costs).map((n) => n.split(',').map((v) => parseFloat(v)));
},
getIsoDistanceConvexHull: function(a, b) {
const nodes = this.findPointsAround(a, b);
const points = helpers.featureCollection(nodes.map((v) => point(v)));
// const hull = convex(points);
return null;
},
getIsoDistanceConcaveHull: function(a, b) {
const nodes = this.findPointsAround(a, b);
const points = helpers.featureCollection(nodes.map((v) => point(v)));
const options = {units: 'kilometers', maxEdge: 10};
const hull = concave(points, options);
return hull;
},
findPath: function(a, b) {
var start = this._keyFn(roundCoord(a.geometry.coordinates, this._precision)),
finish = this._keyFn(roundCoord(b.geometry.coordinates, this._precision));
// We can't find a path if start or finish isn't in the
// set of non-compacted vertices
if (!this._graph.vertices[start] || !this._graph.vertices[finish]) {
return null;
}
var phantomStart = this._createPhantom(start);
var phantomEnd = this._createPhantom(finish);
var path = findPath(this._graph.compactedVertices, start, finish);
if (path) {
var weight = path[0];
path = path[1];
return {
path: path.reduce(function buildPath(cs, v, i, vs) {
if (i > 0) {
cs = cs.concat(this._graph.compactedCoordinates[vs[i - 1]][v]);
}
return cs;
}.bind(this), []).concat([this._graph.sourceVertices[finish]]),
weight: weight,
edgeDatas: this._graph.compactedEdges
? path.reduce(function buildEdgeData(eds, v, i, vs) {
if (i > 0) {
eds.push({
reducedEdge: this._graph.compactedEdges[vs[i - 1]][v]
});
}
return eds;
}.bind(this), [])
: undefined
};
} else {
return null;
}
this._removePhantom(phantomStart);
this._removePhantom(phantomEnd);
},
serialize: function() {
return this._graph;
},
findNearestJunction: function(p) {
var vertex = [ null, Number.MAX_VALUE ];
var junctions = Object.keys(this._graph.vertices).filter ( (function(k) {
var nEdges = Object.keys(this._graph.vertices[k]).length;
return nEdges >= 3 || nEdges == 1;
}).bind(this));
junctions.forEach( (function(k) {
const dist = distance(point(p), point(this._graph.sourceVertices[k]));
if(dist < vertex[1]) {
vertex[1] = dist;
vertex[0] = this._graph.sourceVertices[k].slice(0);
}
}).bind(this));
return vertex;
},
_createPhantom: function(n) {
if (this._graph.compactedVertices[n]) return null;
var phantom = compactor.compactNode(n, this._graph.vertices, this._graph.compactedVertices, this._graph.sourceVertices, this._graph.edgeData, true, this._options);
this._graph.compactedVertices[n] = phantom.edges;
this._graph.compactedCoordinates[n] = phantom.coordinates;
if (this._graph.compactedEdges) {
this._graph.compactedEdges[n] = phantom.reducedEdges;
}
Object.keys(phantom.incomingEdges).forEach(function(neighbor) {
this._graph.compactedVertices[neighbor][n] = phantom.incomingEdges[neighbor];
this._graph.compactedCoordinates[neighbor][n] = [this._graph.sourceVertices[neighbor]].concat(phantom.incomingCoordinates[neighbor].slice(0, -1));
if (this._graph.compactedEdges) {
this._graph.compactedEdges[neighbor][n] = phantom.reducedEdges[neighbor];
}
}.bind(this));
return n;
},
_removePhantom: function(n) {
if (!n) return;
Object.keys(this._graph.compactedVertices[n]).forEach(function(neighbor) {
delete this._graph.compactedVertices[neighbor][n];
}.bind(this));
Object.keys(this._graph.compactedCoordinates[n]).forEach(function(neighbor) {
delete this._graph.compactedCoordinates[neighbor][n];
}.bind(this));
if (this._graph.compactedEdges) {
Object.keys(this._graph.compactedEdges[n]).forEach(function(neighbor) {
delete this._graph.compactedEdges[neighbor][n];
}.bind(this));
}
delete this._graph.compactedVertices[n];
delete this._graph.compactedCoordinates[n];
if (this._graph.compactedEdges) {
delete this._graph.compactedEdges[n];
}
}
};