Browse Source

add timezone lookup from airport codes

main
Aaron Parecki 6 years ago
parent
commit
58034b519f
No known key found for this signature in database GPG Key ID: 276C2817346D6056
7 changed files with 9323 additions and 15 deletions
  1. +1
    -0
      .gitignore
  2. +44
    -15
      controllers/timezone.php
  3. +9165
    -0
      data/airports-compact.csv
  4. +45
    -0
      data/airports-csv-compact.php
  5. +36
    -0
      data/airports-csv-to-json.php
  6. +31
    -0
      p3k/Airports.php
  7. +1
    -0
      views/index.php

+ 1
- 0
.gitignore View File

@ -1,3 +1,4 @@
.DS_Store
vendor/
lib/config.php
data/airports.csv

+ 44
- 15
controllers/timezone.php View File

@ -1,5 +1,28 @@
<?php
function timezone_for_location($lat, $lng) {
$tz = \p3k\Timezone::timezone_for_location($lat, $lng);
$timezone = false;
if($tz) {
$timezone = new p3k\timezone\Result($tz);
}
if($timezone) {
return [
'timezone' => $timezone->name,
'offset' => $timezone->offset,
'seconds' => $timezone->seconds,
'localtime' => $timezone->localtime
];
} else {
return [
'error' => 'not_found',
'error_description' => 'No timezone was found for the requested location'
];
}
}
$app->get('/api/timezone', function() use($app) {
$params = $app->request()->params();
@ -8,28 +31,34 @@ $app->get('/api/timezone', function() use($app) {
$lat = (float)$params['latitude'];
$lng = (float)$params['longitude'];
$tz = \p3k\Timezone::timezone_for_location($lat, $lng);
$timezone = false;
if($tz) {
$timezone = new p3k\timezone\Result($tz);
}
$result = timezone_for_location($lat, $lng);
json_response($app, $result);
} elseif(k($params, 'airport')) {
$code = $params['airport'];
$airport = \p3k\Airports::from_code($code);
if($airport) {
$result = timezone_for_location($airport['latitude'], $airport['longitude']);
if(!isset($result['error'])) {
$result['airport'] = $airport;
}
json_response($app, $result);
if($timezone) {
json_response($app, [
'timezone' => $timezone->name,
'offset' => $timezone->offset,
'seconds' => $timezone->seconds,
'localtime' => $timezone->localtime
]);
} else {
json_response($app, [
'error' => 'not_found',
'error_description' => 'No timezone was found for the requested location'
'error' => 'not_found',
'error_description' => 'The airport code was not found'
]);
}
} else {
json_response($app, [
'error' => 'invalid_request',
'error' => 'invalid_request',
'error_description' => 'Request was missing parameters'
], 400);
}

+ 9165
- 0
data/airports-compact.csv
File diff suppressed because it is too large
View File


+ 45
- 0
data/airports-csv-compact.php View File

@ -0,0 +1,45 @@
<?php
// airports.csv is from
// http://ourairports.com/data/
// http://ourairports.com/data/airports.csv
echo "Downloading...\n";
file_put_contents('airports.csv', file_get_contents('http://ourairports.com/data/airports.csv'));
echo "Processing...\n";
$airports = [];
$fp = fopen('airports.csv', 'r');
while($line = fgetcsv($fp)) {
if($line[0] == 'id') {
$keys = $line;
continue;
}
if($line[13] == '') continue;
$data = [
$line[13], // Code
$line[4], // Latitude
$line[5], // Longitude
$line[3], // Name
];
$airports[] = $data;
}
fclose($fp);
unlink('airports.csv');
$fp = fopen('airports-compact.csv', 'w');
foreach($airports as $line) {
fputcsv($fp, $line);
}
fclose($fp);
echo "Done\n";

+ 36
- 0
data/airports-csv-to-json.php View File

@ -0,0 +1,36 @@
<?php
// airports.csv is from
// http://ourairports.com/data/
// http://ourairports.com/data/airports.csv
echo "Downloading...\n";
file_put_contents('airports.csv', file_get_contents('http://ourairports.com/data/airports.csv'));
echo "Processing...\n";
$airports = [];
$fp = fopen('airports.csv', 'r');
while($line = fgetcsv($fp)) {
if($line[0] == 'id') {
$keys = $line;
continue;
}
if($line[13] == '') continue;
$airports[$line[13]] = [];
foreach($keys as $i=>$k) {
$airports[$line[13]][$k] = $line[$i];
}
}
fclose($fp);
unlink('airports.csv');
file_put_contents('airports.json', json_encode($airports, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
echo "Done\n";

+ 31
- 0
p3k/Airports.php View File

@ -0,0 +1,31 @@
<?php
namespace p3k;
class Airports {
private static $fp = false;
public static function from_code($code) {
if(!self::$fp) {
$fp = fopen(__DIR__.'/../data/airports-compact.csv', 'r');
}
rewind($fp);
$airport = false;
while(!$airport && ($line=fgetcsv($fp))) {
if($line[0] == $code) {
$airport = [
'code' => $code,
'latitude' => $line[1],
'longitude' => $line[2],
'name' => $line[3],
];
}
}
return $airport;
}
}

+ 1
- 0
views/index.php View File

@ -6,6 +6,7 @@
<ul>
<li><a href="/api/timezone?latitude=45.5118&amp;longitude=-122.6433">/api/timezone?latitude=45.5118&amp;longitude=-122.6433</a></li>
<li><a href="/api/timezone?airport=PDX">/api/timezone?airport=PDX</a></li>
</ul>
<h2><i class="fa fa-globe"></i> Geocoder</h2>

Loading…
Cancel
Save