Skip to content

Commit b675b39

Browse files
@turf/shortest-path improve creation of point matrix for A* algorithm (#2943)
1 parent 8ccda4a commit b675b39

File tree

10 files changed

+890
-50
lines changed

10 files changed

+890
-50
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/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.811772324108205, 52.15187842149308]
16+
}
17+
},
18+
{
19+
"type": "Feature",
20+
"properties": {
21+
"name": "end"
22+
},
23+
"geometry": {
24+
"type": "Point",
25+
"coordinates": [13.80780718427863, 52.15711694225445]
26+
}
27+
},
28+
{
29+
"type": "Feature",
30+
"properties": {
31+
"name": "obstacle"
32+
},
33+
"geometry": {
34+
"type": "Polygon",
35+
"coordinates": [
36+
[
37+
[13.809135109049322, 52.1533094692154],
38+
[13.809135109049322, 52.152020775802356],
39+
[13.8130016727888, 52.152020775802356],
40+
[13.8130016727888, 52.1533094692154],
41+
[13.809135109049322, 52.1533094692154]
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.15850315869209908, 51.50751033097521]
16+
}
17+
},
18+
{
19+
"type": "Feature",
20+
"properties": {
21+
"type": "end"
22+
},
23+
"geometry": {
24+
"type": "Point",
25+
"coordinates": [-0.1569383441422758, 51.50724018084421]
26+
}
27+
},
28+
{
29+
"type": "Feature",
30+
"properties": {
31+
"name": "obstacle 1"
32+
},
33+
"geometry": {
34+
"type": "Polygon",
35+
"coordinates": [
36+
[
37+
[-0.15865694501789562, 51.50743756754056],
38+
[-0.15776266547817386, 51.50695735138396],
39+
[-0.1574032001721264, 51.507497594204295],
40+
[-0.15840268907001587, 51.50772132925411],
41+
[-0.15838515417763688, 51.507443024513464],
42+
[-0.15865694501789562, 51.50743756754056]
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.1572162795729355, 51.507531658549084],
57+
[-0.1572162795729355, 51.50731127313941],
58+
[-0.15686981211763396, 51.50731127313941],
59+
[-0.15686981211763396, 51.507531658549084],
60+
[-0.1572162795729355, 51.507531658549084]
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.1574146287955216, 51.50737390306023],
75+
[-0.1575275779884464, 51.50701391127427],
76+
[-0.15717120381211203, 51.506986033003926],
77+
[-0.15690246263028484, 51.507049062112884],
78+
[-0.15719262521125188, 51.50723208845383],
79+
[-0.1574146287955216, 51.50737390306023]
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.1578102237825476, 51.507865789233904],
94+
[-0.15731907760937247, 51.50750085205033],
95+
[-0.1572315089122469, 51.50772834570279],
96+
[-0.1578102237825476, 51.507865789233904]
97+
]
98+
]
99+
}
100+
}
101+
]
102+
}

packages/turf-shortest-path/test/in/simple.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": 10,
5+
"units": "kilometers"
6+
},
47
"features": [
58
{
69
"type": "Feature",

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.809135109049322, 52.1533094692154],
14+
[13.809135109049322, 52.152020775802356],
15+
[13.8130016727888, 52.152020775802356],
16+
[13.8130016727888, 52.1533094692154],
17+
[13.809135109049322, 52.1533094692154]
18+
]
19+
]
20+
}
21+
},
22+
{
23+
"type": "Feature",
24+
"properties": {
25+
"name": "start"
26+
},
27+
"geometry": {
28+
"type": "Point",
29+
"coordinates": [13.811772324108205, 52.15187842149308]
30+
}
31+
},
32+
{
33+
"type": "Feature",
34+
"properties": {
35+
"name": "end"
36+
},
37+
"geometry": {
38+
"type": "Point",
39+
"coordinates": [13.80780718427863, 52.15711694225445]
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.15198],
53+
[13.809232, 52.15198],
54+
[13.807766, 52.152879],
55+
[13.807766, 52.157106],
56+
[13.807807, 52.157117]
57+
]
58+
}
59+
}
60+
]
61+
}

0 commit comments

Comments
 (0)