@turf/shortest-path 7.0.0-alpha.2 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -14
- package/dist/cjs/index.cjs +399 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +34 -0
- package/dist/esm/index.d.ts +34 -0
- package/dist/esm/index.js +399 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +42 -33
- package/dist/es/index.js +0 -589
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -601
- package/index.d.ts +0 -16
package/dist/js/index.js
DELETED
|
@@ -1,601 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var bbox = require('@turf/bbox');
|
|
4
|
-
var booleanPointInPolygon = require('@turf/boolean-point-in-polygon');
|
|
5
|
-
var distance = require('@turf/distance');
|
|
6
|
-
var scale = require('@turf/transform-scale');
|
|
7
|
-
var cleanCoords = require('@turf/clean-coords');
|
|
8
|
-
var bboxPolygon = require('@turf/bbox-polygon');
|
|
9
|
-
var invariant = require('@turf/invariant');
|
|
10
|
-
var helpers = require('@turf/helpers');
|
|
11
|
-
|
|
12
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
-
|
|
14
|
-
var bbox__default = /*#__PURE__*/_interopDefaultLegacy(bbox);
|
|
15
|
-
var booleanPointInPolygon__default = /*#__PURE__*/_interopDefaultLegacy(booleanPointInPolygon);
|
|
16
|
-
var distance__default = /*#__PURE__*/_interopDefaultLegacy(distance);
|
|
17
|
-
var scale__default = /*#__PURE__*/_interopDefaultLegacy(scale);
|
|
18
|
-
var cleanCoords__default = /*#__PURE__*/_interopDefaultLegacy(cleanCoords);
|
|
19
|
-
var bboxPolygon__default = /*#__PURE__*/_interopDefaultLegacy(bboxPolygon);
|
|
20
|
-
|
|
21
|
-
// javascript-astar 0.4.1
|
|
22
|
-
// http://github.com/bgrins/javascript-astar
|
|
23
|
-
// Freely distributable under the MIT License.
|
|
24
|
-
// Implements the astar search algorithm in javascript using a Binary Heap.
|
|
25
|
-
// Includes Binary Heap (with modifications) from Marijn Haverbeke.
|
|
26
|
-
// http://eloquentjavascript.net/appendix2.html
|
|
27
|
-
|
|
28
|
-
function pathTo(node) {
|
|
29
|
-
var curr = node,
|
|
30
|
-
path = [];
|
|
31
|
-
while (curr.parent) {
|
|
32
|
-
path.unshift(curr);
|
|
33
|
-
curr = curr.parent;
|
|
34
|
-
}
|
|
35
|
-
return path;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function getHeap() {
|
|
39
|
-
return new BinaryHeap(function (node) {
|
|
40
|
-
return node.f;
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Astar
|
|
46
|
-
* @private
|
|
47
|
-
*/
|
|
48
|
-
var astar = {
|
|
49
|
-
/**
|
|
50
|
-
* Perform an A* Search on a graph given a start and end node.
|
|
51
|
-
*
|
|
52
|
-
* @private
|
|
53
|
-
* @memberof astar
|
|
54
|
-
* @param {Graph} graph Graph
|
|
55
|
-
* @param {GridNode} start Start
|
|
56
|
-
* @param {GridNode} end End
|
|
57
|
-
* @param {Object} [options] Options
|
|
58
|
-
* @param {bool} [options.closest] Specifies whether to return the path to the closest node if the target is unreachable.
|
|
59
|
-
* @param {Function} [options.heuristic] Heuristic function (see astar.heuristics).
|
|
60
|
-
* @returns {Object} Search
|
|
61
|
-
*/
|
|
62
|
-
search: function (graph, start, end, options) {
|
|
63
|
-
graph.cleanDirty();
|
|
64
|
-
options = options || {};
|
|
65
|
-
var heuristic = options.heuristic || astar.heuristics.manhattan,
|
|
66
|
-
closest = options.closest || false;
|
|
67
|
-
|
|
68
|
-
var openHeap = getHeap(),
|
|
69
|
-
closestNode = start; // set the start node to be the closest if required
|
|
70
|
-
|
|
71
|
-
start.h = heuristic(start, end);
|
|
72
|
-
|
|
73
|
-
openHeap.push(start);
|
|
74
|
-
|
|
75
|
-
while (openHeap.size() > 0) {
|
|
76
|
-
// Grab the lowest f(x) to process next. Heap keeps this sorted for us.
|
|
77
|
-
var currentNode = openHeap.pop();
|
|
78
|
-
|
|
79
|
-
// End case -- result has been found, return the traced path.
|
|
80
|
-
if (currentNode === end) {
|
|
81
|
-
return pathTo(currentNode);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Normal case -- move currentNode from open to closed, process each of its neighbors.
|
|
85
|
-
currentNode.closed = true;
|
|
86
|
-
|
|
87
|
-
// Find all neighbors for the current node.
|
|
88
|
-
var neighbors = graph.neighbors(currentNode);
|
|
89
|
-
|
|
90
|
-
for (var i = 0, il = neighbors.length; i < il; ++i) {
|
|
91
|
-
var neighbor = neighbors[i];
|
|
92
|
-
|
|
93
|
-
if (neighbor.closed || neighbor.isWall()) {
|
|
94
|
-
// Not a valid node to process, skip to next neighbor.
|
|
95
|
-
continue;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// The g score is the shortest distance from start to current node.
|
|
99
|
-
// We need to check if the path we have arrived at this neighbor is the shortest one we have seen yet.
|
|
100
|
-
var gScore = currentNode.g + neighbor.getCost(currentNode),
|
|
101
|
-
beenVisited = neighbor.visited;
|
|
102
|
-
|
|
103
|
-
if (!beenVisited || gScore < neighbor.g) {
|
|
104
|
-
// Found an optimal (so far) path to this node. Take score for node to see how good it is.
|
|
105
|
-
neighbor.visited = true;
|
|
106
|
-
neighbor.parent = currentNode;
|
|
107
|
-
neighbor.h = neighbor.h || heuristic(neighbor, end);
|
|
108
|
-
neighbor.g = gScore;
|
|
109
|
-
neighbor.f = neighbor.g + neighbor.h;
|
|
110
|
-
graph.markDirty(neighbor);
|
|
111
|
-
if (closest) {
|
|
112
|
-
// If the neighbour is closer than the current closestNode or if it's equally close but has
|
|
113
|
-
// a cheaper path than the current closest node then it becomes the closest node
|
|
114
|
-
if (
|
|
115
|
-
neighbor.h < closestNode.h ||
|
|
116
|
-
(neighbor.h === closestNode.h && neighbor.g < closestNode.g)
|
|
117
|
-
) {
|
|
118
|
-
closestNode = neighbor;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (!beenVisited) {
|
|
123
|
-
// Pushing to heap will put it in proper place based on the 'f' value.
|
|
124
|
-
openHeap.push(neighbor);
|
|
125
|
-
} else {
|
|
126
|
-
// Already seen the node, but since it has been rescored we need to reorder it in the heap
|
|
127
|
-
openHeap.rescoreElement(neighbor);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (closest) {
|
|
134
|
-
return pathTo(closestNode);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// No result was found - empty array signifies failure to find path.
|
|
138
|
-
return [];
|
|
139
|
-
},
|
|
140
|
-
// See list of heuristics: http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
|
|
141
|
-
heuristics: {
|
|
142
|
-
manhattan: function (pos0, pos1) {
|
|
143
|
-
var d1 = Math.abs(pos1.x - pos0.x);
|
|
144
|
-
var d2 = Math.abs(pos1.y - pos0.y);
|
|
145
|
-
return d1 + d2;
|
|
146
|
-
},
|
|
147
|
-
diagonal: function (pos0, pos1) {
|
|
148
|
-
var D = 1;
|
|
149
|
-
var D2 = Math.sqrt(2);
|
|
150
|
-
var d1 = Math.abs(pos1.x - pos0.x);
|
|
151
|
-
var d2 = Math.abs(pos1.y - pos0.y);
|
|
152
|
-
return D * (d1 + d2) + (D2 - 2 * D) * Math.min(d1, d2);
|
|
153
|
-
},
|
|
154
|
-
},
|
|
155
|
-
cleanNode: function (node) {
|
|
156
|
-
node.f = 0;
|
|
157
|
-
node.g = 0;
|
|
158
|
-
node.h = 0;
|
|
159
|
-
node.visited = false;
|
|
160
|
-
node.closed = false;
|
|
161
|
-
node.parent = null;
|
|
162
|
-
},
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* A graph memory structure
|
|
167
|
-
*
|
|
168
|
-
* @private
|
|
169
|
-
* @param {Array} gridIn 2D array of input weights
|
|
170
|
-
* @param {Object} [options] Options
|
|
171
|
-
* @param {boolean} [options.diagonal] Specifies whether diagonal moves are allowed
|
|
172
|
-
* @returns {void} Graph
|
|
173
|
-
*/
|
|
174
|
-
function Graph(gridIn, options) {
|
|
175
|
-
options = options || {};
|
|
176
|
-
this.nodes = [];
|
|
177
|
-
this.diagonal = !!options.diagonal;
|
|
178
|
-
this.grid = [];
|
|
179
|
-
for (var x = 0; x < gridIn.length; x++) {
|
|
180
|
-
this.grid[x] = [];
|
|
181
|
-
|
|
182
|
-
for (var y = 0, row = gridIn[x]; y < row.length; y++) {
|
|
183
|
-
var node = new GridNode(x, y, row[y]);
|
|
184
|
-
this.grid[x][y] = node;
|
|
185
|
-
this.nodes.push(node);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
this.init();
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
Graph.prototype.init = function () {
|
|
192
|
-
this.dirtyNodes = [];
|
|
193
|
-
for (var i = 0; i < this.nodes.length; i++) {
|
|
194
|
-
astar.cleanNode(this.nodes[i]);
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
Graph.prototype.cleanDirty = function () {
|
|
199
|
-
for (var i = 0; i < this.dirtyNodes.length; i++) {
|
|
200
|
-
astar.cleanNode(this.dirtyNodes[i]);
|
|
201
|
-
}
|
|
202
|
-
this.dirtyNodes = [];
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
Graph.prototype.markDirty = function (node) {
|
|
206
|
-
this.dirtyNodes.push(node);
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
Graph.prototype.neighbors = function (node) {
|
|
210
|
-
var ret = [],
|
|
211
|
-
x = node.x,
|
|
212
|
-
y = node.y,
|
|
213
|
-
grid = this.grid;
|
|
214
|
-
|
|
215
|
-
// West
|
|
216
|
-
if (grid[x - 1] && grid[x - 1][y]) {
|
|
217
|
-
ret.push(grid[x - 1][y]);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
// East
|
|
221
|
-
if (grid[x + 1] && grid[x + 1][y]) {
|
|
222
|
-
ret.push(grid[x + 1][y]);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// South
|
|
226
|
-
if (grid[x] && grid[x][y - 1]) {
|
|
227
|
-
ret.push(grid[x][y - 1]);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// North
|
|
231
|
-
if (grid[x] && grid[x][y + 1]) {
|
|
232
|
-
ret.push(grid[x][y + 1]);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
if (this.diagonal) {
|
|
236
|
-
// Southwest
|
|
237
|
-
if (grid[x - 1] && grid[x - 1][y - 1]) {
|
|
238
|
-
ret.push(grid[x - 1][y - 1]);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// Southeast
|
|
242
|
-
if (grid[x + 1] && grid[x + 1][y - 1]) {
|
|
243
|
-
ret.push(grid[x + 1][y - 1]);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// Northwest
|
|
247
|
-
if (grid[x - 1] && grid[x - 1][y + 1]) {
|
|
248
|
-
ret.push(grid[x - 1][y + 1]);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Northeast
|
|
252
|
-
if (grid[x + 1] && grid[x + 1][y + 1]) {
|
|
253
|
-
ret.push(grid[x + 1][y + 1]);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
return ret;
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
Graph.prototype.toString = function () {
|
|
261
|
-
var graphString = [],
|
|
262
|
-
nodes = this.grid, // when using grid
|
|
263
|
-
rowDebug,
|
|
264
|
-
row,
|
|
265
|
-
y,
|
|
266
|
-
l;
|
|
267
|
-
for (var x = 0, len = nodes.length; x < len; x++) {
|
|
268
|
-
rowDebug = [];
|
|
269
|
-
row = nodes[x];
|
|
270
|
-
for (y = 0, l = row.length; y < l; y++) {
|
|
271
|
-
rowDebug.push(row[y].weight);
|
|
272
|
-
}
|
|
273
|
-
graphString.push(rowDebug.join(" "));
|
|
274
|
-
}
|
|
275
|
-
return graphString.join("\n");
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
function GridNode(x, y, weight) {
|
|
279
|
-
this.x = x;
|
|
280
|
-
this.y = y;
|
|
281
|
-
this.weight = weight;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
GridNode.prototype.toString = function () {
|
|
285
|
-
return "[" + this.x + " " + this.y + "]";
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
GridNode.prototype.getCost = function (fromNeighbor) {
|
|
289
|
-
// Take diagonal weight into consideration.
|
|
290
|
-
if (fromNeighbor && fromNeighbor.x !== this.x && fromNeighbor.y !== this.y) {
|
|
291
|
-
return this.weight * 1.41421;
|
|
292
|
-
}
|
|
293
|
-
return this.weight;
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
GridNode.prototype.isWall = function () {
|
|
297
|
-
return this.weight === 0;
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
function BinaryHeap(scoreFunction) {
|
|
301
|
-
this.content = [];
|
|
302
|
-
this.scoreFunction = scoreFunction;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
BinaryHeap.prototype = {
|
|
306
|
-
push: function (element) {
|
|
307
|
-
// Add the new element to the end of the array.
|
|
308
|
-
this.content.push(element);
|
|
309
|
-
|
|
310
|
-
// Allow it to sink down.
|
|
311
|
-
this.sinkDown(this.content.length - 1);
|
|
312
|
-
},
|
|
313
|
-
pop: function () {
|
|
314
|
-
// Store the first element so we can return it later.
|
|
315
|
-
var result = this.content[0];
|
|
316
|
-
// Get the element at the end of the array.
|
|
317
|
-
var end = this.content.pop();
|
|
318
|
-
// If there are any elements left, put the end element at the
|
|
319
|
-
// start, and let it bubble up.
|
|
320
|
-
if (this.content.length > 0) {
|
|
321
|
-
this.content[0] = end;
|
|
322
|
-
this.bubbleUp(0);
|
|
323
|
-
}
|
|
324
|
-
return result;
|
|
325
|
-
},
|
|
326
|
-
remove: function (node) {
|
|
327
|
-
var i = this.content.indexOf(node);
|
|
328
|
-
|
|
329
|
-
// When it is found, the process seen in 'pop' is repeated
|
|
330
|
-
// to fill up the hole.
|
|
331
|
-
var end = this.content.pop();
|
|
332
|
-
|
|
333
|
-
if (i !== this.content.length - 1) {
|
|
334
|
-
this.content[i] = end;
|
|
335
|
-
|
|
336
|
-
if (this.scoreFunction(end) < this.scoreFunction(node)) {
|
|
337
|
-
this.sinkDown(i);
|
|
338
|
-
} else {
|
|
339
|
-
this.bubbleUp(i);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
},
|
|
343
|
-
size: function () {
|
|
344
|
-
return this.content.length;
|
|
345
|
-
},
|
|
346
|
-
rescoreElement: function (node) {
|
|
347
|
-
this.sinkDown(this.content.indexOf(node));
|
|
348
|
-
},
|
|
349
|
-
sinkDown: function (n) {
|
|
350
|
-
// Fetch the element that has to be sunk.
|
|
351
|
-
var element = this.content[n];
|
|
352
|
-
|
|
353
|
-
// When at 0, an element can not sink any further.
|
|
354
|
-
while (n > 0) {
|
|
355
|
-
// Compute the parent element's index, and fetch it.
|
|
356
|
-
var parentN = ((n + 1) >> 1) - 1,
|
|
357
|
-
parent = this.content[parentN];
|
|
358
|
-
// Swap the elements if the parent is greater.
|
|
359
|
-
if (this.scoreFunction(element) < this.scoreFunction(parent)) {
|
|
360
|
-
this.content[parentN] = element;
|
|
361
|
-
this.content[n] = parent;
|
|
362
|
-
// Update 'n' to continue at the new position.
|
|
363
|
-
n = parentN;
|
|
364
|
-
// Found a parent that is less, no need to sink any further.
|
|
365
|
-
} else {
|
|
366
|
-
break;
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
},
|
|
370
|
-
bubbleUp: function (n) {
|
|
371
|
-
// Look up the target element and its score.
|
|
372
|
-
var length = this.content.length,
|
|
373
|
-
element = this.content[n],
|
|
374
|
-
elemScore = this.scoreFunction(element);
|
|
375
|
-
|
|
376
|
-
while (true) {
|
|
377
|
-
// Compute the indices of the child elements.
|
|
378
|
-
var child2N = (n + 1) << 1,
|
|
379
|
-
child1N = child2N - 1;
|
|
380
|
-
// This is used to store the new position of the element, if any.
|
|
381
|
-
var swap = null,
|
|
382
|
-
child1Score;
|
|
383
|
-
// If the first child exists (is inside the array)...
|
|
384
|
-
if (child1N < length) {
|
|
385
|
-
// Look it up and compute its score.
|
|
386
|
-
var child1 = this.content[child1N];
|
|
387
|
-
child1Score = this.scoreFunction(child1);
|
|
388
|
-
|
|
389
|
-
// If the score is less than our element's, we need to swap.
|
|
390
|
-
if (child1Score < elemScore) {
|
|
391
|
-
swap = child1N;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
// Do the same checks for the other child.
|
|
396
|
-
if (child2N < length) {
|
|
397
|
-
var child2 = this.content[child2N],
|
|
398
|
-
child2Score = this.scoreFunction(child2);
|
|
399
|
-
if (child2Score < (swap === null ? elemScore : child1Score)) {
|
|
400
|
-
swap = child2N;
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// If the element needs to be moved, swap it, and continue.
|
|
405
|
-
if (swap !== null) {
|
|
406
|
-
this.content[n] = this.content[swap];
|
|
407
|
-
this.content[swap] = element;
|
|
408
|
-
n = swap;
|
|
409
|
-
// Otherwise, we are done.
|
|
410
|
-
} else {
|
|
411
|
-
break;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
},
|
|
415
|
-
};
|
|
416
|
-
|
|
417
|
-
/**
|
|
418
|
-
* Returns the shortest {@link LineString|path} from {@link Point|start} to {@link Point|end} without colliding with
|
|
419
|
-
* any {@link Feature} in {@link FeatureCollection<Polygon>| obstacles}
|
|
420
|
-
*
|
|
421
|
-
* @name shortestPath
|
|
422
|
-
* @param {Coord} start point
|
|
423
|
-
* @param {Coord} end point
|
|
424
|
-
* @param {Object} [options={}] optional parameters
|
|
425
|
-
* @param {Geometry|Feature|FeatureCollection<Polygon>} [options.obstacles] areas which path cannot travel
|
|
426
|
-
* @param {number} [options.minDistance] minimum distance between shortest path and obstacles
|
|
427
|
-
* @param {string} [options.units='kilometers'] unit in which resolution & minimum distance will be expressed in; it can be degrees, radians, miles, kilometers, ...
|
|
428
|
-
* @param {number} [options.resolution=100] distance between matrix points on which the path will be calculated
|
|
429
|
-
* @returns {Feature<LineString>} shortest path between start and end
|
|
430
|
-
* @example
|
|
431
|
-
* var start = [-5, -6];
|
|
432
|
-
* var end = [9, -6];
|
|
433
|
-
* var options = {
|
|
434
|
-
* obstacles: turf.polygon([[[0, -7], [5, -7], [5, -3], [0, -3], [0, -7]]])
|
|
435
|
-
* };
|
|
436
|
-
*
|
|
437
|
-
* var path = turf.shortestPath(start, end, options);
|
|
438
|
-
*
|
|
439
|
-
* //addToMap
|
|
440
|
-
* var addToMap = [start, end, options.obstacles, path];
|
|
441
|
-
*/
|
|
442
|
-
function shortestPath(start, end, options) {
|
|
443
|
-
// Optional parameters
|
|
444
|
-
options = options || {};
|
|
445
|
-
if (!helpers.isObject(options)) throw new Error("options is invalid");
|
|
446
|
-
var resolution = options.resolution;
|
|
447
|
-
var minDistance = options.minDistance;
|
|
448
|
-
var obstacles = options.obstacles || helpers.featureCollection([]);
|
|
449
|
-
|
|
450
|
-
// validation
|
|
451
|
-
if (!start) throw new Error("start is required");
|
|
452
|
-
if (!end) throw new Error("end is required");
|
|
453
|
-
if ((resolution && !helpers.isNumber(resolution)) || resolution <= 0)
|
|
454
|
-
throw new Error("options.resolution must be a number, greater than 0");
|
|
455
|
-
if (minDistance)
|
|
456
|
-
throw new Error("options.minDistance is not yet implemented");
|
|
457
|
-
|
|
458
|
-
// Normalize Inputs
|
|
459
|
-
var startCoord = invariant.getCoord(start);
|
|
460
|
-
var endCoord = invariant.getCoord(end);
|
|
461
|
-
start = helpers.point(startCoord);
|
|
462
|
-
end = helpers.point(endCoord);
|
|
463
|
-
|
|
464
|
-
// Handle obstacles
|
|
465
|
-
switch (invariant.getType(obstacles)) {
|
|
466
|
-
case "FeatureCollection":
|
|
467
|
-
if (obstacles.features.length === 0)
|
|
468
|
-
return helpers.lineString([startCoord, endCoord]);
|
|
469
|
-
break;
|
|
470
|
-
case "Polygon":
|
|
471
|
-
obstacles = helpers.featureCollection([helpers.feature(invariant.getGeom(obstacles))]);
|
|
472
|
-
break;
|
|
473
|
-
default:
|
|
474
|
-
throw new Error("invalid obstacles");
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
// define path grid area
|
|
478
|
-
var collection = obstacles;
|
|
479
|
-
collection.features.push(start);
|
|
480
|
-
collection.features.push(end);
|
|
481
|
-
var box = bbox__default['default'](scale__default['default'](bboxPolygon__default['default'](bbox__default['default'](collection)), 1.15)); // extend 15%
|
|
482
|
-
if (!resolution) {
|
|
483
|
-
var width = distance__default['default']([box[0], box[1]], [box[2], box[1]], options);
|
|
484
|
-
resolution = width / 100;
|
|
485
|
-
}
|
|
486
|
-
collection.features.pop();
|
|
487
|
-
collection.features.pop();
|
|
488
|
-
|
|
489
|
-
var west = box[0];
|
|
490
|
-
var south = box[1];
|
|
491
|
-
var east = box[2];
|
|
492
|
-
var north = box[3];
|
|
493
|
-
|
|
494
|
-
var xFraction = resolution / distance__default['default']([west, south], [east, south], options);
|
|
495
|
-
var cellWidth = xFraction * (east - west);
|
|
496
|
-
var yFraction = resolution / distance__default['default']([west, south], [west, north], options);
|
|
497
|
-
var cellHeight = yFraction * (north - south);
|
|
498
|
-
|
|
499
|
-
var bboxHorizontalSide = east - west;
|
|
500
|
-
var bboxVerticalSide = north - south;
|
|
501
|
-
var columns = Math.floor(bboxHorizontalSide / cellWidth);
|
|
502
|
-
var rows = Math.floor(bboxVerticalSide / cellHeight);
|
|
503
|
-
// adjust origin of the grid
|
|
504
|
-
var deltaX = (bboxHorizontalSide - columns * cellWidth) / 2;
|
|
505
|
-
var deltaY = (bboxVerticalSide - rows * cellHeight) / 2;
|
|
506
|
-
|
|
507
|
-
// loop through points only once to speed up process
|
|
508
|
-
// define matrix grid for A-star algorithm
|
|
509
|
-
var pointMatrix = [];
|
|
510
|
-
var matrix = [];
|
|
511
|
-
|
|
512
|
-
var closestToStart = [];
|
|
513
|
-
var closestToEnd = [];
|
|
514
|
-
var minDistStart = Infinity;
|
|
515
|
-
var minDistEnd = Infinity;
|
|
516
|
-
var currentY = north - deltaY;
|
|
517
|
-
var r = 0;
|
|
518
|
-
while (currentY >= south) {
|
|
519
|
-
// var currentY = south + deltaY;
|
|
520
|
-
var matrixRow = [];
|
|
521
|
-
var pointMatrixRow = [];
|
|
522
|
-
var currentX = west + deltaX;
|
|
523
|
-
var c = 0;
|
|
524
|
-
while (currentX <= east) {
|
|
525
|
-
var pt = helpers.point([currentX, currentY]);
|
|
526
|
-
var isInsideObstacle = isInside(pt, obstacles);
|
|
527
|
-
// feed obstacles matrix
|
|
528
|
-
matrixRow.push(isInsideObstacle ? 0 : 1); // with javascript-astar
|
|
529
|
-
// matrixRow.push(isInsideObstacle ? 1 : 0); // with astar-andrea
|
|
530
|
-
// map point's coords
|
|
531
|
-
pointMatrixRow.push(currentX + "|" + currentY);
|
|
532
|
-
// set closest points
|
|
533
|
-
var distStart = distance__default['default'](pt, start);
|
|
534
|
-
// if (distStart < minDistStart) {
|
|
535
|
-
if (!isInsideObstacle && distStart < minDistStart) {
|
|
536
|
-
minDistStart = distStart;
|
|
537
|
-
closestToStart = { x: c, y: r };
|
|
538
|
-
}
|
|
539
|
-
var distEnd = distance__default['default'](pt, end);
|
|
540
|
-
// if (distEnd < minDistEnd) {
|
|
541
|
-
if (!isInsideObstacle && distEnd < minDistEnd) {
|
|
542
|
-
minDistEnd = distEnd;
|
|
543
|
-
closestToEnd = { x: c, y: r };
|
|
544
|
-
}
|
|
545
|
-
currentX += cellWidth;
|
|
546
|
-
c++;
|
|
547
|
-
}
|
|
548
|
-
matrix.push(matrixRow);
|
|
549
|
-
pointMatrix.push(pointMatrixRow);
|
|
550
|
-
currentY -= cellHeight;
|
|
551
|
-
r++;
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
// find path on matrix grid
|
|
555
|
-
|
|
556
|
-
// javascript-astar ----------------------
|
|
557
|
-
var graph = new Graph(matrix, { diagonal: true });
|
|
558
|
-
var startOnMatrix = graph.grid[closestToStart.y][closestToStart.x];
|
|
559
|
-
var endOnMatrix = graph.grid[closestToEnd.y][closestToEnd.x];
|
|
560
|
-
var result = astar.search(graph, startOnMatrix, endOnMatrix);
|
|
561
|
-
|
|
562
|
-
var path = [startCoord];
|
|
563
|
-
result.forEach(function (coord) {
|
|
564
|
-
var coords = pointMatrix[coord.x][coord.y].split("|");
|
|
565
|
-
path.push([+coords[0], +coords[1]]); // make sure coords are numbers
|
|
566
|
-
});
|
|
567
|
-
path.push(endCoord);
|
|
568
|
-
// ---------------------------------------
|
|
569
|
-
|
|
570
|
-
// astar-andrea ------------------------
|
|
571
|
-
// var result = aStar(matrix, [closestToStart.x, closestToStart.y], [closestToEnd.x, closestToEnd.y], 'DiagonalFree');
|
|
572
|
-
// var path = [start.geometry.coordinates];
|
|
573
|
-
// result.forEach(function (coord) {
|
|
574
|
-
// var coords = pointMatrix[coord[1]][coord[0]].split('|');
|
|
575
|
-
// path.push([+coords[0], +coords[1]]); // make sure coords are numbers
|
|
576
|
-
// });
|
|
577
|
-
// path.push(end.geometry.coordinates);
|
|
578
|
-
// ---------------------------------------
|
|
579
|
-
|
|
580
|
-
return cleanCoords__default['default'](helpers.lineString(path));
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
/**
|
|
584
|
-
* Checks if Point is inside any of the Polygons
|
|
585
|
-
*
|
|
586
|
-
* @private
|
|
587
|
-
* @param {Feature<Point>} pt to check
|
|
588
|
-
* @param {FeatureCollection<Polygon>} polygons features
|
|
589
|
-
* @returns {boolean} if inside or not
|
|
590
|
-
*/
|
|
591
|
-
function isInside(pt, polygons) {
|
|
592
|
-
for (var i = 0; i < polygons.features.length; i++) {
|
|
593
|
-
if (booleanPointInPolygon__default['default'](pt, polygons.features[i])) {
|
|
594
|
-
return true;
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
return false;
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
module.exports = shortestPath;
|
|
601
|
-
module.exports.default = shortestPath;
|
package/index.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Polygon, Feature, FeatureCollection, LineString } from "geojson";
|
|
2
|
-
import { Coord, Units } from "@turf/helpers";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* http://turfjs.org/docs/#shortestpath
|
|
6
|
-
*/
|
|
7
|
-
export default function shortestPath(
|
|
8
|
-
start: Coord,
|
|
9
|
-
end: Coord,
|
|
10
|
-
options?: {
|
|
11
|
-
obstacles?: Polygon | Feature<Polygon> | FeatureCollection<Polygon>;
|
|
12
|
-
minDistance?: number;
|
|
13
|
-
units?: Units;
|
|
14
|
-
resolution?: number;
|
|
15
|
-
}
|
|
16
|
-
): Feature<LineString>;
|