source: subversion/applications/routing/pyroute/geometry.py @ 5740

Last change on this file since 5740 was 5740, checked in by ojw, 12 years ago

try using spherical law of cosines to do distances

File size: 1.5 KB
Line 
1#!/usr/bin/python
2#-----------------------------------------------------------------------------
3# Handles geometry on the earth's surface (e.g. bearing/distance)
4#
5# Usage:
6#   (library code)
7#-----------------------------------------------------------------------------
8# Copyright 2007, Oliver White
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22#-----------------------------------------------------------------------------
23from math import *
24
25def bearing(a,b):
26    dlat = radians(b[0] - a[0])
27    dlon = radians(b[1] - a[1])
28
29    dlon = dlon * cos(radians(a[0]))
30   
31    return(degrees(atan2(dlon, dlat)))
32
33def distance(a,b):
34  lat1 = radians(a[0])
35  lon1 = radians(a[1])
36  lat2 = radians(b[0])
37  lon2 = radians(b[1])
38  d = acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2) * cos(lon2-lon1)) * 6371;
39  return(d)
40
41if(__name__ == "__main__"):
42  a = (51.477,-0.4856)
43  b = (51.477,-0.4328)
44
45  print bearing(a,b)
46  print distance(a,b)
Note: See TracBrowser for help on using the repository browser.