Created
February 17, 2014 18:31
-
-
Save h2rd/9056252 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # unicode: utf-8 | |
| from __future__ import print_function | |
| from math import radians, sqrt, sin, cos, atan2 | |
| import sys | |
| def get_distance(point1, point2): | |
| lat1, lon1 = point1.strip().split(',') | |
| lat2, lon2 = point2.strip().split(',') | |
| lat1 = radians(float(lat1)) | |
| lon1 = radians(float(lon1)) | |
| lat2 = radians(float(lat2)) | |
| lon2 = radians(float(lon2)) | |
| dlon = lon1 - lon2 | |
| y = sqrt( | |
| (cos(lat2) * sin(dlon)) ** 2 | |
| + (cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dlon)) ** 2 | |
| ) | |
| x = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(dlon) | |
| c = atan2(y, x) | |
| return 6372.8 * c | |
| def main(): | |
| sourse = sys.stdin.read().strip() | |
| distances = [] | |
| for p in sourse.split("\n"): | |
| point1, point2 = p.split(';') | |
| distances.append({ | |
| "point1": point1, | |
| "point2": point2, | |
| "distance": get_distance(point1, point2), | |
| "unit": "km" | |
| }) | |
| return distances | |
| if __name__ == '__main__': | |
| import pprint | |
| pprint.pprint(main(), indent=2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 22.880831,-109.905608;24.21767,-110.3053 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment