TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-01-2009, 02:36 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default Locations based on ZIP code...

Hey guys,

I don't want the code, but would like to generally get an idea of how this works.

You know how when you search for a car, it asks for your zip code? And then it allows you to choose 5 miles radius from you? 10 miles, 20 miles etc...

I was just thinking, how exactly does that work? Say you live in Beverly Hills 90210.

So now I have an account profile that shows your postal code is 90210.

I can have another table called "locations" that would have the 90210 zip code too, but that wouldn't necessarily tell me if those locations were 1, 5 10, 30 miles near me...

So what is the "design" algorithm for displaying radius?
Is this something where I have to use Google's map API to do some fancy calculation? (i'm not sure google's map api even exist, just theorizing)
allworknoplay is offline  
Reply With Quote
Old 05-01-2009, 05:18 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

I've never done this, so this is just me theorizing. Zip codes do not indicate a location by themselves. You have to have a database that have the zips and their corresponding GPS locations (many are for sale, a free one is available at http://www.census.gov/tiger/tms/gazetteer/zips.txt ). Then using basic algebra, you use the following formula

r^2 <= (x-h)^2 + (y-k)^2

r being your radius, x and y being your testing points and h and k being your zip codes location. If a real number is returned x and y are within your circular radius.

There are probably algorithms to ensure you are only checking ones in the right geographic area.
__________________

Village Idiot is offline  
Reply With Quote
Old 05-01-2009, 06:18 PM   #3 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
I've never done this, so this is just me theorizing. Zip codes do not indicate a location by themselves. You have to have a database that have the zips and their corresponding GPS locations (many are for sale, a free one is available at http://www.census.gov/tiger/tms/gazetteer/zips.txt ). Then using basic algebra, you use the following formula

r^2 <= (x-h)^2 + (y-k)^2

r being your radius, x and y being your testing points and h and k being your zip codes location. If a real number is returned x and y are within your circular radius.

There are probably algorithms to ensure you are only checking ones in the right geographic area.
Thanks VI, I'll check out that link.

So you think GPS is the key huh? If I can whip something up real quick I'll be sure to post it here..
allworknoplay is offline  
Reply With Quote
Old 05-01-2009, 06:21 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I used to have a database of all the UK postcodes, and the distances were calculated using the longitude and latitude values.

The algorithm was fairly simple, but I'm unable to locate the script I did at the moment.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-01-2009, 06:24 PM   #5 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Oh and thanks for the link, I didn't know this data was free, I actually had my company PAY for ZIP codes and....drum roll....it doesn't even include GPS coordinates!!!
allworknoplay is offline  
Reply With Quote
Old 05-01-2009, 06:29 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
I used to have a database of all the UK postcodes, and the distances were calculated using the longitude and latitude values.

The algorithm was fairly simple, but I'm unable to locate the script I did at the moment.

Ohhh!! I don't want the script, since you say it's fairly simple I hope I can learn to make my own....just some guidance?

From the link that VI provided, here's the layout, but I don't know what is what?

Code:
"01","35004","AL","ACMAR",86.51557,33.584132,6055,0.001499
There's 8 columns, I'll try to guess what they are:

#1 = 01 --> not a clue? But it doesn't look all that important.
#2 = 35004 --> ZIP code
#3 = AL --> This is the state.
#4 = ACMAR --> This is the town
#5 = 86.51557 --> nope, don't know.
#6 = 33.584132 --> nope, don't know.
#7 = 6055 --> nope, don't know.
#8 = 0.001499 --> nope, don't know.
allworknoplay is offline  
Reply With Quote
Old 05-01-2009, 06:49 PM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

  • Field 1 - State Fips Code
  • Field 2 - 5-digit Zipcode
  • Field 3 - State Abbreviation
  • Field 4 - Zipcode Name
  • Field 5 - Longitude in Decimal Degrees (West is assumed, no minus sign)
  • Field 6 - Latitude in Decimal Degrees (North is assumed, no plus sign)
  • Field 7 - 1990 Population (100%)
  • Field 8 - Allocation Factor (decimal portion of state within zipcode)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
allworknoplay (05-01-2009)
Old 05-01-2009, 06:59 PM   #8 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
  • Field 1 - State Fips Code
  • Field 2 - 5-digit Zipcode
  • Field 3 - State Abbreviation
  • Field 4 - Zipcode Name
  • Field 5 - Longitude in Decimal Degrees (West is assumed, no minus sign)
  • Field 6 - Latitude in Decimal Degrees (North is assumed, no plus sign)
  • Field 7 - 1990 Population (100%)
  • Field 8 - Allocation Factor (decimal portion of state within zipcode)

Thank you so kindly....
allworknoplay is offline  
Reply With Quote
Old 05-01-2009, 07:06 PM   #9 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Hey WH:

What do you mean by 1990 population?
How did you get the 1990? And I am assuming it's 1,990....


Field 7 - 1990 Population (100%)
allworknoplay is offline  
Reply With Quote
Old 05-01-2009, 08:03 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

By "1990 Population" it means, the population in the year 1990 (that is when all this data is from).
Salathe is offline  
Reply With Quote
Old 05-01-2009, 08:07 PM   #11 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Yes, from the 1990 census im guessing.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-01-2009, 08:09 PM   #12 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Meaning the data is nearly 20 years old, it is probably too inaccurate to use.
__________________

Village Idiot is offline  
Reply With Quote
Old 05-01-2009, 08:13 PM   #13 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
By "1990 Population" it means, the population in the year 1990 (that is when all this data is from).
Quote:
Originally Posted by sketchMedia View Post
Yes, from the 1990 census im guessing.
Quote:
Originally Posted by Village Idiot View Post
Meaning the data is nearly 20 years old, it is probably too inaccurate to use.
WOW, that is OLD....oh well I don't need population numbers anyways, and zip codes don't change that often....I should be ok...
allworknoplay is offline  
Reply With Quote
Old 05-01-2009, 08:16 PM   #14 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
WOW, that is OLD....oh well I don't need population numbers anyways, and zip codes don't change that often....I should be ok...
20 years is a very long time, they are probably quite different. Many areas that where not populated in 1990 are quire populated now, which would change zip codes by way of introducing new ones.

But for learning purposes, this should do.
__________________

Village Idiot is offline  
Reply With Quote
Old 05-01-2009, 08:36 PM   #15 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
20 years is a very long time, they are probably quite different. Many areas that where not populated in 1990 are quire populated now, which would change zip codes by way of introducing new ones.

But for learning purposes, this should do.
Exactly, I'm just messing around, this isn't part of any project for work or anything else of any importance....

allworknoplay is offline  
Reply With Quote
Old 05-01-2009, 09:09 PM   #16 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Off topic:
Just don't be surprised if the zip codes of New Orleans still show up as being in New Orleans.


Please keep us up-to-date with this research of yours!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-01-2009, 09:12 PM   #17 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Off topic:
Just don't be surprised if the zip codes of New Orleans still show up as being in New Orleans.


Please keep us up-to-date with this research of yours!
Thank you WH:

I will try to put up a page for you guys to play with. Any code I come up with I will most definitley share with you guys...

The only thing is that it's US based...I guess I need to think bigger and cover the world!???

Small steps first....
allworknoplay is offline  
Reply With Quote
Old 05-02-2009, 12:02 AM   #18 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You're right. I suppose either GPS co-ordinates or traditional longitude and latitude would be more than sufficient. It's just getting the data -- the zip codes and postcodes (and any others that other countries use) to correlate with either of the two aforementioned.

I don't think it's going to be easy!

However, once you have that data, the zip codes and postcodes become one (not in a Buddhist sense) and become irrelevant insofar as the algorithm is concerned. Those codes are used to retrieve L&L or GPS co-ordinates and the system uses those to calculate distance.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-02-2009, 01:37 AM   #19 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Zip codes are mainly for the US postal service, towns are a better way to go worldwide.

Proficiently doing towns in the world would require a well designed database and an optimized script because you would be working with tons of different locations and you have to compare them against each other (a good narrowing algorithm would be required).

I would stick to zip codes for now, but having something that could do a good portion of the world would be an awesome project to take on. As wild put it though, zip codes are just a way to reference to the GPS, so you could easily use the same theory to power a town based search.
__________________

Village Idiot is offline  
Reply With Quote
Old 05-05-2009, 08:28 PM   #20 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

After doing some research on this, this can get extremely complicated. I've seen some code that use COS, SIN and other types of math formulas I haven't done since high school..

Anyways, for you UK folks, I found this website if any of you are interested.

http://www.milldo.com/en16_uk_post_codes_database.htm

And here is the formula that seems to be the most accurate..

http://en.wikipedia.org/wiki/Haversine_formula


It's all just a bit too much for my current skillsets right now...
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tips to Improve Your Coding and Projects Village Idiot Tips & Tricks 45 12-03-2012 07:46 AM
Writing Clean Code Village Idiot Tips & Tricks 10 06-25-2012 12:35 PM
Font color for key based on modif date ? Peuplarchie General 1 04-14-2009 01:37 PM
I love maintaining legacy code (priceless snippet of ASP code) Village Idiot The Lounge 10 04-03-2009 09:20 PM
Snipply.com - Code snippet website codyodell Show Off 27 04-13-2008 02:09 PM


All times are GMT. The time now is 02:29 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design