author Simon Chabot Tue, 30 Oct 2012 16:16:40 +0100 changeset 62 c05fe3386919 parent 61 72614ee36ea2 child 63 407a9c351e66
[Distance] The output unit of geographical distance can be precised
 distances.py file | annotate | diff | comparison | revisions
```--- a/distances.py	Mon Oct 29 15:19:09 2012 +0100
+++ b/distances.py	Tue Oct 30 16:16:40 2012 +0100
@@ -219,7 +219,8 @@
except TypeError:
return abs(float(a) - float(b))

+                 units = 'm'):
""" Return the geographical distance between two points.

Both points must be tuples (latitude, longitude)
@@ -228,7 +229,12 @@
otherwise
- planetRadius is the planet's radius in meters. By default, it's the
Earth'one.
+
+        - `units` can be 'm' (meters) or 'km' (kilometers)
"""
+    pointa = (float(pointa), float(pointa))
+    pointb = (float(pointb), float(pointb))
+
difflat = pointa - pointb
difflong = pointa - pointb
meanlat = (pointa + pointb)/2.0
@@ -238,4 +244,5 @@
difflong *= pi / 180.0
meanlat *= pi / 180.0

-    return planetRadius * sqrt(difflat**2 + (cos(meanlat) * difflong)**2)
+    coef = 1. if units == 'm' else 0.001
+    return coef * planetRadius * sqrt(difflat**2 + (cos(meanlat) * difflong)**2)```