Finding all zip codes in a given radius

5,521 views
Skip to first unread message

trouble

unread,
May 20, 2011, 9:50:41 AM5/20/11
to google-map...@googlegroups.com
Problem-->
Given a zip code (i.e. 90210), find all the zip codes within a given radius (10 miles).

Brainstorm-->
So, I've read up a couple of different threads on this, namely here and here.

But I still find myself scratching my head and want to walk away in frustration.  There doesn't seem to be a 1-2-3 process to do this.

I suppose I should establish some initial conditions:

I'm using SQL Server 2008 R2, but the table does not utilize spatial data.  It does contain centroid (lng/lat) point for each zip and it can also map zip code boundaries.  Instead of using spatial data to map the zip code boundary, however, it uses many, many longitude and latitude points.

So, let's say I map a circle on a map, where the circle has a radius of 10 miles.  So, area = pi * r^2 = pi * (10 miles * 1609.344 meters / 1 mile)^2 = 258998811.0336pi meters and circumference = 32186.88pi meters. 

However, since this is a map of Planet Earth, we're talking about a 3-D image mapped onto a 2-D plane, and straight 2-D formulas may not be entirely accurate or correct, perhaps.  Furthermore, the area and circumference of the circle don't reveal a wealth of information about its coordinates within.  I can have an infinite amount of coordinates on a circle and certainly inside of it.

So, where and how should I be devoting my time and efforts?  Should I be looking for where the circle touches the zip code region (i.e. polygon) or where the circle touches the centroid of the zip (i.e. lat/lng), based on the information given above?

With either method, I am not entirely confident as to how to proceed and what formula(s) to use.  With the centroids, I'd have to calculate the radius from the center of the circle to every centroid of every zip code in the database and return those which are less-than-or-equal to that of the circle's.  If that is the case, that seems like it's computationally expensive.  Is there a more efficient way?

If I use the polygons, and unless someone knows of a way to convert typical float/decimal to spatial data, this seems complex and I'm not even sure where to start.  I suppose I'd have to figure out the bounds of the polygon and then make some determination about if edge of the circle contains all or part of this polygon.  That all sounds good, in theory.  But how do I apply that to the google maps api and sql?

Thank you.

circle.jpg

Rossko

unread,
May 20, 2011, 10:51:44 AM5/20/11
to Google Maps JavaScript API v3
> With the centroids, I'd have to calculate the
> radius from the center of the circle to every centroid of every zip code in
> the database and return those which are less-than-or-equal to that of the
> circle's.  If that is the case, that seems like it's computationally
> expensive.

It isn't, its a simple distance calculation, the sort of thing
computers are good at.

You might improve efficiency for very large searches by using
rectangular bounds of your circle, and a simple numeric comparison to
see if a target is inside. If the circular part is important, do the
distance calculations just on those candidates.

If you want to go the polygon way, you'll need to create polygons of
course (do once and store?). Algorithms are available for distance to
poly, efficiency can be improved with bounds again.

JKurtock

unread,
May 20, 2011, 10:58:13 AM5/20/11
to Google Maps JavaScript API v3
Finding the Centroids of Zip codes (if you already have the centroid
-- see below) within 10 miles would not be too difficult. As a first
cut, I would sort two lists of centroids, one by lat and one by lng.
Find those centroids within 10 miles both by lat and lng (that would
find them within a 20 mile edge box, but the Google Maps geometry
library could do the final check).

But it sounds like you want to also find zip codes where any part of
the zip code is within 10 miles, even if the centroid is not. So look
for all centroids within, say, 30 miles. Then you will need to search
all the points in the perimeter polygon (using a similar algorithm) to
find any polygon with an edge point within 10 miles.

You indicate that you already have a database with zip code centroids
and complete polygon data. Keep in mind that any such data is an
approximation, because zip codes are laid out for the purposes of the
USPS, not as mapping "shape." http://www.census.gov/geo/ZCTA/zcta.html
But I'm going to guess you do not need house-level accuracy.

- Jeff

On May 20, 6:50 am, trouble <mrok...@gmail.com> wrote:
> Problem-->
> Given a zip code (i.e. 90210), find all the zip codes within a given radius
> (10 miles).
>
> Brainstorm-->
> So, I've read up a couple of different threads on this, namely here<http://groups.google.com/group/google-maps-api/browse_thread/thread/4...>and
> here<http://groups.google.com/group/google-maps-api/browse_thread/thread/e...>
> .
>  circle.jpg
> 24KViewDownload

Nathan Raley

unread,
May 20, 2011, 11:26:59 AM5/20/11
to google-map...@googlegroups.com
It is more or less as the two above you pointed out, and in reality only gets more complex the more accurate you want.

The easiest clean cut approach would just to bound off the position you want as your center point and grab all the lat and lon that fall within a rectangular area from that point of origin.  Now this isn't circular, which is what you wanted, but gives you a pretty good approximation.

My recommendation would be to as Rossko said, use a simple distance calculation.  There are many variations of this floating around that can convert a lat/lon to distance and vice versa, if your having a hard time finding one just ask and someone can provide you with one.  

I'd use this as the primary source to check for your criteria (being within a certain radius of a central point), but you could use the bounding box method I mentioned above to perform this calculation on a much smaller range of points if you are worried about performance.  Just grab the top left and bottom right corners and do a simple select statement filtering out all those that don't fall within your bounding box, then perform your calculation based on these results.

Hope this helps.


--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


davie

unread,
May 21, 2011, 4:17:59 AM5/21/11
to Google Maps JavaScript API v3
Hi
As you are using an SQL database you should be using an SQL statement
as this is the most effective way of extracting data from a database.
The following link is for an MySQL database but you should be able to
change it to suit your database.
http://code.google.com/apis/maps/articles/phpsqlsearch_v3.html#findnearsql
Regards Davie
Reply all
Reply to author
Forward
0 new messages