calculate distance based on gps coordinates - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: calculate distance based on gps coordinates (/thread-5782.html) |
calculate distance based on gps coordinates - atux_null - Oct-21-2017 Hi i do have a homework that it needs to calculate the distance between two points on earth. the earth's radius is R = 6372.8Km. so i have to ask the user to input the two coordinates and based on haversine's formula to creae the code. here i have: from math import sin, cos, sqrt, atan2, radians # approximate radius of earth in km R = 6372.8 x1=int(input('Please state the of the longitude of point A: ')) y1=int(input('Please state the of the latitude of point A: ')) x2=int(input('Please state the of the longitude of point B: ')) y2=int(input('Please state the of the latitude of point B: ')) lat1 = radians(x1) lon1 = radians(y1) lat2 = radians(x2) lon2 = radians(y2) dlon = lon2 - lon1 dlat = lat2 - lat1 a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2 c = 2 * atan2(sqrt(a), sqrt(1 - a)) distance = R * c print("Result:", distance,"Km")if i give an integer number everything works fine. But longtitude and latitude are floats so if i give floats it comes with an error: Any ideas please?
RE: calculate distance based on gps coordinates - gruntfutuk - Oct-21-2017 Use float() instead of int() maybe?
|