-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathzipcode.php
371 lines (312 loc) · 11.6 KB
/
zipcode.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/**
* @package zipcode
*/
/**
* Zip Code Range and Distance Calculation
*
* Calculate the distance between zip codes and find all zip codes within a
* given distance of a known zip code.
*
* Project page: https://github.com/Quixotix/PHP-ZipCode-Class
* Live example: http://www.micahcarrick.com/code/PHP-ZipCode/example.php
*
* @package zipcode
* @author Micah Carrick
* @copyright (c) 2011 - Micah Carrick
* @version 2.0
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License v3
*/
class ZipCode
{
private $zip_code_id;
private $zip_code;
private $lat;
private $lon;
private $city;
private $county;
private $area_code;
private $time_zone;
private $state_prefix;
private $state_name;
public $mysql_table = 'zip_code';
public $mysql_conn = false;
private $mysql_row;
private $print_name;
private $location_type;
const UNIT_MILES = 1;
const UNIT_KILOMETERS = 2;
const MILES_TO_KILOMETERS = 1.609344;
const LOCATION_ZIP = 1;
const LOCATION_CITY_STATE = 2;
/**
* Constructor
*
* Instantiate a new ZipCode object by passing in a location. The location
* can be specified by a string containing a 5-digit zip code, city and
* state, or latitude and longitude.
*
* @param string
* @return ZipCode
*/
public function __construct($location)
{
if (is_array($location)) {
$this->setPropertiesFromArray($location);
$this->print_name = $this->zip_code;
$this->location_type = $this::LOCATION_ZIP;
} else {
$this->location_type = $this->locationType($location);
switch ($this->location_type) {
case ZipCode::LOCATION_ZIP:
$this->zip_code = $this->sanitizeZip($location);
$this->print_name = $this->zip_code;
break;
case ZipCode::LOCATION_CITY_STATE:
$a = $this->parseCityState($location);
$this->city = $a[0];
$this->state_prefix = $a[1];
$this->print_name = $this->city;
break;
default:
throw new Exception('Invalid location type for '.__CLASS__);
}
}
}
public function __toString()
{
return $this->print_name;
}
/**
* Calculate Distance using SQL
*
* Calculates the distance, in miles, to a specified location using MySQL
* math functions within the query.
*
* @access private
* @param string
* @return float
*/
private function calcDistanceSql($location)
{
$sql = 'SELECT 3956 * 2 * ATAN2(SQRT(POW(SIN((RADIANS(t2.lat) - '
.'RADIANS(t1.lat)) / 2), 2) + COS(RADIANS(t1.lat)) * '
.'COS(RADIANS(t2.lat)) * POW(SIN((RADIANS(t2.lon) - '
.'RADIANS(t1.lon)) / 2), 2)), '
.'SQRT(1 - POW(SIN((RADIANS(t2.lat) - RADIANS(t1.lat)) / 2), 2) + '
.'COS(RADIANS(t1.lat)) * COS(RADIANS(t2.lat)) * '
.'POW(SIN((RADIANS(t2.lon) - RADIANS(t1.lon)) / 2), 2))) '
.'AS "miles" '
."FROM {$this->mysql_table} t1 INNER JOIN {$this->mysql_table} t2 ";
switch ($this->location_type) {
case ZipCode::LOCATION_ZIP:
// note: zip code is sanitized in the constructor
$sql .= "WHERE t1.zip_code = '{$this->zip_code}' ";
break;
case ZipCode::LOCATION_CITY_STATE:
$city = @mysql_real_escape_string($this->city);
$state = @mysql_real_escape_string($this->state_prefix);
$sql .= "WHERE (t1.city = '$city' AND t1.state_prefix = '$state') AND t2.zip_code = '$zip_to'";
break;
default:
throw new Exception('Invalid location type for '.__CLASS__);
}
switch (ZipCode::locationType($location))
{
case ZipCode::LOCATION_ZIP:
$zip_to = $this->sanitizeZip($location);
$sql .= "AND t2.zip_code = '$zip_to'";
break;
case ZipCode::LOCATION_CITY_STATE:
$a = $this->parseCityState($location);
$city = @mysql_real_escape_string($a[0]);
$state = @mysql_real_escape_string($a[1]);
$sql .= "AND (t2.city = '$city' AND t2.state_prefix = '$state')";
break;
}
$r = @mysql_query($sql);
if (!$r) {
throw new Exception(mysql_error());
}
if (mysql_num_rows($r) == 0) {
throw new Exception("Record does not exist calculating distance between $zip_from and $zip_to");
}
$miles = mysql_result($r, 0);
mysql_free_result($r);
return $miles;
}
public function getAreaCode()
{
if (empty($this->zip_code_id)) $this->setPropertiesFromDb();
return $this->city;
}
public function getCity()
{
if (empty($this->zip_code_id)) $this->setPropertiesFromDb();
return $this->city;
}
public function getCounty()
{
if (empty($this->zip_code_id)) $this->setPropertiesFromDb();
return $this->county;
}
public function getStateName()
{
if (empty($this->zip_code_id)) $this->setPropertiesFromDb();
return $this->state_name;
}
public function getStatePrefix()
{
if (empty($this->zip_code_id)) $this->setPropertiesFromDb();
return $this->state_prefix;
}
public function getDbRow()
{
if (empty($this->zip_code_id)) $this->setPropertiesFromDb();
return $this->mysql_row;
}
/**
* Get Distance To Zip
*
* Gets the distance to another zip code. The distance can be obtained in
* either miles or kilometers.
*
* @param string
* @param integer
* @param integer
* @return float
*/
public function getDistanceTo($zip, $units=ZipCode::UNIT_MILES)
{
$miles = $this->calcDistanceSql($zip);
if ($units == ZipCode::UNIT_KILOMETERS) {
return $miles * ZipCode::MILES_TO_KILOMETERS;
} else {
return $miles;
}
}
public function getZipsInRange($range_from, $range_to, $units=1)
{
if (empty($this->zip_code_id)) $this->setPropertiesFromDb();
$sql = "SELECT 3956 * 2 * ATAN2(SQRT(POW(SIN((RADIANS({$this->lat}) - "
.'RADIANS(z.lat)) / 2), 2) + COS(RADIANS(z.lat)) * '
."COS(RADIANS({$this->lat})) * POW(SIN((RADIANS({$this->lon}) - "
."RADIANS(z.lon)) / 2), 2)), SQRT(1 - POW(SIN((RADIANS({$this->lat}) - "
."RADIANS(z.lat)) / 2), 2) + COS(RADIANS(z.lat)) * "
."COS(RADIANS({$this->lat})) * POW(SIN((RADIANS({$this->lon}) - "
."RADIANS(z.lon)) / 2), 2))) AS \"miles\", z.* FROM {$this->mysql_table} z "
."WHERE zip_code <> '{$this->zip_code}' "
."AND lat BETWEEN ROUND({$this->lat} - (25 / 69.172), 4) "
."AND ROUND({$this->lat} + (25 / 69.172), 4) "
."AND lon BETWEEN ROUND({$this->lon} - ABS(25 / COS({$this->lat}) * 69.172)) "
."AND ROUND({$this->lon} + ABS(25 / COS({$this->lat}) * 69.172)) "
."AND 3956 * 2 * ATAN2(SQRT(POW(SIN((RADIANS({$this->lat}) - "
."RADIANS(z.lat)) / 2), 2) + COS(RADIANS(z.lat)) * "
."COS(RADIANS({$this->lat})) * POW(SIN((RADIANS({$this->lon}) - "
."RADIANS(z.lon)) / 2), 2)), SQRT(1 - POW(SIN((RADIANS({$this->lat}) - "
."RADIANS(z.lat)) / 2), 2) + COS(RADIANS(z.lat)) * "
."COS(RADIANS({$this->lat})) * POW(SIN((RADIANS({$this->lon}) - "
."RADIANS(z.lon)) / 2), 2))) <= $range_to "
."AND 3956 * 2 * ATAN2(SQRT(POW(SIN((RADIANS({$this->lat}) - "
."RADIANS(z.lat)) / 2), 2) + COS(RADIANS(z.lat)) * "
."COS(RADIANS({$this->lat})) * POW(SIN((RADIANS({$this->lon}) - "
."RADIANS(z.lon)) / 2), 2)), SQRT(1 - POW(SIN((RADIANS({$this->lat}) - "
."RADIANS(z.lat)) / 2), 2) + COS(RADIANS(z.lat)) * "
."COS(RADIANS({$this->lat})) * POW(SIN((RADIANS({$this->lon}) - "
."RADIANS(z.lon)) / 2), 2))) >= $range_from "
."ORDER BY 1 ASC";
$r = mysql_query($sql);
if (!$r) {
throw new Exception(mysql_error());
}
$a = array();
while ($row = mysql_fetch_array($r, MYSQL_ASSOC))
{
// TODO: load ZipCode from array
$a[$row['miles']] = new ZipCode($row);
}
return $a;
}
private function hasDbConnection()
{
if ($this->mysql_conn) {
return mysql_ping($this->mysql_conn);
} else {
return mysql_ping();
}
}
private function locationType($location)
{
if (ZipCode::isValidZip($location)) {
return ZipCode::LOCATION_ZIP;
} elseif (ZipCode::isValidCityState($location)) {
return ZipCode::LOCATION_CITY_STATE;
} else {
return false;
}
}
static function isValidZip($zip)
{
return preg_match('/^[0-9]{5}/', $zip);
}
static function isValidCityState($location)
{
$words = split(',', $location);
if (empty($words) || count($words) != 2 || strlen(trim($words[1])) != 2) {
return false;
}
if (!is_numeric($words[0]) && !is_numeric($words[1])) {
return true;
}
return false;
}
static function parseCityState($location)
{
$words = split(',', $location);
if (empty($words) || count($words) != 2 || strlen(trim($words[1])) != 2) {
throw new Exception("Failed to parse city and state from string.");
}
$city = trim($words[0]);
$state = trim($words[1]);
return array($city, $state);
}
// @access protected
private function sanitizeZip($zip)
{
return preg_replace("/[^0-9]/", '', $zip);
}
private function setPropertiesFromArray($a)
{
if (!is_array($a)) {
throw new Exception("Argument is not an array");
}
foreach ($a as $key => $value)
{
$this->$key = $value;
}
$this->mysql_row = $a;
}
private function setPropertiesFromDb()
{
switch ($this->location_type) {
case ZipCode::LOCATION_ZIP:
$sql = "SELECT * FROM {$this->mysql_table} t "
."WHERE zip_code = '{$this->zip_code}' LIMIT 1";
break;
case ZipCode::LOCATION_CITY_STATE:
$sql = "SELECT * FROM {$this->mysql_table} t "
."WHERE city = '{$this->city}' "
."AND state_prefix = '{$this->state_prefix}' LIMIT 1";
break;
}
$r = mysql_query($sql);
$row = mysql_fetch_array($r, MYSQL_ASSOC);
mysql_free_result($r);
if (!$row)
{
throw new Exception("{$this->print_name} was not found in the database.");
}
$this->setPropertiesFromArray($row);
}
}
?>