473,568 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Haversine SQL trouble - Distance between zip codes

I am trying to use the haversine function to find the distance between
two points on a sphere, specifically two zip codes in my database. I'm
neither horribly familiar with SQL syntax nor math equations :), so I
was hoping I could get some help. Below is what I'm using and it is,
as best as I can figure, the correct formula. It is not however,
giving me correct results. Some are close, others don't seem right at
all. Any ideas?
SET @lat1 = RADIANS(@lat1)
SET @log1 = RADIANS(@log1)
SET @lat2 = RADIANS(@lat2)
SET @log2 = RADIANS(@log2)
SET @Dlat = ABS(@lat2 - @lat1)
SET @Dlog = ABS(@log2 - @log1)
SET @R = 3956 /*Approximate radius of earth in miles*/
SET @A = SQUARE(SIN(@Dla t/2)) + COS(@lat1) * COS(@lat2) *
SQUARE(SIN(@Dlo g/2))
SET @C = 2 * ATN2(SQRT(@A), SQRT(1 - @A))
/*SET @C = 2 * ASIN(min(SQRT(@ A))) Alternative calculation*/

SET @distance = @R * @C
thnx,
cjrsumner
Jul 20 '05 #1
7 11011
ch*******@admin console.com (csumner) wrote in message news:<32******* *************** ****@posting.go ogle.com>...
I am trying to use the haversine function to find the distance between
two points on a sphere, specifically two zip codes in my database. I'm
neither horribly familiar with SQL syntax nor math equations :), so I
was hoping I could get some help. Below is what I'm using and it is,
as best as I can figure, the correct formula. It is not however,
giving me correct results. Some are close, others don't seem right at
all. Any ideas?
SET @lat1 = RADIANS(@lat1)
SET @log1 = RADIANS(@log1)
SET @lat2 = RADIANS(@lat2)
SET @log2 = RADIANS(@log2)
SET @Dlat = ABS(@lat2 - @lat1)
SET @Dlog = ABS(@log2 - @log1)
SET @R = 3956 /*Approximate radius of earth in miles*/
SET @A = SQUARE(SIN(@Dla t/2)) + COS(@lat1) * COS(@lat2) *
SQUARE(SIN(@Dlo g/2))
SET @C = 2 * ATN2(SQRT(@A), SQRT(1 - @A))
/*SET @C = 2 * ASIN(min(SQRT(@ A))) Alternative calculation*/

SET @distance = @R * @C
thnx,
cjrsumner


It would help if you could post your DECLARE statments (different data
types can affect calculations in various ways), as well as some sample
data for cases which give the results you want and for cases which
don't.

Simon
Jul 20 '05 #2
>> I am trying to use the haversine function to find the distance
between two points on a sphere, specifically two zip codes in my
database. <<

Do not re-invent (and have to maintain!!!) the wheel. KJL Software
(www.kjlsoftware.com) gives you 5-digit ZIP Code, City, State,USPS
Status Code, LATEST Area Code(s) from NANPA, Time Zone,Latitude, and
Longitude for all valid US Postal Service 5 digit ZIP Codes/City/State
combinations and a Distance Calculator.
Jul 20 '05 #3
Just for the record and in SQL/PSM:

CREATE FUNCTION Distance
(IN latitude1 REAL, IN longitude1 REAL,
IN latitude2 REAL, IN longitude2 REAL)
RETURNS REAL
AS
BEGIN
DECLARE r REAL;
DECLARE lat REAL;
DECLARE lon REAL;
DECLARE a REAL;
DECLARE c REAL;
SET r = 6367.00 * 0.6214;

-- calculate the Deltas...
SET lon = longitude2 - longitude1;
SET lat = latitude2 - latitude1;

--Intermediate values...
SET a = SIN(lat / 2) + COS(latitude1)
* COS(latitude2) * SIN(lon / 2)

--Intermediate result c is the great circle distance in radians...
SET c = 2 * ARCSIN(LEAST(1. 00, SQRT(a)))

--Multiply the radians by the radius to get the distance
RETURN (r * c)
END;

LEAST() function protects against possible roundoff errors that could
sabotage computation of the ARCSIN() if the two points are very nearly
antipodal. It exists as a vendor extension in Oracle, but can be
written with a CASE expression in Standard SQL.
Jul 20 '05 #4
Okay, below is the whole function. Here are some values and their
results compared to zipfind.net. Note the strange behavior for 32610 and
32611, and others had this same value too.

Search for 10 mile radius of 32601 and get distance between zip codes
(This post is only concerned with the distance function part)

my results (sample):
zip code miles latitude longitude
32601 0.0 29.68040999998 -82.345738999999 995
32602 4.6333652 29.629887 -82.396567000000 005
32604 8.0310783 29.573293 -82.397903999999 997
32610 0.49070904 29.68131199998 -82.353862000000 007
32611 0.49070904 29.68131199998 -82.353862000000 007
...

zipfind.com results:
zip code miles
32601 0.0
32602 5.5
32604 1.5
32610 1.3
32611 0.2
...

CREATE FUNCTION dbo.GetDistance (
@lat1 Float(8),
@log1 Float(8),
@lat2 Float(8),
@log2 Float(8)
)
RETURNS Float(8)
AS
BEGIN

DECLARE @distance Float(8)
DECLARE @R int
DECLARE @Dlog Float(8)
DECLARE @Dlat Float(8)
DECLARE @A Float(8)
DECLARE @C FLoat(8)

SET @lat1 = RADIANS(@lat1)
SET @lat2 = RADIANS(@lat2)
SET @log1 = RADIANS(@log1)
SET @log2 = RADIANS(@log2)
SET @Dlat = ABS(@lat2 - @lat1)
SET @Dlog = ABS(@log2 - @log1)
SET @R = 3956 /*Approximate radius of earth in miles*/
SET @A = SQUARE(SIN(@Dla t/2)) + COS(@lat1) * COS(@lat2) *
SQUARE(SIN(@Dlo g/2))
SET @C = 2 * ATN2(SQRT(@A), SQRT(1 - @A))
/*SET @C = 2 * ASIN(min(SQRT(@ A))) Alternative calculation*/

SET @distance = @R * @C
RETURN @distance
END
GO

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
I dont' understand this: How can these three zip code finding website
have such drastically different results???:
Are their databases so different? This first one doesn't quite match up
with my lat/log values.

www.kjlsoftware.com
32601 GAINESVILLE FL AD 352 -5 29.653195 -82.3244 0
32602 GAINESVILLE FL AD 352 -5 29.665245 -82.336097 1.0884
32603 GAINESVILLE FL AD 352 -5 29.653145 -82.346901 1.3501
32604 GAINESVILLE FL AD 352 -5 29.665245 -82.336097 1.0884
32605 GAINESVILLE FL AD 352 -5 29.676006 -82.368897 3.0994
32606 GAINESVILLE FL AD 352 -5 29.681426 -82.415022 5.7754
32607 GAINESVILLE FL AD 352 -5 29.646189 -82.396588 4.3583
32608 GAINESVILLE FL AD 352 -5 29.611545 -82.394108 5.0763

http://zipfind.net
1 32601 Gainesville FL 17,760 0.0 Alachua 352 Eastern
2 32602 Gainesville FL 0 5.5 Alachua 352 Eastern
3 32603 Gainesville FL 10,034 1.4 Alachua 352 Eastern
4 32604 Gainesville FL 0 1.5 Alachua 352 Eastern
5 32605 Gainesville FL 21,539 3.3 Alachua 352 Eastern
6 32606 Gainesville FL 19,662 6.3 Alachua 352 Eastern
7 32607 Gainesville FL 26,666 4.8 Alachua 352 Eastern
8 32608 Gainesville FL 39,781 4.9 Alachua 352 Eastern
and http://www.cryptnet.net/fsp/zipdy/ gives different results too.

These results seem to be off too far to be accounted for with simple
rounding errors and the like. Anyone know what's going on here?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #6

thnx, ill try this out

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #7
>> These results seem to be off too far to be accounted for with simple
rounding errors and the like. Anyone know what's going on here? <<

Nope, but perhaps one uses the location of the post office that serves
the zipcode and the other uses a map with the centroid of the territory?

Another thought is that if we want the for mailing purposes, you can get
a table of zones for each zip code.

--CELKO--
=============== ============
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3809
by: Xenophobe | last post by:
I have successfully created a zip code radius search, but the performance is unacceptable. I have two tables. The first is 52K zip codes w/lat and long. The second is 3K national business addresses. Using the zip code 10010, it takes almost 60 seconds to return 122 businesses within a 25 mile radius. This slowdown is almost entirely due...
1
1913
by: Galsaba | last post by:
I am trying to find a software that can find distance between two ZIP codes. I think most of them do it by having the longtitude and latidute of each ZIP, and substract. But this gives the air distance. Where can I find software that find the ground distance (like in Mapquest). Thanks, Joe
2
1757
by: Galsaba | last post by:
anyone knows what the formula is for finding a distance betweeen 2 zip codes? Aaron
4
2729
by: Dave \IT\ | last post by:
I'm looking to find out how I'd go about setting up a database where a visitor to my site could punch in their postal code, and find out how far they are from another postal code. For example, AutoTrader has this feature I believe to tell you how far the vehicle is from you. Dating sites have them so you can do proximity searches. Anyone...
7
6761
by: Aric | last post by:
Hi, I'm a bit new to programming in general, really I've just picked it up as a hobby, and so I've started by reading "Practical C Programming" by Steve Oualline. Things have been going fine in the book until I reached exercise 6-1 in the book, which reads: Write a program to find the square of the distance between two points. (For a...
1
1540
by: mahsa | last post by:
Hi,i want to give aa zipcode and find the distance between 2 zipcodes do you have any idea?its better i I can find some thing free but not necessar tanck you
9
3435
by: Sandy | last post by:
Hello - I need either a cheap tool or code & DB that calculates, eg. within 50-mile radius of a zip code. Anyone have any suggestions? -- Sandy
4
1623
by: Dave | last post by:
Hi I want to add functionality to my site so that customers can type in a postcode and we can tell them the "nearest store" in our database. I have noticed there are loads of sites on the net that offer this service but some of the them are small back bedroom sites. Do they really pay for the expensive postcode data or is there an...
2
1407
by: Samuel Shulman | last post by:
Hi I need to measure the distance using 2 postcodes (UK) How can that be done? Thank you, Samuel
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7660
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.