Re: [Re: calculating distance based on zip/postal codes]

by Zach Kenyon <zantispam(at)netscape.net>

 Date:  15 May 00 13:51:08 CDT
 To:  hwg-business <hwg-business(at)hwg.org>
  todo: View Thread, Original
"Capt. Ron" <strays(at)bellsouth.net> wrote:

> Good Lord !!!!!!
> Okay, with the above calculation in mind, what's the distance between =

> my house in zip  code xxxxx and the City Hall in the zip code next to =

> mine  ?????

D=3D 6,370,997*arcos(sin(LAT1)*sin(LAT2) + cos(LAT1)*cos(LAT2)*cos(LONG1-=
LONG2))


WHERE: =

D =3D distance in meters =

LAT1 =3D latitude of point1 (in radians) =

LONG1 =3D longitude of point1 (in radians) =

LAT2 =3D latitude of point2(in radians) =

LONG2 =3D longitude of point2 (in radians) =

arcos =3D arc cosine
cos =3D cosine
sin =3D sine
and 6,370,997 is the radius of the sphere in meters. =

Latitude and longitude can be converted from degrees to radians by dividi=
ng by
57.29577951. =


(From the Calculating Distance to Hospital page,
http://www.umanitoba.ca/centres/mchpe/concept/dict/hosp_distance.htm)

Though I believe that this is the arc-cosine rule that Galkowski advised
against.

http://www.4guysfromrolla.com/webtech/010500-1.shtml appears to be a bett=
er
algorithm...nope.  My bad.  It is, however, an implementation in asp that=
 may
serve the prupose of the original poster.

*dig-dig*

This site
(http://oak.oakland.edu/pub/hamradio/arrl/infoserv/techmisc/dx_w2iol.txt)=
 has
a wealth of information on how to calculate distance based on latitude an=
d
longitude.  http://www.usgs.gov/ has latitude and longitude for just abou=
t
every feature, rock, zip, bush, shrub, and tree in the US. =

http://GeoNames.NRCan.gc.ca/english/ has the same information for Canada.=
 =

http://www.ngs.noaa.gov/PC_PROD/pc_prod.shtml is a list of software that
calculates neat things like this.  Good ol' Xerox PARC
(http://pubweb.parc.xerox.com/map/) has a map finder (may be useful). =

http://www.indo.com/distance/ calculates distance, but doesn't do zip cod=
es.

> If you are going to use latitude and longitude as in GPS, then zip =

> codes have nothing to do with it.

Right and wrong.  Right because with lat/long you no longer need zip code=
s. =

Wrong because you need the zip codes to determine which part of the globe=
 to
get you lat/long from.  (The only thing worse than infinite recursion is.=
=2E.)

My suggestion would be to grab lat/long info for all zip codes.  Calculat=
e the
positions based on the center of the zip (or weighted based on population=

center, if you prefer...), then stick all three numbers into a database. =
 Do a

`SELECT * FROM TableName WHERE Zip =3D FirstZip AND Zip =3D LastZip'  =

That gives you your positions for both zip codes.  Then do something like=

$rad =3D 57.29577951;
$pi =3D 3.14159265358979323846;
$LAT1 =3D (rst("LAT1") / $rad);
$LAT2 =3D (rst("LAT2") / $rad);
$LONG1 =3D (rst("LONG1") / $rad);
$LONG2 =3D (rst("LONG2") / $rad);
$D =3D 6,370,997 * Acos((Sin($LAT1) * Sin($LAT2)) + (Cos($LAT1) * Cos($LA=
T2) *
Cos($LONG1-$LONG2)));
//convert to miles
$D =3D ((1.852 * 60.0 * (($D / $pi) * 180)) / 1.609344);
return $D;

include("StandardDisclaimer.inc");

Do note: this is the `Do-it-yourself'(tm) option.  There are other tools =
out
there to calculate things like shipping distances and the like.  Also, YM=
MV. =

I haven't tried any of this.  But the challenge seems fascinating, so I
decided to jump in :-)



--Jedi Hacker(apprentice) and Code Poet

____________________________________________________________________
Get your own FREE, personal Netscape WebMail account today at http://webm=
ail.netscape.com.

HTML: hwg-business mailing list archives, maintained by Webmasters @ IWA