-
Notifications
You must be signed in to change notification settings - Fork 127
/
2_horde_service_weather_airports_change.php
107 lines (99 loc) · 3.5 KB
/
2_horde_service_weather_airports_change.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
<?php
class HordeServiceWeatherAirportsChange extends Horde_Db_Migration_Base
{
protected $_handle;
public function up()
{
// Check for the file before attempting to populate the table. Will
// save us from having to rollback the transaction.
if (!$this->_checkForMetarData()) {
throw new Horde_Exception('Unable to locate METAR data.');
}
// Get rid of the old data.
$this->down();
$this->beginDbTransaction();
$t = $this->createTable('horde_metar_airports', array('autoincrementKey' => array('id')));
$t->column('id', 'integer');
$t->column('icao', 'string', array('limit' => 4));
$t->column('name', 'string', array('limit' => 80));
$t->column('state', 'string', array('limit' => 2));
$t->column('country', 'string', array('limit' => 50));
$t->column('municipality', 'string', array('limit' => 80));
$t->column('latitude', 'float', array('default' => 0));
$t->column('longitude', 'float', array('default' => 0));
$t->column('elevation', 'float', array('default' => 0));
$t->end();
$this->_populateTable();
$this->commitDbTransaction();
}
protected function _checkForMetarData()
{
// First see if we have a local copy in the same directory.
$file_name = __DIR__ . DIRECTORY_SEPARATOR . 'airport-codes.csv';
if (file_exists($file_name)) {
$this->_handle = @fopen($file_name, 'rb');
} else {
$file_location = 'https://raw.githubusercontent.com/datasets/airport-codes/master/data/airport-codes.csv';
$this->_handle = @fopen($file_location, 'rb');
}
if (!$this->_handle) {
$this->announce('ERROR: Unable to populate METAR database.');
return false;
}
return true;
}
protected function _populateTable()
{
$line = 0;
$insert = 'INSERT INTO horde_metar_airports '
. '(id, icao, name, state, country, municipality, latitude,'
. 'longitude, elevation) '
. 'VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)';
/**
* ident,
* type,
* name,
* latitude_deg,
* longitude_deg,
* elevation_ft,
* continent,
* iso_country,
* iso_region,
* municipality,
* gps_code,
* iata_code,
* local_code
*/
while (($data = fgetcsv($this->_handle)) !== false) {
if (sizeof($data) < 13) {
continue;
}
try {
$this->_connection->insert($insert, array(
$line,
$data[0],
$data[2],
str_replace($data[7] . '-', '', $data[8]),
$data[7],
$data[9],
round($data[3], 4),
round($data[4], 4),
$data[5])
);
} catch (Horde_Db_Exception $e) {
$this->announce('ERROR: ' . $e->getMessage());
$this->rollbackDbTransaction();
return;
}
$line++;
}
$this->announce('Added ' . ($line) . ' airport identifiers to the database.', 'cli.message');
}
public function down()
{
$tableList = $this->tables();
if (in_array('horde_metar_airports', $tableList)) {
$this->dropTable('horde_metar_airports');
}
}
}