|
5 | 5 | error_reporting(E_ALL); |
6 | 6 | ini_set('display_errors', '1'); |
7 | 7 |
|
8 | | -$cwd = dirname(realpath(__FILE__)); |
9 | | -$zipfile = $cwd.'/allCountries.zip'; |
10 | | -$txtfile = $cwd.'/allCountries.txt'; |
| 8 | +$cwd = dirname(realpath(__FILE__)); |
| 9 | +$zipfile = $cwd.'/allCountries.zip'; |
| 10 | +$txtfile = $cwd.'/allCountries.txt'; |
11 | 11 | $statsOnly = false; |
12 | | -$regenKey = false; |
13 | | -$keyArr = $regenKey ? [] : json_decode(file_get_contents($cwd.'/key.json'), true); |
| 12 | +$regenKey = false; |
| 13 | +$keyArr = $regenKey ? [] : json_decode(file_get_contents($cwd.'/key.json'), true); |
14 | 14 |
|
15 | 15 | // Download http://download.geonames.org/export/zip/allCountries.zip |
16 | 16 | if (!file_exists($txtfile)) { |
|
37 | 37 | echo "Cleaning up from previous build...\n"; |
38 | 38 | clean($cwd); |
39 | 39 |
|
40 | | -$row = 0; |
| 40 | +$row = 0; |
41 | 41 | $headers = [ |
42 | 42 | 'countryCode', // iso country code, 2 characters |
43 | 43 | 'postalCode', // varchar(20) |
|
52 | 52 | 'longitude', // estimated longitude (wgs84) |
53 | 53 | 'accuracy', // accuracy of lat/lng from 1=estimated to 6=centroid |
54 | 54 | ]; |
55 | | -$stats = []; |
| 55 | +$stats = []; |
56 | 56 | if (($handle = fopen($txtfile, 'r')) !== false) { |
57 | 57 | while (($data = fgetcsv($handle, 1000, "\t")) !== false) { |
58 | 58 | $place = new stdClass(); |
59 | 59 | foreach ($data as $key => $value) { |
60 | | - $keyname = $headers[$key]; |
| 60 | + $keyname = $headers[$key]; |
61 | 61 | $place->{$keyname} = $value; |
62 | 62 | } |
63 | | - if (!empty($place->countryCode) && !empty($place->postalCode)) { |
| 63 | + if (!empty($place->countryCode) || !empty($place->postalCode)) { |
64 | 64 | $countryCode = strtoupper($place->countryCode); |
65 | 65 |
|
66 | 66 | if ($statsOnly) { |
|
71 | 71 |
|
72 | 72 | if (!isset($stats[$countryCode])) { |
73 | 73 | $stats[$countryCode] = [ |
74 | | - 'count' => 1, |
75 | | - 'lowest' => $minCode, |
| 74 | + 'count' => 1, |
| 75 | + 'lowest' => $minCode, |
76 | 76 | 'highest' => $minCode, |
77 | 77 | ]; |
78 | 78 | } else { |
|
86 | 86 | continue; |
87 | 87 | } |
88 | 88 |
|
89 | | - $minOnly = in_array($countryCode, ['JP', 'PT']); |
| 89 | + $minCodeRounded = minPostalRounded($place->postalCode, $place->countryCode, $keyArr); |
| 90 | + $minOnly = in_array($countryCode, ['JP', 'PT']); |
90 | 91 | echo 'Creating entry for '.$place->countryCode.' '.$place->postalCode.($minOnly ? ' (min only)' : '').' in index '.$minCodeRounded."\n"; |
91 | 92 | if (!is_dir($cwd.'/'.$countryCode)) { |
92 | 93 | mkdir($cwd.'/'.$countryCode); |
93 | 94 | } |
94 | 95 |
|
95 | 96 | if (!file_exists($cwd.'/'.$countryCode.'/test.jsonp')) { |
96 | | - $test = new stdClass(); |
| 97 | + $test = new stdClass(); |
97 | 98 | $test->success = true; |
98 | 99 | file_put_contents($cwd.'/'.$countryCode.'/test.jsonp', 'zipCodesTestCallback('.json_encode($test).');'); |
99 | 100 | } |
|
108 | 109 | mkdir($cwd.'/'.$countryCode.'/min'); |
109 | 110 | } |
110 | 111 |
|
111 | | - $minCodeRounded = minPostalRounded($place->postalCode, $place->countryCode, $keyArr); |
112 | 112 | $codes = []; |
113 | 113 | if (file_exists($cwd.'/'.$countryCode.'/min/'.$minCodeRounded.'.json')) { |
114 | 114 | $codes = json_decode(file_get_contents($cwd.'/'.$countryCode.'/min/'.$minCodeRounded.'.json'), true); |
115 | 115 | } |
116 | 116 | $codes[$place->postalCode] = $data; |
117 | | - $codesJSON = json_encode($codes); |
| 117 | + $codesJSON = json_encode($codes); |
118 | 118 | file_put_contents( |
119 | 119 | $cwd.'/'.$countryCode.'/min/'.$minCodeRounded.'.json', |
120 | 120 | $codesJSON |
|
138 | 138 | $key[$country] = []; |
139 | 139 | if ($stat['count'] > 100000) { |
140 | 140 | $chars = 3; |
141 | | - } elseif($stat['count'] > 10000) { |
| 141 | + } elseif ($stat['count'] > 10000) { |
142 | 142 | $chars = 2; |
143 | | - } elseif($stat['count'] > 1000) { |
| 143 | + } elseif ($stat['count'] > 1000) { |
144 | 144 | $chars = 1; |
145 | 145 | } else { |
146 | 146 | $chars = 0; |
|
161 | 161 |
|
162 | 162 | /** |
163 | 163 | * Recursively delete files within a directory based on type. |
| 164 | + * |
164 | 165 | * @param $dir |
165 | | - * @param array $types |
| 166 | + * @param array $types |
166 | 167 | */ |
167 | 168 | function clean($dir, $types = ['json', 'jsonp']) |
168 | 169 | { |
@@ -200,7 +201,7 @@ function minPostalRounded($postalCode, $country, $keyArr = []) |
200 | 201 | { |
201 | 202 | $result = 0; |
202 | 203 |
|
203 | | - if ($keyArr[$country] > 1) { |
| 204 | + if (isset($keyArr[$country]) && $keyArr[$country] > 1) { |
204 | 205 |
|
205 | 206 | // Only allow uppercase. |
206 | 207 | $postalCode = strtoupper($postalCode); |
|
0 commit comments