Skip to content

Instantly share code, notes, and snippets.

@bkerley
Created October 11, 2008 15:14
Show Gist options
  • Save bkerley/16280 to your computer and use it in GitHub Desktop.
Save bkerley/16280 to your computer and use it in GitHub Desktop.
proc gc_distance {pos1 pos2} {
set pi 3.1415926535
set lat1 [deg2rad [lindex $pos1 0]]
set lon1 [deg2rad [lindex $pos1 1]]
set lat2 [deg2rad [lindex $pos2 0]]
set lon2 [deg2rad [lindex $pos2 1]]
set theta [expr $lon2 - $lon1]
set dist [expr acos((sin($lat1) * sin($lat2)) + (cos($lat1) * cos($lat2) * cos($theta)))]
if {$dist < 0} {
set dist [expr $dist + $pi]
}
expr $dist * 3958.9
}
proc get_coords {icao} {
set ical [string toupper $icao]
if [cache exists airport_cache $icao] {
return [cache get airport_cache $icao]
}
set addr "http://maps.google.com/maps/geo?output=csv&oe=utf-8&q=$icao&key=ABQIAAAAdsSAw1O-7omKe7LKXavpFRQh99ZTDzCKrh6sdHmT446MSWVSuhQasqJRpIgMOXXBU7v4pHuaaCd2rA"
set api_output [split [cwget $addr] ","];
if {[lindex $api_output 0] != 200} then {error "geo api failed"};
set coords [lrange $api_output 2 3]
cache put airport_cache $icao $coords
return $coords
}
proc airport_distance {icao1 icao2} {
format "$icao1-$icao2: %.2f miles" [gc_distance [get_coords $icao1] [get_coords $icao2]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment