TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Locations based on ZIP code... (http://www.talkphp.com/absolute-beginners/4206-locations-based-zip-code.html)

allworknoplay 05-01-2009 02:36 PM

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)

Village Idiot 05-01-2009 05:18 PM

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.

allworknoplay 05-01-2009 06:18 PM

Quote:

Originally Posted by Village Idiot (Post 23544)
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..

Wildhoney 05-01-2009 06:21 PM

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.

allworknoplay 05-01-2009 06:24 PM

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 05-01-2009 06:29 PM

Quote:

Originally Posted by Wildhoney (Post 23546)
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.

Wildhoney 05-01-2009 06:49 PM

  • 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)

allworknoplay 05-01-2009 06:59 PM

Quote:

Originally Posted by Wildhoney (Post 23550)
  • 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.... :-D

allworknoplay 05-01-2009 07:06 PM

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%)

Salathe 05-01-2009 08:03 PM

By "1990 Population" it means, the population in the year 1990 (that is when all this data is from).

sketchMedia 05-01-2009 08:07 PM

Yes, from the 1990 census im guessing.

Village Idiot 05-01-2009 08:09 PM

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

allworknoplay 05-01-2009 08:13 PM

Quote:

Originally Posted by Salathe (Post 23553)
By "1990 Population" it means, the population in the year 1990 (that is when all this data is from).

Quote:

Originally Posted by sketchMedia (Post 23554)
Yes, from the 1990 census im guessing.

Quote:

Originally Posted by Village Idiot (Post 23555)
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...

Village Idiot 05-01-2009 08:16 PM

Quote:

Originally Posted by allworknoplay (Post 23556)
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.

allworknoplay 05-01-2009 08:36 PM

Quote:

Originally Posted by Village Idiot (Post 23557)
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....

^^

Wildhoney 05-01-2009 09:09 PM

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!

allworknoplay 05-01-2009 09:12 PM

Quote:

Originally Posted by Wildhoney (Post 23559)
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....

Wildhoney 05-02-2009 12:02 AM

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.

Village Idiot 05-02-2009 01:37 AM

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.

allworknoplay 05-05-2009 08:28 PM

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...


All times are GMT. The time now is 03:55 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0