geo_util.py 721 B

12345678910111213141516171819202122232425262728
  1. import math
  2. def ggcd(lon1, lat1, lon2, lat2):
  3. """
  4. Returns the great-circle distance between two points, in metres
  5. @param lon1: longitude #1
  6. @param lat1: latitude #1
  7. @param lon2: longitude #2
  8. @param lat2: latitude #2
  9. @return: the distance between the two points, in metres
  10. """
  11. if lat1 == lat2 and lon1 == lon2:
  12. return 0
  13. RADIUS = 6371000.0
  14. rLon1 = math.radians(lon1)
  15. rLat1 = math.radians(lat1)
  16. rLon2 = math.radians(lon2)
  17. rLat2 = math.radians(lat2)
  18. cosdist = math.cos(rLat1) * math.cos(rLat2) * math.cos(rLon2 - rLon1) + math.sin(rLat1) * math.sin(rLat2)
  19. if cosdist >= 1.0:
  20. return 0.0
  21. else:
  22. return math.acos(cosdist) * RADIUS