Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions packages/turf-shortest-path/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,20 @@ function shortestPath(
const box = bbox(scale(bboxPolygon(bbox(collection)), 1.15)); // extend 15%
const [west, south, east, north] = box;

const width = distance([west, south], [east, south], options);
const division = width / resolution;

collection.features.pop();
collection.features.pop();

const xFraction = division / distance([west, south], [east, south], options);
const cellWidth = xFraction * (east - west);
const yFraction = division / distance([west, south], [west, north], options);
const cellHeight = yFraction * (north - south);
const columnsWithFraction =
distance([west, south], [east, south], options) / resolution;
const cellWidth = (east - west) / columnsWithFraction;

const rowsWithFraction =
distance([west, south], [west, north], options) / resolution;
const cellHeight = (north - south) / rowsWithFraction;

const bboxHorizontalSide = east - west;
const bboxVerticalSide = north - south;
const columns = Math.floor(bboxHorizontalSide / cellWidth);
const rows = Math.floor(bboxVerticalSide / cellHeight);
// adjust origin of the grid
const deltaX = (bboxHorizontalSide - columns * cellWidth) / 2;
const deltaY = (bboxVerticalSide - rows * cellHeight) / 2;
const deltaX = ((columnsWithFraction % 1) * cellWidth) / 2;
const deltaY = ((rowsWithFraction % 1) * cellHeight) / 2;

// loop through points only once to speed up process
// define matrix grid for A-star algorithm
Expand Down
3 changes: 2 additions & 1 deletion packages/turf-shortest-path/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"author": "Turf Authors",
"contributors": [
"Stefano Borghi <@stebogit>",
"Denis Carriere <@DenisCarriere>"
"Denis Carriere <@DenisCarriere>",
"Daniel Ziegler <@ziegler-daniel>"
],
"license": "MIT",
"bugs": {
Expand Down
1 change: 1 addition & 0 deletions packages/turf-shortest-path/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ test("turf-shortest-path -- with polygon feature as obstacle", (t) => {

const path = truncate(
shortestPath(start, end, {
...geojson.properties,
obstacles: obstacle,
})
);
Expand Down
5 changes: 4 additions & 1 deletion packages/turf-shortest-path/test/in/bermuda-triangle.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"type": "FeatureCollection",
"properties": {},
"properties": {
"resolution": 20,
"units": "kilometers"
},
"features": [
{
"type": "Feature",
Expand Down
47 changes: 47 additions & 0 deletions packages/turf-shortest-path/test/in/farmland.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"type": "FeatureCollection",
"properties": {
"resolution": 10,
"units": "meters"
},
"features": [
{
"type": "Feature",
"properties": {
"name": "start"
},
"geometry": {
"type": "Point",
"coordinates": [13.811772, 52.151878]
}
},
{
"type": "Feature",
"properties": {
"name": "end"
},
"geometry": {
"type": "Point",
"coordinates": [13.807807, 52.157116]
}
},
{
"type": "Feature",
"properties": {
"name": "obstacle"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[13.809135, 52.153309],
[13.809135, 52.152021],
[13.813001, 52.152021],
[13.813001, 52.153309],
[13.809135, 52.153309]
]
]
}
}
]
}
102 changes: 102 additions & 0 deletions packages/turf-shortest-path/test/in/multiple-obstacles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"type": "FeatureCollection",
"properties": {
"resolution": 10,
"units": "centimeters"
},
"features": [
{
"type": "Feature",
"properties": {
"type": "start"
},
"geometry": {
"type": "Point",
"coordinates": [-0.158503, 51.50751]
}
},
{
"type": "Feature",
"properties": {
"type": "end"
},
"geometry": {
"type": "Point",
"coordinates": [-0.156938, 51.50724]
}
},
{
"type": "Feature",
"properties": {
"name": "obstacle 1"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-0.158656, 51.507437],
[-0.157762, 51.506957],
[-0.157403, 51.507497],
[-0.158402, 51.507721],
[-0.158385, 51.507443],
[-0.158656, 51.507437]
]
]
}
},
{
"type": "Feature",
"properties": {
"name": "obstacle 2"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-0.157216, 51.507531],
[-0.157216, 51.507311],
[-0.156869, 51.507311],
[-0.156869, 51.507531],
[-0.157216, 51.507531]
]
]
}
},
{
"type": "Feature",
"properties": {
"name": "obstacle 3"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-0.157414, 51.507373],
[-0.157527, 51.507013],
[-0.157171, 51.506986],
[-0.156902, 51.507049],
[-0.157192, 51.507232],
[-0.157414, 51.507373]
]
]
}
},
{
"type": "Feature",
"properties": {
"name": "obstacle 4"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-0.15781, 51.507865],
[-0.157319, 51.507501],
[-0.157231, 51.507728],
[-0.15781, 51.507865]
]
]
}
}
]
}
25 changes: 14 additions & 11 deletions packages/turf-shortest-path/test/in/simple.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"type": "FeatureCollection",
"properties": {},
"properties": {
"resolution": 10,
"units": "kilometers"
},
"features": [
{
"type": "Feature",
Expand Down Expand Up @@ -29,16 +32,16 @@
"type": "Polygon",
"coordinates": [
[
[8.206787109375, 40.730608477796636],
[8.5693359375, 39.8928799002948],
[8.349609375, 39.257778150283364],
[8.843994140625, 38.90813299596705],
[9.085693359375, 39.257778150283364],
[9.591064453125, 39.16414104768742],
[9.591064453125, 40.271143686084194],
[9.82177734375, 40.47202439692057],
[9.283447265625, 41.253032440653186],
[8.206787109375, 40.730608477796636]
[8.206787, 40.730608],
[8.569335, 39.892879],
[8.349609, 39.257778],
[8.843994, 38.908132],
[9.085693, 39.257778],
[9.591064, 39.164141],
[9.591064, 40.271143],
[9.821777, 40.472024],
[9.283447, 41.253032],
[8.206787, 40.730608]
]
]
}
Expand Down
61 changes: 33 additions & 28 deletions packages/turf-shortest-path/test/out/bermuda-triangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,39 @@
"type": "LineString",
"coordinates": [
[-60.925, 22.335],
[-61.090854, 22.526114],
[-62.427321, 23.800508],
[-62.427321, 29.960077],
[-64.209278, 31.659269],
[-64.877512, 32.296466],
[-65.100257, 32.296466],
[-65.323001, 32.084067],
[-65.768491, 32.084067],
[-65.991235, 31.871668],
[-66.21398, 31.871668],
[-66.436725, 31.659269],
[-66.659469, 31.659269],
[-66.882214, 31.44687],
[-67.104958, 31.44687],
[-67.327703, 31.234471],
[-67.773192, 31.234471],
[-67.995937, 31.022072],
[-68.218681, 31.022072],
[-68.441426, 30.809673],
[-68.664171, 30.809673],
[-68.886915, 30.597274],
[-69.10966, 30.597274],
[-69.332405, 30.384875],
[-69.777894, 30.384875],
[-70.000638, 30.172476],
[-70.223383, 30.172476],
[-70.446128, 29.960077],
[-80.024146, 29.960077],
[-61.126249, 22.515675],
[-62.257999, 23.594859],
[-62.257999, 29.890102],
[-64.521499, 32.048471],
[-64.898749, 32.408199],
[-65.087374, 32.228335],
[-65.275999, 32.228335],
[-65.464624, 32.048471],
[-65.653249, 32.048471],
[-65.841874, 31.868607],
[-66.219125, 31.868607],
[-66.40775, 31.688743],
[-66.596375, 31.688743],
[-66.785, 31.508878],
[-66.973625, 31.508878],
[-67.16225, 31.329014],
[-67.350875, 31.329014],
[-67.5395, 31.14915],
[-67.91675, 31.14915],
[-68.105375, 30.969286],
[-68.294, 30.969286],
[-68.482625, 30.789422],
[-68.67125, 30.789422],
[-68.859875, 30.609558],
[-69.0485, 30.609558],
[-69.237125, 30.429694],
[-69.614375, 30.429694],
[-69.803, 30.24983],
[-69.991625, 30.24983],
[-70.18025, 30.069966],
[-70.368875, 30.069966],
[-70.5575, 29.890102],
[-79.988751, 29.890102],
[-79.991, 29.965]
]
}
Expand Down
61 changes: 61 additions & 0 deletions packages/turf-shortest-path/test/out/farmland.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "obstacle"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[13.809135, 52.153309],
[13.809135, 52.152021],
[13.813001, 52.152021],
[13.813001, 52.153309],
[13.809135, 52.153309]
]
]
}
},
{
"type": "Feature",
"properties": {
"name": "start"
},
"geometry": {
"type": "Point",
"coordinates": [13.811772, 52.151878]
}
},
{
"type": "Feature",
"properties": {
"name": "end"
},
"geometry": {
"type": "Point",
"coordinates": [13.807807, 52.157116]
}
},
{
"type": "Feature",
"properties": {
"stroke": "#F00",
"stroke-width": 5
},
"geometry": {
"type": "LineString",
"coordinates": [
[13.811772, 52.151878],
[13.811577, 52.151979],
[13.809231, 52.151979],
[13.807766, 52.152878],
[13.807766, 52.157105],
[13.807807, 52.157116]
]
}
}
]
}
Loading