Given coordinates, how do I get all the Zip Codes within a 10 mile radius?
Asked Answered
D

3

8

I have a location (latitude & longitude). How can I get a list of zipcodes that are either partially or fully within the 10 mile radius of my location?

The solution could be a call to a well known web service (google maps, bing maps, etc...) or a local database solution (the client has sql server 2005) or an algorithm.

I have seen the somewhat similar question, but all the answers there pretty much pertain to using SQL Server 2008 geography functionality which is unavailable to me.

Danieldaniela answered 16/11, 2010 at 0:23 Comment(1)
What country are you trying to find zip codes for?Mc
C
5

Firstly, you'll need a database of all zipcodes and their corresponding latitudes and longitudes. In Australia, there are only a few thousand of these (and the information is easily available), however I assume it's probably a more difficult task in the US.

Secondly, given you know where you are, and you know the radius you are looking for, you can look up all zipcodes that fall within that radius. Something simple written in PHP would be as follows: (apologies it's not in C#)

function distanceFromTo($latitude1,$longitude1,$latitude2,$longitude2,$km){
  $latitude1  = deg2rad($latitude1);
  $longitude1 = deg2rad($longitude1);
  $latitude2  = deg2rad($latitude2);
  $longitude2 = deg2rad($longitude2);
  $delta_latitude  = $latitude2  - $latitude1;
  $delta_longitude = $longitude2 - $longitude1;
  $temp = pow(sin($delta_latitude/2.0),2) + cos($latitude1) * cos($latitude2) * pow(sin($delta_longitude/2.0),2);
  $earth_radius = 3956;
  $distance = $earth_radius * 2 * atan2(sqrt($temp),sqrt(1-$temp));
  if ($km)
    $distance = $distance * 1.609344;
  return $distance;
}
Chromous answered 16/11, 2010 at 1:1 Comment(1)
AngryHacker: Luke has posted a PHP implementation of the Haversine formula that I mentioned.Selflove
H
11

Start with a zip code database that contains zipcodes and their corresponding latitude and longitude coordinates:

http://www.zipcodedownload.com/Products/Product/Z5Commercial/Standard/Overview/

To get the distance between latitude and longitude, you will need a good distance formula. This site has a couple variations:

http://www.meridianworlddata.com/distance-calculation/

The "Great Circle Distance" formula is a little extreme. This one works well enough from my experience:

sqrt(x * x + y * y)

where x = 69.1 * (lat2 - lat1)
and y = 69.1 * (lon2 - lon1) * cos(lat1/57.3)

Your SQL Query will then look something like this:

select zd.ZipCode
from ZipData zd
where 
    sqrt(
        square(69.1 * (zd.Latitude - @Latitude)) +
        square(69.1 * (zd.Longitude - @Longitude) * cos(@Latitude/57.3))
    ) < @Distance

Good luck!

Heigho answered 18/11, 2010 at 4:28 Comment(4)
This was actually a great answer... all the distances are within a quarter mile that I have figured... +1Neptunium
great answer +1 - can you tell me where the maths comes from? What do the decimal numbers represent?Hoarfrost
The link to meridianworld.com is no longer valid, so I updated it to a cached version. I believe the magic numbers come from some distance approximation that calculates distances on the surface of a sphere. There are more accurate formulas out there, but for your basic "store locator" scenario, this one has worked well for me.Heigho
@Heigho it looks like the link still works if you drop the .asp extension (meridianworlddata.com/distance-calculation)Gothurd
S
5

Most searches work with centroids. In order to work with partial zipcodes being within the 10 miles, you are going to have to buy a database of zipcode polygons (*). Then implement an algorithm which checks for zipcodes with vertices within your 10 mile radius. To be done properly, you owuld use the Haversine formula for the distance measurement. With some clever data structures, you can significant reduce the search space. Similarly, searches can be greatly speeded up by storing and initially comparing against zipcoe extents (North,West,East,South).

(*) Note: Technically zipcodes are NOT polygons! I know we all think of them like that, but really they are collections of data points (street addresses) and this is how the USPS really uses them. This means zipcodes can include other zipcodes; zipcodes can be made of multiple "polygons"; and zipcodes can overlap other zipcodes. Most of these situations should not be a problem, but you will have to handle zipcodes that can be defined as multiple polygons.

Selflove answered 16/11, 2010 at 0:35 Comment(0)
C
5

Firstly, you'll need a database of all zipcodes and their corresponding latitudes and longitudes. In Australia, there are only a few thousand of these (and the information is easily available), however I assume it's probably a more difficult task in the US.

Secondly, given you know where you are, and you know the radius you are looking for, you can look up all zipcodes that fall within that radius. Something simple written in PHP would be as follows: (apologies it's not in C#)

function distanceFromTo($latitude1,$longitude1,$latitude2,$longitude2,$km){
  $latitude1  = deg2rad($latitude1);
  $longitude1 = deg2rad($longitude1);
  $latitude2  = deg2rad($latitude2);
  $longitude2 = deg2rad($longitude2);
  $delta_latitude  = $latitude2  - $latitude1;
  $delta_longitude = $longitude2 - $longitude1;
  $temp = pow(sin($delta_latitude/2.0),2) + cos($latitude1) * cos($latitude2) * pow(sin($delta_longitude/2.0),2);
  $earth_radius = 3956;
  $distance = $earth_radius * 2 * atan2(sqrt($temp),sqrt(1-$temp));
  if ($km)
    $distance = $distance * 1.609344;
  return $distance;
}
Chromous answered 16/11, 2010 at 1:1 Comment(1)
AngryHacker: Luke has posted a PHP implementation of the Haversine formula that I mentioned.Selflove

© 2022 - 2024 — McMap. All rights reserved.