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

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

Move the map tile code into its own module

Add headers and usage instructions to a bunch of modules

File size: 1.7 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#-----------------------------------------------------------------------------
23import math
24
25def bearing(a,b):
26    dlat = math.radians(b[0] - a[0])
27    dlon = math.radians(b[1] - a[1])
28
29    dlon = dlon * math.cos(math.radians(a[0]))
30   
31    return(math.degrees(math.atan2(dlon, dlat)))
32
33def distance(a,b):
34    dlat = math.radians(a[0] - b[0])
35    dlon = math.radians(a[1] - b[1])
36
37    dlon = dlon * math.cos(math.radians(a[0]))
38    #print "d = %f, %f" % (dlat, dlon)
39    # todo: mercator proj
40    dRad = math.sqrt(dlat * dlat + dlon * dlon)
41
42    c = 40000 # earth circumference,km
43   
44    return(dRad * c)
45   
46
47if(__name__ == "__main__"):
48    a = (51.477,-0.4856)
49    b = (51.477,-0.4328)
50
51    print bearing(a,b)
Note: See TracBrowser for help on using the repository browser.