Re: geography math?
- From: David Gravereaux <davygrvy@xxxxxxxxx>
- Date: Sun, 11 Dec 2005 18:04:46 -0800
David Gravereaux wrote:
> Helmut Jarausch wrote:
>
>>David Gravereaux wrote:
>>
>>
>>>I found this for calculating distance from 2 points given geographical
>>>coordinates, but it doesn't appear to give correct results at first
>>>glance.
>>>
>>>proc geographical_distance {lat1 long1 lat2 long2} {
>>> set pi [expr atan2(1,1) * 4]
>>>
>>> # assume the earth is a perfect sphere (miles)
>>> set r 3963.1
>>>
>>> # convert to radians
>>> set a1 [expr {$lat1*($pi/180)}]
>>> set b1 [expr {$long1*($pi/180)}]
>>> set a2 [expr {$lat2*($pi/180)}]
>>> set b2 [expr {$long2*($pi/180)}]
>>>
>>> # found @ http://jan.ucc.nau.edu/~cvm/latlon_formula.html
>>> #return [expr {acos(cos($a1)*cos($b1)*cos($a2)*cos($b2) + \
>>> cos($a1)*sin($b1)*cos($a2)*sin($b2) + sin($a1)*sin($a2)) * $r}]
>>>
>>> # found @ http://mathforum.org/library/drmath/view/51879.html
>>> set dlon [expr {$b2 - $b1}]
>>> set dlat [expr {$a2 - $a1}]
>>> set a [expr {pow((sin($dlat/2)),2) + \
>>> cos($a1)*cos($a2)*pow((sin($dlon/2)),2)}]
>>> set c [expr {2 * atan2(sqrt($a),sqrt(1-$a))}]
>>> return [expr {$r * $c}]
>>>}
>>>
>>>
>>>% geographical_distance 37.943591 -122.323258 37.111111 -120.157222
>>>with the first: 132.031435852
>>>with the second: 132.031435843
>>>
>>>
>>>Hmm.. My "reference" source says those points are 63.26 miles away from
>>>each other. Anyone have any thoughts on this? I think my reference one
>>>just might be wrong after all...
>>
>>
>>I don't know what your reference is.
>>The formula you have used looks like a spherical approximation to the
>>earth.
>
>
> yup.
>
>
>>But for higher precision you need an ellipsoidal approximation. But even
>>these
>>are not unique - (nearly) each country uses it own ellipsoidal
>>approximation.
>>Look for geodetic datum.
>>There is one exception: for world wide reference (e.g. UTM) the so-called
>>WGS84 datum is used. When using an ellipsoidal model of the earth, there
>>are
>>no closed form exact formulas. Using a very exact approximation and the
>>WGS84
>>system, I get 212582.7 m i.e. 132.0928 miles ( using 1 mile = 1609.344 m)
>>The quoted result of 63.26 miles is plain wrong, the differences caused
>>by using different ellipsoidal models is well below 0.5 miles.
>
>
>
> Helmut,
>
> Thanks for your input. Yes, looks like my "reference" is plain wrong.
> Excellent news. Four alternate checks with yours give me 132 mi, so
> that's good enough for me to call that reference broken. Thanks for the
> pointer to ellipsoid models, I'll check them out.
>
> "I test, therefore I am".
Wait a sec.. Something is terribly wrong here. I broke-out a printed
map and measured with some calipers to get an approximate distance from
+37° 56' 36.93", -122° 19' 23.73" to
+37° 45' 20.00", -122° 27' 5.00" (Sutro tower, SF, CA)
to be 15.09mi
My "reference" says those are 14.8mi apart. That's good, they verify.
Next I plugged those into my proc and got 44.26mi!! What the? Ok, what
did I do wrong? :)
Ahh.. My calculation from minutes/seconds into pure decimal degrees
went the wrong way.. Haha.. my bad. I found it be checking on
maps.google.com
[expr {-122+(27+(5/60.0))/60}] != -122.451389
changed to 'expr {-(abs(-122)+(27+(5/60.0))/60)}' to apply direction
after adding the decimal parts.
Silly me. A 6th grade math error on my part.
.
- Follow-Ups:
- Re: geography math?
- From: Torsten Reincke
- Re: geography math?
- References:
- geography math?
- From: David Gravereaux
- Re: geography math?
- From: Helmut Jarausch
- Re: geography math?
- From: David Gravereaux
- geography math?
- Prev by Date: OT: Combining CGI and SSI (Was: Re: Rivet and snit)
- Next by Date: binary scan issue
- Previous by thread: Re: geography math?
- Next by thread: Re: geography math?
- Index(es):
Relevant Pages
|