File tree Expand file tree Collapse file tree 3 files changed +43
-1
lines changed
packages/turf-shortest-path Expand file tree Collapse file tree 3 files changed +43
-1
lines changed Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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+ } ) ;
Original file line number Diff line number Diff line change 1- import { point , featureCollection , polygon } from "@turf/helpers" ;
1+ import { point , feature , featureCollection , polygon } from "@turf/helpers" ;
22import { shortestPath } from "./index.js" ;
33
44const start = point ( [ - 5 , - 6 ] ) ;
55const end = point ( [ 9 , - 6 ] ) ;
66
77shortestPath ( start . geometry , end . geometry . coordinates ) ;
88shortestPath ( 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+ } ) ;
920shortestPath ( start , end , {
1021 obstacles : polygon ( [
1122 [
You can’t perform that action at this time.
0 commit comments