Skip to content

Commit ad10a65

Browse files
@turf/shortest-path improve creation of point matrix for A* algorithm (#2943) (#2944)
* @turf/shortest-path improve creation of point matrix for A* algorithm (#2943) * Use 6 decimal digits as precision for test data --------- Co-authored-by: James Beard <[email protected]>
1 parent 9edc247 commit ad10a65

File tree

11 files changed

+911
-70
lines changed

11 files changed

+911
-70
lines changed

packages/turf-shortest-path/index.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,20 @@ function shortestPath(
9999
const box = bbox(scale(bboxPolygon(bbox(collection)), 1.15)); // extend 15%
100100
const [west, south, east, north] = box;
101101

102-
const width = distance([west, south], [east, south], options);
103-
const division = width / resolution;
104-
105102
collection.features.pop();
106103
collection.features.pop();
107104

108-
const xFraction = division / distance([west, south], [east, south], options);
109-
const cellWidth = xFraction * (east - west);
110-
const yFraction = division / distance([west, south], [west, north], options);
111-
const cellHeight = yFraction * (north - south);
105+
const columnsWithFraction =
106+
distance([west, south], [east, south], options) / resolution;
107+
const cellWidth = (east - west) / columnsWithFraction;
108+
109+
const rowsWithFraction =
110+
distance([west, south], [west, north], options) / resolution;
111+
const cellHeight = (north - south) / rowsWithFraction;
112112

113-
const bboxHorizontalSide = east - west;
114-
const bboxVerticalSide = north - south;
115-
const columns = Math.floor(bboxHorizontalSide / cellWidth);
116-
const rows = Math.floor(bboxVerticalSide / cellHeight);
117113
// adjust origin of the grid
118-
const deltaX = (bboxHorizontalSide - columns * cellWidth) / 2;
119-
const deltaY = (bboxVerticalSide - rows * cellHeight) / 2;
114+
const deltaX = ((columnsWithFraction % 1) * cellWidth) / 2;
115+
const deltaY = ((rowsWithFraction % 1) * cellHeight) / 2;
120116

121117
// loop through points only once to speed up process
122118
// define matrix grid for A-star algorithm

packages/turf-shortest-path/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"author": "Turf Authors",
66
"contributors": [
77
"Stefano Borghi <@stebogit>",
8-
"Denis Carriere <@DenisCarriere>"
8+
"Denis Carriere <@DenisCarriere>",
9+
"Daniel Ziegler <@ziegler-daniel>"
910
],
1011
"license": "MIT",
1112
"bugs": {

packages/turf-shortest-path/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ test("turf-shortest-path -- with polygon feature as obstacle", (t) => {
6666

6767
const path = truncate(
6868
shortestPath(start, end, {
69+
...geojson.properties,
6970
obstacles: obstacle,
7071
})
7172
);

packages/turf-shortest-path/test/in/bermuda-triangle.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"type": "FeatureCollection",
3-
"properties": {},
3+
"properties": {
4+
"resolution": 20,
5+
"units": "kilometers"
6+
},
47
"features": [
58
{
69
"type": "Feature",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"type": "FeatureCollection",
3+
"properties": {
4+
"resolution": 10,
5+
"units": "meters"
6+
},
7+
"features": [
8+
{
9+
"type": "Feature",
10+
"properties": {
11+
"name": "start"
12+
},
13+
"geometry": {
14+
"type": "Point",
15+
"coordinates": [13.811772, 52.151878]
16+
}
17+
},
18+
{
19+
"type": "Feature",
20+
"properties": {
21+
"name": "end"
22+
},
23+
"geometry": {
24+
"type": "Point",
25+
"coordinates": [13.807807, 52.157116]
26+
}
27+
},
28+
{
29+
"type": "Feature",
30+
"properties": {
31+
"name": "obstacle"
32+
},
33+
"geometry": {
34+
"type": "Polygon",
35+
"coordinates": [
36+
[
37+
[13.809135, 52.153309],
38+
[13.809135, 52.152021],
39+
[13.813001, 52.152021],
40+
[13.813001, 52.153309],
41+
[13.809135, 52.153309]
42+
]
43+
]
44+
}
45+
}
46+
]
47+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"type": "FeatureCollection",
3+
"properties": {
4+
"resolution": 10,
5+
"units": "centimeters"
6+
},
7+
"features": [
8+
{
9+
"type": "Feature",
10+
"properties": {
11+
"type": "start"
12+
},
13+
"geometry": {
14+
"type": "Point",
15+
"coordinates": [-0.158503, 51.50751]
16+
}
17+
},
18+
{
19+
"type": "Feature",
20+
"properties": {
21+
"type": "end"
22+
},
23+
"geometry": {
24+
"type": "Point",
25+
"coordinates": [-0.156938, 51.50724]
26+
}
27+
},
28+
{
29+
"type": "Feature",
30+
"properties": {
31+
"name": "obstacle 1"
32+
},
33+
"geometry": {
34+
"type": "Polygon",
35+
"coordinates": [
36+
[
37+
[-0.158656, 51.507437],
38+
[-0.157762, 51.506957],
39+
[-0.157403, 51.507497],
40+
[-0.158402, 51.507721],
41+
[-0.158385, 51.507443],
42+
[-0.158656, 51.507437]
43+
]
44+
]
45+
}
46+
},
47+
{
48+
"type": "Feature",
49+
"properties": {
50+
"name": "obstacle 2"
51+
},
52+
"geometry": {
53+
"type": "Polygon",
54+
"coordinates": [
55+
[
56+
[-0.157216, 51.507531],
57+
[-0.157216, 51.507311],
58+
[-0.156869, 51.507311],
59+
[-0.156869, 51.507531],
60+
[-0.157216, 51.507531]
61+
]
62+
]
63+
}
64+
},
65+
{
66+
"type": "Feature",
67+
"properties": {
68+
"name": "obstacle 3"
69+
},
70+
"geometry": {
71+
"type": "Polygon",
72+
"coordinates": [
73+
[
74+
[-0.157414, 51.507373],
75+
[-0.157527, 51.507013],
76+
[-0.157171, 51.506986],
77+
[-0.156902, 51.507049],
78+
[-0.157192, 51.507232],
79+
[-0.157414, 51.507373]
80+
]
81+
]
82+
}
83+
},
84+
{
85+
"type": "Feature",
86+
"properties": {
87+
"name": "obstacle 4"
88+
},
89+
"geometry": {
90+
"type": "Polygon",
91+
"coordinates": [
92+
[
93+
[-0.15781, 51.507865],
94+
[-0.157319, 51.507501],
95+
[-0.157231, 51.507728],
96+
[-0.15781, 51.507865]
97+
]
98+
]
99+
}
100+
}
101+
]
102+
}

packages/turf-shortest-path/test/in/simple.json

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"type": "FeatureCollection",
3-
"properties": {},
3+
"properties": {
4+
"resolution": 10,
5+
"units": "kilometers"
6+
},
47
"features": [
58
{
69
"type": "Feature",
@@ -29,16 +32,16 @@
2932
"type": "Polygon",
3033
"coordinates": [
3134
[
32-
[8.206787109375, 40.730608477796636],
33-
[8.5693359375, 39.8928799002948],
34-
[8.349609375, 39.257778150283364],
35-
[8.843994140625, 38.90813299596705],
36-
[9.085693359375, 39.257778150283364],
37-
[9.591064453125, 39.16414104768742],
38-
[9.591064453125, 40.271143686084194],
39-
[9.82177734375, 40.47202439692057],
40-
[9.283447265625, 41.253032440653186],
41-
[8.206787109375, 40.730608477796636]
35+
[8.206787, 40.730608],
36+
[8.569335, 39.892879],
37+
[8.349609, 39.257778],
38+
[8.843994, 38.908132],
39+
[9.085693, 39.257778],
40+
[9.591064, 39.164141],
41+
[9.591064, 40.271143],
42+
[9.821777, 40.472024],
43+
[9.283447, 41.253032],
44+
[8.206787, 40.730608]
4245
]
4346
]
4447
}

packages/turf-shortest-path/test/out/bermuda-triangle.json

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,39 @@
4646
"type": "LineString",
4747
"coordinates": [
4848
[-60.925, 22.335],
49-
[-61.090854, 22.526114],
50-
[-62.427321, 23.800508],
51-
[-62.427321, 29.960077],
52-
[-64.209278, 31.659269],
53-
[-64.877512, 32.296466],
54-
[-65.100257, 32.296466],
55-
[-65.323001, 32.084067],
56-
[-65.768491, 32.084067],
57-
[-65.991235, 31.871668],
58-
[-66.21398, 31.871668],
59-
[-66.436725, 31.659269],
60-
[-66.659469, 31.659269],
61-
[-66.882214, 31.44687],
62-
[-67.104958, 31.44687],
63-
[-67.327703, 31.234471],
64-
[-67.773192, 31.234471],
65-
[-67.995937, 31.022072],
66-
[-68.218681, 31.022072],
67-
[-68.441426, 30.809673],
68-
[-68.664171, 30.809673],
69-
[-68.886915, 30.597274],
70-
[-69.10966, 30.597274],
71-
[-69.332405, 30.384875],
72-
[-69.777894, 30.384875],
73-
[-70.000638, 30.172476],
74-
[-70.223383, 30.172476],
75-
[-70.446128, 29.960077],
76-
[-80.024146, 29.960077],
49+
[-61.126249, 22.515675],
50+
[-62.257999, 23.594859],
51+
[-62.257999, 29.890102],
52+
[-64.521499, 32.048471],
53+
[-64.898749, 32.408199],
54+
[-65.087374, 32.228335],
55+
[-65.275999, 32.228335],
56+
[-65.464624, 32.048471],
57+
[-65.653249, 32.048471],
58+
[-65.841874, 31.868607],
59+
[-66.219125, 31.868607],
60+
[-66.40775, 31.688743],
61+
[-66.596375, 31.688743],
62+
[-66.785, 31.508878],
63+
[-66.973625, 31.508878],
64+
[-67.16225, 31.329014],
65+
[-67.350875, 31.329014],
66+
[-67.5395, 31.14915],
67+
[-67.91675, 31.14915],
68+
[-68.105375, 30.969286],
69+
[-68.294, 30.969286],
70+
[-68.482625, 30.789422],
71+
[-68.67125, 30.789422],
72+
[-68.859875, 30.609558],
73+
[-69.0485, 30.609558],
74+
[-69.237125, 30.429694],
75+
[-69.614375, 30.429694],
76+
[-69.803, 30.24983],
77+
[-69.991625, 30.24983],
78+
[-70.18025, 30.069966],
79+
[-70.368875, 30.069966],
80+
[-70.5575, 29.890102],
81+
[-79.988751, 29.890102],
7782
[-79.991, 29.965]
7883
]
7984
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {
7+
"name": "obstacle"
8+
},
9+
"geometry": {
10+
"type": "Polygon",
11+
"coordinates": [
12+
[
13+
[13.809135, 52.153309],
14+
[13.809135, 52.152021],
15+
[13.813001, 52.152021],
16+
[13.813001, 52.153309],
17+
[13.809135, 52.153309]
18+
]
19+
]
20+
}
21+
},
22+
{
23+
"type": "Feature",
24+
"properties": {
25+
"name": "start"
26+
},
27+
"geometry": {
28+
"type": "Point",
29+
"coordinates": [13.811772, 52.151878]
30+
}
31+
},
32+
{
33+
"type": "Feature",
34+
"properties": {
35+
"name": "end"
36+
},
37+
"geometry": {
38+
"type": "Point",
39+
"coordinates": [13.807807, 52.157116]
40+
}
41+
},
42+
{
43+
"type": "Feature",
44+
"properties": {
45+
"stroke": "#F00",
46+
"stroke-width": 5
47+
},
48+
"geometry": {
49+
"type": "LineString",
50+
"coordinates": [
51+
[13.811772, 52.151878],
52+
[13.811577, 52.151979],
53+
[13.809231, 52.151979],
54+
[13.807766, 52.152878],
55+
[13.807766, 52.157105],
56+
[13.807807, 52.157116]
57+
]
58+
}
59+
}
60+
]
61+
}

0 commit comments

Comments
 (0)