forked from perliedman/geojson-path-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (99 loc) · 3.72 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
import { compactNode } from './compactor'
import findPath from './dijkstra'
import preprocess from './preprocessor'
import roundCoord from './round-coord'
export default 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 = {
findPath: function (a, b) {
var start = this._keyFn(roundCoord(a.geometry.coordinates, this._precision))
var 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
},
_createPhantom: function (n) {
if (this._graph.compactedVertices[n]) return null
var phantom = 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] = phantom.incomingCoordinates[neighbor]
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]
}
}
}