473,378 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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(@Dlat/2)) + COS(@lat1) * COS(@lat2) *
SQUARE(SIN(@Dlog/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 10989
ch*******@adminconsole.com (csumner) wrote in message news:<32**************************@posting.google. 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(@Dlat/2)) + COS(@lat1) * COS(@lat2) *
SQUARE(SIN(@Dlog/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.345738999999995
32602 4.6333652 29.629887 -82.396567000000005
32604 8.0310783 29.573293 -82.397903999999997
32610 0.49070904 29.68131199998 -82.353862000000007
32611 0.49070904 29.68131199998 -82.353862000000007
...

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(@Dlat/2)) + COS(@lat1) * COS(@lat2) *
SQUARE(SIN(@Dlog/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
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...
1
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...
2
by: Galsaba | last post by:
anyone knows what the formula is for finding a distance betweeen 2 zip codes? Aaron
4
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,...
7
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...
1
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
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
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...
2
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.