Solved

Need a great circle calculation

Posted on 2003-10-22
11
556 Views
Last Modified: 2008-01-09
Hey all

I need a great circle calculation for vbscript.
I just got finished with determining a "nearest store locator" thanks to some help here but I am not smart enough to figure out all the "great circle"  info out there...

I have 4 coordinates

Originating Lat/Long
and
Destination Lat/Long

I need to know how to calculate the distace between those two points.

I have read about the Great Circle computation method.. but just can't wrap my head around it and I can't seem to tailor the web examples I have found into a useable VBscript.
anyone got anything?

0
Comment
Question by:gormly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 4

Expert Comment

by:brother7
ID: 9602323
I found this page which gives formulas for calculating Great Circle, using both radians and degrees.
http://www.meridianworlddata.com/HTML3/great-circle-distance-3.asp

From that page:

Great Circle Distance Formula using radians:
3963.0 * arccos[sin(lat1) *  sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1)]

Great Circle Distance Formula using decimal degrees:
3963.0 * arccos[sin(lat1/57.2958) * sin(lat2/57.2958) + cos(lat1/57.2958) * cos(lat2/57.2958) *  cos(lon2/57.2958 -lon1/57.2958)]
OR
r * acos[sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1)]

Where r is the radius of the earth in whatever units you desire.
r=3437.74677 (statute miles)
r=6378.7 (kilometers)
r=3963.0 (normal miles)


Since there's no arccos function in VB, you'll have to use the arctan (Atn, in VB) version, as described on the page.

Assume long/lat is in radians and the result GD is in normal miles.  In VBScript, try this:

<SCRIPT LANGUAGE="VBScript">
<!--
Dim lon1, lat1, lon2, lat2, x, GD

x = sin(lat1) *  sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1)
GD = 3963.0 * atn(sqrt(1-x^2))/x)
-->
</SCRIPT>

Hope that helps.
0
 
LVL 1

Author Comment

by:gormly
ID: 9606011
I am getting some screwy numbers


(my variable names)

Origin: +41.144496 (tLong) -073.343261  (tLat)
Destination: +41.571496 (tLong2) -73.044035  (tLat2)

my code is:

x = sin(tLat) *  sin(tLat2) + cos(tLat) * cos(tLat2) * cos(tLong2- tLong)
GD = 3963.0 * atn(sqr(1-x^2))/x

which translates to:

x = sin(-73.044035) *  sin(-073.343261) + cos(-73.044035) * cos(-073.343261) * cos(41.571496  - 41.144496)
GD = 3963.0 * atn(sqr(1-x^2))/x

I get:
1544.11124783767  Not hardly since it is about 40 miles away.
What am I doing wrong here?

(had to change to sqr cause sqrt isn't valid in vbscript)

I already tried something like that before with similar results.. maybe my values aren't correct?
Whats a Radian?
0
 
LVL 1

Author Comment

by:gormly
ID: 9606129
I spent some time on that page and I tried this as well:

x = 69.1 * (tlat2 - tlat)
y = 69.1 * (tlong2 - tlong) * cos(tlat/57.3)
GD = sqr(x * x + y * y)

and STILL get the wacky numbers.

This is frustrating.
I am missing something , I know it.

I have tried MANY variations on what the page is describing and cannot get this to be accurate.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:brother7
ID: 9606234
It looks like your original data is given in degrees, not radians.  The algorithm I gave applies if the data is in radians.  There's a different formula to use if the data is in degrees.

A quick tutorial on degrees and radians...
If you travel the circumference of a circle, you travel 360 degree.  This is also equal to 2 * PI radians.
To convert from degrees to radians, you multiply by (2 * PI) / 360 or PI / 180 , which approximately equals 1 / 57.2958.
In other words, to convert from degrees to radians, you divide by 57.2958.

Example:
41 degrees = 41/57.2958 radians = 0.7156 radians

Lemme try using the algorithm for degrees on your data and I'll let you know what I come up with.
0
 
LVL 1

Author Comment

by:gormly
ID: 9606335
cool cause this is driving me nuts..

I have tried everything (I am not a math guy as you can tell)

also I will up the points for the extra effort, I am REALLY glad I am a member here.
0
 
LVL 4

Accepted Solution

by:
brother7 earned 100 total points
ID: 9606356
Since I'm using VB.NET now, I tried coding the algorithm using your initial data.  The result I got was 22.389071 mi or 36.036631 km.  Does that look correct?  VB.NET is similar to VBScript, so you should be able to understand it.  I've attached my code below.

--- Code Start ---

   Sub Main()
      Const DegToRad As Double = 57.29578
      Dim tLong, tLat, tLong2, tLat2, x, GD As Double

      tLong = 41.144496
      tLat = -73.343261
      tLong2 = 41.571496
      tLat2 = -73.044035

      x = (Math.Sin(tLat / DegToRad) * Math.Sin(tLat2 / DegToRad)) + _
      (Math.Cos(tLat / DegToRad) * Math.Cos(tLat2 / DegToRad) * Math.Cos(tLong2 / DegToRad - tLong / DegToRad))

      GD = 3963.0 * Math.Atan(Math.Sqrt(1 - x ^ 2) / x)

      Console.WriteLine(GD)

   End Sub ' Main

--- Code End ---
0
 
LVL 4

Expert Comment

by:brother7
ID: 9606688
Just for kicks, I did the same calculations for the 2 approximation formulas given at the top of the webpage.  The results were:

1st approximation = 30.654209 mi
2nd approximation = 22.339349 mi
true Great Distance = 22.389071 mi

In addition, I plugged your numbers into an online Great Circle calculator, found at http://www.gb3pi.org.uk/great.html
and it said 22.4 mi.
0
 
LVL 1

Author Comment

by:gormly
ID: 9606791
Looks pretty close to me
I took the milage and compared it to a map search at mapquest and it looks right
(obviously it is "off" due to driving distances and not "as the crow flies")

Thanks so much!

I ended up using:
    DegToRad = 57.29578
      Dim x, GD

      x = (Sin(tLat / DegToRad) * Sin(tLat2 / DegToRad)) +(Cos(tLat / DegToRad) * Cos(tLat2 / DegToRad) * Cos(tLong2 / DegToRad - tLong / DegToRad))
      GD = 3963.0 * atn(Sqr(1 - x ^ 2) / x)

fantastic, thank you so much for saving me time...
No on to find the latest Zip Code database with Lat/Lon for free!!
I got one at the census from 1999 but it is outdated.

Thanks much!
points are doubled

although still not what this was worth (a lot) , sorry gotta be prudent, I have a lot of questions :>  
0
 
LVL 4

Expert Comment

by:brother7
ID: 9606956
Thanks for the points :)

I've got WAY too much time on my hands... did a little surfing and came up with this:
http://www.cfdynamics.com/zipbase/
http://www.kchost.net/media/  (look at ZipCodes.mdb)

Enjoy!
0
 
LVL 4

Expert Comment

by:brother7
ID: 9606966
0
 
LVL 1

Author Comment

by:gormly
ID: 9607000
WOW!  Cool  thanks.

and to think, some companies want to charge big bucks for this!
bassttards!

thanks for the extra effort, you should be bottled and sold on the open market in a late night infomercial as..

"The Ultimate Time saver!"

act now.. limited time offer.. operators standing by.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP Button to clear text 4 67
edit .asp files 5 47
Clear input text 15 26
ASP AND XML 3 30
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question