source: subversion/applications/utils/amr2wav/geo.py @ 17248

Last change on this file since 17248 was 14494, checked in by gjones, 11 years ago

initial import

File size: 1.9 KB
Line 
1#!/usr/bin/python
2#---------------------------------------------------------------------------
3# Geographic calculations
4#---------------------------------------------------------------------------
5# Copyright 2007-2008, Oliver White
6#                2009, Graham Jones (minor corrections)
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#---------------------------------------------------------------------------
21from math import *
22
23def distance(lat1,lon1,lat2,lon2):
24  """Distance between two points in km"""
25  R = 6371.0
26  dLat = radians(lat2-lat1)
27  dLon = radians(lon2-lon1)
28  a = sin(dLat/2.0) * sin(dLat/2.0) + \
29          cos(radians(lat1)) * cos(radians(lat2)) * \
30          sin(dLon/2.0) * sin(dLon/2.0)
31  # GJ Added error correction for rounding errors.
32  if a>1: a=1
33  c = 2 * atan2(sqrt(a), sqrt(1.0-a))
34  return(R * c)
35
36def bearing(lat1,lon1,lat2,lon2):
37  """Bearing from one point to another in degrees (0-360)"""
38  dLat = lat2-lat1
39  dLon = lon2-lon1
40  y = sin(radians(dLon)) * cos(radians(lat2))
41  x = cos(radians(lat1)) * sin(radians(lat2)) - \
42          sin(radians(lat1)) * cos(radians(lat2)) * cos(radians(dLon))
43  bearing = degrees(atan2(y, x))
44  if(bearing < 0.0):
45    bearing += 360.0
46  return(bearing)
47
48
49if(__name__ == "__main__"):
50  print distance(51,-1,52,1)
51  print bearing(51,-1,52,1)
Note: See TracBrowser for help on using the repository browser.