Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 3rd, 2008, 05:03 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 198
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via MSN to itHighway
Default US zip codes - distance calculator

I have a database having, US zipcodes, longitude and latitude. What I want to do is LIST THOSE ZIPCODES which are at a distance of given miles from a particular zip code.

For Exmaple:
I want to find the zip codes which are at a distance of 15 miles from zipcode 10001 (NYC).


I have the script that uses Zipcodes, latitudes and longitudes of two cities and caculate the distance. But it calculates distance.

www.ithighway.co.uk
 
Old June 3rd, 2008, 07:46 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I wrote a similar application a few years ago but I can't seem to find the script at the moment to share the equation with you. What I employed was a bit of Trig to get the distance between points x1,y1 & x2,y2; the minimal short coming of this was that it is an "as the crow flies" distance measurement so the aforementioned points may be 10 driving miles apart but only 9.5 miles as the crow flies.

Further, this is not something you are going to want to do on the fly since this calculation can be quite intensive since you would need to compare 1 set of points to all the other points in your database (I think my Zip Code database was about 90K but I don't remember exactly).

I would suggest writing a sproc that would do this calculation on all the zip codes in your database and then drop the results into another table with a structure similar to:

OriginZipCode 15MileZipCode
10001 10001,10002,10003

You could then wire your UI up to simply select the 15MileZipCode column based on your OriginZipCode, Split() the results and then do any further processing. If I can turn up my old script I will be happy to share it with you but, for the time being, this should give you some direction.

http://codeidol.com/sql/sql-hack/Num...GPS-Locations/

hth.
-Doug



================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old June 3rd, 2008, 12:45 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

You mean something like this?

http://www.mywhizbang.com/zipLocator...+in+that+range

Although given that this is NYC, even a radius of *ZERO* miles gives you more zips than I map for you!
 
Old June 3rd, 2008, 12:48 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Incidentally, that's all done with an Access DB. The "trick" is in pre-calculating certain values and stuffing them in the DB, rather than going with raw latitude/longitude.

And that's running on a GoDaddy.com shared host, so you *know* it's underpowered. And yet it still performs adequately.
 
Old June 3rd, 2008, 02:04 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 198
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via MSN to itHighway
Default

Yes exactly like this one.

www.ithighway.co.uk
 
Old June 3rd, 2008, 02:21 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I'll send you the code when I get home tonight. Your email address in your profile is correct?

As I said, the only major "trick" here is adding a couple of fields to the DB.

I paid a whole $5 for the DB and can't redistribute it, so you'll need to pay your own $5 to get it. Hope that won't break you.
 
Old June 3rd, 2008, 02:25 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 198
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via MSN to itHighway
Default

The email is [email protected], it's also in my profile. I can pay for the db. Thanks!

www.ithighway.co.uk
 
Old June 3rd, 2008, 02:42 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 198
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via MSN to itHighway
Default

From where can I get the DB?

www.ithighway.co.uk
 
Old June 3rd, 2008, 07:40 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

For everyone else who is not getting a copy of the source code, here is the calculation that I used. You can determine the (rough) distaince between 2 sets of Lat. and Long. points using the following code:

Const radian = .01745329252
Dim x1, y1, x2, y2 'x = Lat. y = Long.
Dim TotalDistance, TempDistanceValue

'This operation could be preformed in the following equation but doing it this way simplifies the equation.
x1 = x1 * radian
x2 = x2 * radian
y1 = y1 * radian
y2 = y2 * radian

TempDistanceValue = (sin(x1) * sin(x2)) + (cos(x1) * cos(x2)) * cos(abs(y1 - y2))
TempDistanceValue = atn(-TempDistanceValue / Sqr(-TempDistanceValue * TempDistanceValue +1)) + 2 * Atn(1)
TotalDistance = (TempDistanceValue / radian) * 80

hth.
-Doug


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old June 3rd, 2008, 07:58 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I'll post the code on that site in the next day or two (put a link to "View Source" at the bottom of the page and include a link to a small sample ".mdb" file).

But...

But the formula you are using there is really WAY over-complex!

You would use it when you needed accuracy to a few meters and/or good accuracy over distances of more than 500 or more miles (800 KM).

It's called the "Haversine Formula" and is used for accurately measuring distances on a sphere. But the Earth is *NOT* a sphere, so even it will introduce inaccuraces on the order of a kilometer or so when talking about trans-continental distances.

The real reason it's way overkill is simply that it is about 1000 times more accurate than the latitudes and longitudes you get from typical zip code databases!!

Consider my own situation: The latitude/longitude listed for my zip code is about 3 miles from me. And the zip code for the nearest town is listed as about 12 miles from that. In reality, the town is only 6 miles from me! Further, when I do a radius calculation ("get all zips within 10 miles of me") that *misses* ALL THREE of the closest towns!! All because the lat/long that is in the "books" for a given zip code is (typically) the geographically weighted "center" of the zip code area. If you, like I, live near the edge of the zip code area, and if you don't happen to live in downtown New York...well, it's pointless to use the Haversine Formula to get within a few meters when the data in the DB is off by KILOMETERS!!

So...

Stick with the pythagorean theorem. You can then do the calcs in the SQL query and get reasonably good performance!!

I'll show the other (really minor) tricks I pull when I post the code.





Similar Threads
Thread Thread Starter Forum Replies Last Post
calculator mojtaba rashidi Visual Studio 2005 0 March 17th, 2008 07:29 AM
Need a zip code radius finder and calculator. baburman ASP.NET 2.0 Professional 4 December 10th, 2007 10:14 AM
Calculator DweeLer Other Programming Languages 1 November 18th, 2005 08:13 AM
calculator kale_tushar C++ Programming 1 January 28th, 2004 01:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.