1

I am using the following scripting that I found on the net to grab all postal codes between a given set coordinates.

When using it my concern is that when some postal codes being grab are greater than the distance entered; not by much - about 20 KM off.

function GetPostalCodes($latitude, $longitude, $range) {
    $radius = 3959;
    $north = rad2deg(asin(sin(deg2rad($latitude)) * cos($range / $radius) + cos(deg2rad($latitude)) * sin($range / $radius) * cos(deg2rad(0))));
    $south = rad2deg(asin(sin(deg2rad($latitude)) * cos($range / $radius) + cos(deg2rad($latitude)) * sin($range / $radius) * cos(deg2rad(180))));
    $east = rad2deg(deg2rad($longitude) + atan2(sin(deg2rad(90)) * sin($range / $radius) * cos(deg2rad($latitude)), cos($range / $radius) - sin(deg2rad($latitude)) * sin(deg2rad($north))));
    $west = rad2deg(deg2rad($longitude) + atan2(sin(deg2rad(270)) * sin($range / $radius) * cos(deg2rad($latitude)), cos($range / $radius) - sin(deg2rad($latitude)) * sin(deg2rad($north))));
    $return = DBSelectAllArrays("SELECT postal FROM postalcodes WHERE (latitude <= $north AND latitude >= $south AND longitude <= $east AND longitude >= $west)");
    krsort($return);
    if (empty($return)) return false;
    return $return;
}

Is there something I am missing to get a more accurate result?

Tim
  • 403
  • 1
  • 6
  • 20
  • Does the accuracy decrease the further from the point of origin(lat/long) they get? – nickhar Dec 01 '12 at 02:18
  • do not know, my queries have been mainly within the 20 KM range. – Tim Dec 01 '12 at 02:42
  • seems to get worse actually, when I search a 40 KM range, I am getting 100 KM results. – Tim Dec 01 '12 at 02:46
  • I believe I just figured this out. even though it says I am search kilometers, I believe it doing it in miles. – Tim Dec 01 '12 at 03:07

2 Answers2

0

Given your comments:

$radius = 6371.0; // mean radius of Earth in km

This is taken from wikipedia, but I've seen it within a +/- 3km tolerance from other sources. I began to question whether you were using great circle distance calculations, but this is more important for accuracy over longer distances due to the curvature of the earths surface.

nickhar
  • 19,981
  • 12
  • 60
  • 73
  • I can not believe it was that easy. Thank you for that radius number! – Tim Dec 01 '12 at 03:20
  • That's why I asked my initial question - I wondered if you were using GCD, but it's practically irrelevant over 20km! – nickhar Dec 01 '12 at 03:24
  • Note: you're using a bounding box computation. So even if you have the radius number correct (for km) in your haversine (aka Great Circle Distance) formula, you're going to get some hits outside the radius, in the corners of your bounding box. It probably doesn't matter much in sparsely populated areas. But it does confuse things a bit in places like New York City or Toronto. – O. Jones Dec 01 '12 at 05:13
0

Tim, you started by using a bounding box (rectangle) and then with the Haversine formula, you'll get a radius (circle), which generally is much better if you just want people within a certain distance. You don't state your purpose, but if you're looking for people who may travel a certain distance to you, you may want to consider metropolitan areas, which vary in shape. If so, look at: Canadian Metro Areas data

user1473461
  • 369
  • 2
  • 2