forked from jpatokal/openflights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
greatcircle.php
218 lines (190 loc) · 6.21 KB
/
greatcircle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
$PI = 3.1415926;
$EARTH_RADIUS = 3958.75; // in miles
$EARTH_CIRCUMFERENCE = 24900; // in miles
$MOON_DISTANCE = 238857; // in miles
$MARS_DISTANCE = 34649589; // in miles
$DEG2RAD = 0.01745329252; // factor to convert degrees to radians (PI/180)
$RAD2DEG = 57.29577951308;
$GC_STEP = 100; // draw segment every GC_STEP mi
$GC_MIN = 1000; // trigger GC paths once distance is greater than this
$ASCENT_STEP = 1; // compute ascent path every ASCENT_STEP miles
$ASCENT_SPEED = 100; // meters per ASCENT_STEP (from sealevel)
$CRUISE_ALTITUDE = 10000; // cruising altitude in meters
$MAGIC_NUMBER = 19; // number of steps needed for the landing curve (integral of y(n)=y(n-1)+n?)
//Compute great circle distance from "from" to "to"
function gcPointDistance($from, $to) {
$lon1 = $from["x"];
$lat1 = $from["y"];
$lon2 = $to["x"];
$lat2 = $to["y"];
// Eliminate one trivial case...
if($lon1 == $lon2 && $lat1 == $lat2) {
return 0;
}
$pi = 3.1415926;
$rad = doubleval($pi/180.0);
$lon1 = doubleval($lon1)*$rad; $lat1 = doubleval($lat1)*$rad;
$lon2 = doubleval($lon2)*$rad; $lat2 = doubleval($lat2)*$rad;
$theta = $lon2 - $lon1;
$dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta));
if ($dist < 0) { $dist += $pi; }
return floor($dist * 6371.2 * 0.621);
}
// Compute great circle bearing from point "from" towards point "to"
function gcBearingTo($from, $to) {
global $PI, $DEG2RAD, $RAD2DEG;
$x1 = $from["x"] * $DEG2RAD;
$y1 = $from["y"] * $DEG2RAD;
$x2 = $to["x"] * $DEG2RAD;
$y2 = $to["y"] * $DEG2RAD;
$a = cos($y2) * sin($x2 - $x1);
$b = cos($y1) * sin($y2) - sin($y1) * cos($y2) * cos($x2 - $x1);
if(($a == 0) && ($b == 0)) {
$bearing = 0;
return $bearing;
}
if($b == 0) {
if($a < 0)
$bearing = 270;
else
$bearing = 90;
return $bearing;
}
if( $b < 0)
$adjust = $PI;
else {
if($a < 0)
$adjust = 2 * $PI;
else
$adjust = 0;
}
$bearing = (atan($a/$b) + $adjust) * $RAD2DEG;
return $bearing;
}
/**
* Compute great circle waypoint "distance" miles away from "from" in direction "bearing"
*/
function gcWaypoint($from, $distance, $bearing) {
global $DEG2RAD, $RAD2DEG, $EARTH_RADIUS;
// Math.* trig functions require angles to be in radians
$x = $from["x"] * $DEG2RAD;
$y = $from["y"] * $DEG2RAD;
$radBearing = $bearing * $DEG2RAD;
// Convert arc distance to radians
$d = $distance / $EARTH_RADIUS;
// Modified based on http://williams.best.vwh.net/avform.htm
$lat = asin( sin($y) * cos($d) + cos($y) * sin($d) * cos($radBearing));
$lon = atan2( sin($radBearing) * sin($d) * cos($y), cos($d) - sin($y) * sin($lat));
$x = ($x + $lon) * $RAD2DEG;
$y = $lat * $RAD2DEG;
return array("x" => $x, "y" => $y);
}
function straightPath($startPoint, $endPoint) {
// Do we cross the dateline? If yes, then flip endPoint across it
if(abs($startPoint["x"]-$endPoint["x"]) > 180) {
if($startPoint["x"] < $endPoint["x"]) {
$endPoint["x"] -= 360;
} else {
$endPoint["x"] += 360;
}
}
return array($startPoint, $endPoint);
}
// If $threed = true, plot path in three dimensions (x,y,z), else only two (x,y)
function gcPath($startPoint, $endPoint, $distance, $threed) {
global $GC_STEP, $ASCENT_STEP, $ASCENT_SPEED, $MAGIC_NUMBER, $CRUISE_ALTITUDE;
$pointList = array();
$pointList[] = $startPoint;
$wayPoint = $startPoint;
$distance = gcPointDistance($startPoint, $endPoint);
$elevation = 0;
if($threed) {
$step = $ASCENT_STEP;
$elevation = $startPoint["z"];
$delta = 1; // Ascending
// Calculate distance at which to start descent
$ascentspeed = ($CRUISE_ALTITUDE - $startPoint["z"]) / $ASCENT_SPEED;
$descentspeed = ($CRUISE_ALTITUDE - $endPoint["z"]) / $ASCENT_SPEED;
$descentpoint = $distance - ($MAGIC_NUMBER * $ASCENT_STEP);
if($descentpoint < $distance / 2) {
$descentpoint = $distance / 2;
}
} else {
$step = $GC_STEP;
}
$d = 0;
// And... action!
while($d + 1 < $distance) {
//printf("%s of %s: from %s,%s now at %s,%s step %s bearing %s target %s,%s\n", $d, $distance, $startPoint["x"], $startPoint["y"], $step, $bearing, $wayPoint["x"], $wayPoint["y"], $endPoint["x"], $endPoint["y"]);
// Cruising, but increase step resolution near the poles
if($threed && $delta != 0) {
$step = $ASCENT_STEP;
} else {
if(abs($wayPoint["y"]) > 60) {
$step = $GC_STEP / 2;
} else {
$step = $GC_STEP;
}
}
if($threed) {
// Ascending
if($delta > 0) {
if($elevation < ($CRUISE_ALTITUDE - $startPoint["z"]) / 2 + $startPoint["z"]) {
$delta += $ascentspeed;
} else {
$delta -= $ascentspeed;
}
if($elevation >= $CRUISE_ALTITUDE || $delta < 0) {
$elevation = $CRUISE_ALTITUDE;
$delta = 0;
}
}
// Descending
if($d + $step >= $descentpoint) {
if($d >= $descentpoint) {
if($elevation > ($CRUISE_ALTITUDE - $endPoint["z"]) / 2 + $endPoint["z"]) {
$delta -= $descentspeed;
} else {
$delta += $descentspeed;
}
$step = $ASCENT_STEP;
} else {
// Prepare for descent!
$step = $descentpoint - $d;
}
if($elevation < $endPoint["z"]) {
$delta = 0;
$elevation = $endPoint["z"];
}
}
$elevation += $delta;
}
$bearing = gcBearingTo($wayPoint, $endPoint); // degrees, clockwise from 0 deg at north
$wayPoint = gcWaypoint($wayPoint, $step, $bearing);
if($threed) {
$wayPoint["z"] = $elevation;
}
if($wayPoint["x"] > -180 && $wayPoint["x"] < 180) {
$pointList[] = $wayPoint;
} else {
// Flip paths crossing the edge of the map
if($wayPoint["x"] < -180 && $bearing > 180) {
$pointList[] = array("x" => -180, "y" => $wayPoint["y"], "z" => $elevation);
$wayPoint["x"] += 360;
$pointList[] = null; // break mark
$pointList[] = array("x" => 180, "y" => $wayPoint["y"], "z" => $elevation);
}
if($wayPoint["x"] > 180 && $bearing < 180) {
$pointList[] = array("x" => 180, "y" => $wayPoint["y"], "z" => $elevation);
$wayPoint["x"] -= 360;
$pointList[] = null; // break mark
$pointList[] = array("x" => -180, "y" => $wayPoint["y"], "z" => $elevation);
}
}
$d = gcPointDistance($startPoint, $wayPoint);
}
$pointList[] = $endPoint;
return $pointList;
}
?>