|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Generate a zip /city/state lookup database that can be opcache'd for near-instant lookup. |
| 4 | + */ |
| 5 | +error_reporting(E_ALL); |
| 6 | +ini_set('display_errors', '1'); |
| 7 | + |
| 8 | +$cwd = dirname(realpath(__FILE__)); |
| 9 | +$zipfile = $cwd.'/allCountries.zip'; |
| 10 | +$txtfile = $cwd.'/allCountries.txt'; |
| 11 | +$statsOnly = false; |
| 12 | +$regenKey = false; |
| 13 | + |
| 14 | +// Download http://download.geonames.org/export/zip/allCountries.zip |
| 15 | +if (!file_exists($txtfile)) { |
| 16 | + echo "Downloading zipcodes..."; |
| 17 | + $ch = curl_init(); |
| 18 | + curl_setopt($ch, CURLOPT_URL, 'http://download.geonames.org/export/zip/allCountries.zip'); |
| 19 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 20 | + $data = curl_exec($ch); |
| 21 | + curl_close($ch); |
| 22 | + $file = fopen($zipfile, "w+"); |
| 23 | + fputs($file, $data); |
| 24 | + fclose($file); |
| 25 | + $zip = new ZipArchive; |
| 26 | + $res = $zip->open($zipfile); |
| 27 | + if ($res === true) { |
| 28 | + $zip->extractTo('.'); |
| 29 | + $zip->close(); |
| 30 | + unlink($zipfile); |
| 31 | + } else { |
| 32 | + die('Unable to unzip.'); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +$row = 0; |
| 37 | +$headers = [ |
| 38 | + 'countryCode', // iso country code, 2 characters |
| 39 | + 'postalCode', // varchar(20) |
| 40 | + 'placeName', // varchar(180) |
| 41 | + 'adminName1', // 1. order subdivision (state) varchar(100) |
| 42 | + 'adminCode1', // 1. order subdivision (state) varchar(20) |
| 43 | + 'adminName2', // 2. order subdivision (county/province) varchar(100) |
| 44 | + 'adminCode2', // 2. order subdivision (county/province) varchar(20) |
| 45 | + 'adminName3', // 3. order subdivision (community) varchar(100) |
| 46 | + 'adminCode3', // 3. order subdivision (community) varchar(20) |
| 47 | + 'latitude', // estimated latitude (wgs84) |
| 48 | + 'longitude', // estimated longitude (wgs84) |
| 49 | + 'accuracy', // accuracy of lat/lng from 1=estimated to 6=centroid |
| 50 | +]; |
| 51 | +$stats = []; |
| 52 | +if (($handle = fopen($txtfile, 'r')) !== false) { |
| 53 | + while (($data = fgetcsv($handle, 1000, "\t")) !== false) { |
| 54 | + $place = new stdClass(); |
| 55 | + foreach ($data as $key => $value) { |
| 56 | + $keyname = $headers[$key]; |
| 57 | + $place->{$keyname} = str_replace(["'", "\t"], '', $value); |
| 58 | + } |
| 59 | + if (!empty($place->countryCode) && !empty($place->postalCode)) { |
| 60 | + $countryCode = strtoupper($place->countryCode); |
| 61 | + $zipsFile = $cwd.'/zips/'.$countryCode.'.php'; |
| 62 | + if (!file_exists($zipsFile)) { |
| 63 | + file_put_contents( |
| 64 | + $zipsFile, |
| 65 | + "<?php \$zipCodes['".$countryCode."']=[" |
| 66 | + ); |
| 67 | + } |
| 68 | + $city = strtoupper($place->placeName); |
| 69 | + $cityFile = $cwd.'/cities/'.$countryCode.'.php'; |
| 70 | + if (!file_exists($cityFile)) { |
| 71 | + file_put_contents( |
| 72 | + $cityFile, |
| 73 | + "<?php \$cities['".$countryCode."']=[" |
| 74 | + ); |
| 75 | + } |
| 76 | + $string = "'".$place->postalCode."'=>'".implode("\t", $data)."',"; |
| 77 | + file_put_contents($zipsFile, $string, FILE_APPEND); |
| 78 | + } |
| 79 | + $row++; |
| 80 | + } |
| 81 | + fclose($handle); |
| 82 | +} |
| 83 | + |
| 84 | +// @todo - Add '];' to the end of all files that were generated. |
0 commit comments