forked from robho/php-igc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIGCReader.php
More file actions
338 lines (300 loc) · 8.37 KB
/
IGCReader.php
File metadata and controls
338 lines (300 loc) · 8.37 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
require_once(dirname(__FILE__).'/ITrackfileReader.php');
/**
* IGCReader
* forked from project php-igc by Mike Milano <coder1@gmail.com>
*
* This class is instanciated with the file path of the IGC file. It
* will create an array of IGC record objects for convenient use of the data.
*/
class IGCReader implements ITrackfileReader
{
/**
* The date and time of the flight
* @access public
* @var DateTime
*/
public $datetime;
/**
* The Pilot's name
* @access public
* @var string
*/
public $pilot;
/**
* The Glider type
* @access public
* @var string
*/
public $glider_type;
/**
* The Glider ID
* @access public
* @var string
*/
public $glider_id;
/**
* The max altitude of the flight
* @access public
* @var string
*/
public $max_altitude;
/**
* The minimum altitude of the flight
* @access public
* @var string
*/
public $min_altitude;
/**
* The total distance of the flight
* @access public
* @var string
*/
public $distance;
/**
* The total duration of the flight (in seconds)
* @access public
* @var integer
*/
public $duration;
private $detailsset;
/**
* Class constructor creates the IGCReader object from a file path.
*
* @param string $file_path usually this will be the request vars
*/
public function __construct($file_path)
{
$this->detailsset = false;
spl_autoload_register('self::ClassAutoloader');
if (@is_file($file_path)) {
$handle = @fopen($file_path, "r");
if ($handle) {
while (($buffer = fgets($handle)) !== FALSE) {
$this->records[] = $this->getRecord($buffer);
}
}
} else {
// assume $file_path contains IGC
foreach (explode("\n", $file_path) as $buffer) {
if (strlen(trim($buffer))<=0) {
continue;
}
$this->records[] = $this->getRecord($buffer);
}
}
spl_autoload_unregister('self::ClassAutoloader');
}
/**
* Returns an IGC record object
*
* @param string $string is the raw record line from an IGC file
* @return IGC_Record Returns the specific IGC_Record object or false if the record isn't supported.
*/
public function getRecord($string) {
$classname = 'IGC_'.strtoupper(substr($string,0,1)).'_Record';
return new $classname($string);
}
/**
* Sets the details of the IGC files from the record objects within
*/
public function setDetails()
{
if ($this->detailsset)
return;
$this->max_altitude = 0;
$this->min_altitude = 80000;
// set lowest and highest altitude
if (is_array($this->records)) {
$this->datetime = new DateTime("1970-01-01");
$this->datetime->setTimezone(new DateTimeZone('UTC'));
$start_found = false;
foreach ($this->records as $each) {
if ($each->type == 'H') {
if ($each->tlc == 'DTE') {
if ($this->startsWith($each->value, 'DATE:')) {
$this->datetime->setDate(intval('20'.substr($each->value, 9, 2)),
intval(substr($each->value, 7, 2)),
intval(substr($each->value, 5, 2)));
}
else {
$this->datetime->setDate(intval('20'.substr($each->value, 4, 2)),
intval(substr($each->value, 2, 2)),
intval(substr($each->value, 0, 2)));
}
}
elseif ($each->tlc == 'GTY') {
if ($this->startsWith($each->value, 'GLIDERTYPE:')) {
$this->glider_type = substr($each->value, 11);
}
else {
$this->glider_type = $each->value;
}
}
elseif ($each->tlc == 'PLT') {
$this->pilot = ucwords(strtolower($each->value));
}
}
elseif ($each->type == 'B') {
$record_time = clone $this->datetime;
$record_time->setTime($each->time_array['h'],
$each->time_array['m'],
$each->time_array['s']);
if (!$start_found) {
$start_found = true;
$this->datetime = $record_time;
}
$this->duration = $record_time->getTimestamp() - $this->datetime->getTimestamp();
if ($each->pressure_altitude > $this->max_altitude) {
$this->max_altitude = $each->pressure_altitude;
}
elseif ($each->pressure_altitude < $this->min_altitude) {
$this->min_altitude = $each->pressure_altitude;
}
}
}
}
// reset to 0 if a minimum altitude was never recorded
if ($this->min_altitude == 80000) {
$this->min_altitude = 0;
}
$this->detailsset = true;
}
/**
* Returns the point list
*/
public function getRecords($first = false)
{
$pt_records = array();
$this->setDetails();
//$date = str_replace('+00:00', 'Z', date('c', $this->datetime->getTimestamp()));// $this->datetime;
$date = $this->datetime;// $this->toGMT($this->datetime);
//$dateinit = $date;
if (is_array($this->records)) {
$use_gps = true;
foreach ($this->records as $each) {
if ($each->type == "B" && floatval($each->pressure_altitude) != 0)
{
$use_gps = false;
break;
}
}
foreach ($this->records as $each) {
if ($each->type == "B")
{
$date->setTime(intval($each->time_array['h']), intval($each->time_array['m']), intval($each->time_array['s']));
//$date->add(new DateInterval("PT".intval($each->time_array['h'])."H".$each->time_array['m']."M".$each->time_array['s']."S"));
$pt_records[] = (object)[
'date' => clone $date,
'latitude' => floatval($each->latitude['decimal_degrees']),
'longitude' => floatval($each->longitude['decimal_degrees']),
'altitude' => floatval($use_gps?$each->gps_altitude:$each->pressure_altitude)];
if ($first)
break;
}
}
}
return $pt_records;
}
/**
* Returns the first point
*/
public function getFirstRecord()
{
$pt_records = $this->getRecords(true);
if (is_array($pt_records) && count($pt_records)>0)
return $pt_records[0];
return null;
}
/**
* Returns the HTML and Javascript to draw the path over GoogleMaps
*
* @param string $key is the GoogleAPI developer key
* @param integer $width in pixels
* @param integer $height in pixels
* @return string Returns HTML, CSS, and JavaScript
*/
public function getMap($key, $width, $height)
{
if (count($this->records)<1) {
$code = "invalid file";
return $code;
}
$code = '<script src="http://maps.google.com/maps?file=api&v=2&key='.$key.'" type="text/javascript"></script>
<br /><br />
<div id="map" style="width: '.$width.'px; height: '.$height.'px; border: 2px solid #111111;"></div>
<script type="text/javascript">
function loadIGC() {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
';
$started = false;
foreach ($this->records as $each) {
if ($each->type == "B") {
if (!$started) {
$code .= "map.setCenter(new GLatLng(".$each->latitude['decimal_degrees'].", ".$each->longitude['decimal_degrees']."), 13, G_SATELLITE_MAP);\n";
$code .= "var polyline = new GPolyline([\n";
$started = true;
}
$code .= "new GLatLng(".$each->latitude['decimal_degrees'].", ".$each->longitude['decimal_degrees']."),\n";
}
}
$code .= '
], "#FF0000", 2);
map.addOverlay(polyline);
}
window.onload = loadIGC;
</script>';
return $code;
}
/**
* Returns the full manufacturer string from the code defined in the A record
*
* @param string $code is the manufacturer's code from the A record
* @return string Full manufacturer string
*/
public static function GetManufacturerFromCode($code)
{
// manufacturer array
$man = array();
$man['B'] = "Borgelt";
$man['C'] = "Cambridge";
$man['E'] = "EW";
$man['F'] = "Filser";
$man['I'] = "Ilec";
$man['M'] = "Metron";
$man['P'] = "Peschges";
$man['S'] = "Sky Force";
$man['T'] = "PathTracker";
$man['V'] = "Varcom";
$man['W'] = "Westerboer";
$man['Z'] = "Zander";
$man['1'] = "Collins";
$man['2'] = "Honeywell";
$man['3'] = "King";
$man['4'] = "Garmin";
$man['5'] = "Trimble";
$man['6'] = "Motorola";
$man['7'] = "Magellan";
$man['8'] = "Rockwell";
if (!$man[(string)$code]) {
return false;
}
return $man[(string)$code];
}
private static function ClassAutoloader($class_name)
{
$file_path = dirname(__FILE__).DIRECTORY_SEPARATOR.
"lib".DIRECTORY_SEPARATOR.$class_name.".php";
if (!is_file($file_path))
throw new NotFoundException($file_path);
@require($file_path);
}
private static function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
}
?>