Skip to content

Commit e352195

Browse files
Merge pull request #2942 from ziegler-daniel/bugfix/issue-2941
@turf/shortest-path Allow polygon feature as obstacle for shortestPath (#2941)
2 parents e09b4df + f3fce80 commit e352195

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

packages/turf-shortest-path/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ function shortestPath(
8181
if (obstacles.features.length === 0) {
8282
return lineString([startCoord, endCoord]);
8383
}
84+
} else if (
85+
obstacles.type === "Feature" &&
86+
obstacles.geometry.type === "Polygon"
87+
) {
88+
obstacles = featureCollection([obstacles]);
8489
} else if (obstacles.type === "Polygon") {
8590
obstacles = featureCollection([feature(getGeom(obstacles))]);
8691
} else {

packages/turf-shortest-path/test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,29 @@ test("turf-shortest-path", (t) => {
5555
}
5656
t.end();
5757
});
58+
59+
test("turf-shortest-path -- with polygon feature as obstacle", (t) => {
60+
const filename = "simple.json";
61+
const geojson = loadJsonFileSync(directories.in + filename);
62+
63+
const start = geojson.features.shift();
64+
const end = geojson.features.shift();
65+
const obstacle = geojson.features.shift();
66+
67+
const path = truncate(
68+
shortestPath(start, end, {
69+
obstacles: obstacle,
70+
})
71+
);
72+
path.properties["stroke"] = "#F00";
73+
path.properties["stroke-width"] = 5;
74+
75+
const results = featureCollection([]);
76+
results.features.push(obstacle);
77+
results.features.push(point(getCoord(start), start.properties));
78+
results.features.push(point(getCoord(end), end.properties));
79+
results.features.push(path);
80+
81+
t.deepEqual(results, loadJsonFileSync(directories.out + filename), "simple");
82+
t.end();
83+
});

packages/turf-shortest-path/types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
import { point, featureCollection, polygon } from "@turf/helpers";
1+
import { point, feature, featureCollection, polygon } from "@turf/helpers";
22
import { shortestPath } from "./index.js";
33

44
const start = point([-5, -6]);
55
const end = point([9, -6]);
66

77
shortestPath(start.geometry, end.geometry.coordinates);
88
shortestPath(start, end);
9+
shortestPath(start, end, {
10+
obstacles: polygon([
11+
[
12+
[0, -7],
13+
[5, -7],
14+
[5, -3],
15+
[0, -3],
16+
[0, -7],
17+
],
18+
]).geometry,
19+
});
920
shortestPath(start, end, {
1021
obstacles: polygon([
1122
[

0 commit comments

Comments
 (0)