Distance between two points based on latitude/longitude in Python

In this Tutorial, we here to find the Distance between two points based on latitude and longitude in Python.

Distance between two coordinates of different points

Before starting the tutorial I would like to give you some small details regarding the latitude and longitude.

  • Latitude:- It is a measurement of the vertical position between the North Pole and the South Pole to find the distance between two location coordinates.
  • Longitude:- Longitude is a measurement of the horizontal position.

To find the distance between the two different point latitudes and longitude we import the built-in Python Math library. Here we import some math library functions like sin, cos, and radians to make distance formulas for measuring the distance.

from math import radians, sin, cos, acos

In this Enter latitude and longitude of two points in radians for computing in the distance formula. First, create a variable that can hold the value entered by the user. The value is input in the float declaration instruction. (user input)

For, input of Point_1 Latitude and Longitude :-

s_lat1 = radians(float(input("Point1 Latitude : ")))            
e_lon1 = radians(float(input("Point1 Longitute: ")))

 

For, input of Point_2 Latitude and Longitude :-

s_lat2 = radians(float(input("Point2 Latitude : ")))            
e_lon2 = radians(float(input("Point2 Longitude: ")))

 

Then, we use the distance formula for computing the point’s latitudes and longitudes for finding the distance between them. This will follow the BODMAS Rule to compute the different input points for the output. We, take the input point coordinates in radians and the distance output in Kilometer(Km).

dist = 6371.01 * acos(sin(s_lat1)*sin(s_lat2) + cos(s_lat1)*cos(s_lat2)*cos(e_lon2 - e_lon1)
print("The distance is %.3f km." % dist)

Here, Python(3.7) codes to find the distance between two points by entering the different latitudes and longitude.

  • Python Code:-
                 # Distance Between the two point of Longitude/Latitude in Python

from math import radians, sin, cos, acos
print("Input the coordinates of two points of Longitute and Latitude:")
                    # Input The Coordinate in float/decimal form for distance accuracy
                                    # Point 1 Coordinates

s_lat1 = radians(float(input("Point1 Latitude : ")))            # s_lat1 = "Starting Latitude"
e_lon1 = radians(float(input("Point1 Longitute: ")))            # e_lon1 = "Ending Longitute"
                                    
                                    # Point 2 Coordinates
s_lat2 = radians(float(input("Point2 Latitude : ")))            # s_lat2 = "Starting Latitude"
e_lon2 = radians(float(input("Point2 Longitude: ")))            # e_lon2 = "Ending Longitude"

dist = 6371.01 * acos(sin(s_lat1)*sin(s_lat2) + cos(s_lat1)*cos(s_lat2)*cos(e_lon2 - e_lon1))
print("The distance is %.3f km." % dist)

For References

Example, for further reference purposes and output:-

Input the coordinates of two points of Longitute and Latitude:
Point1 Latitude : 24.5
Point1 Longitute: 29.6
Point2 Latitude : 45.6
Point2 Longitude: 48.5
The distance is 2893.487 km.

So for further reference use the below link to understand more details of the math module in Python:-

Leave a Reply

Your email address will not be published. Required fields are marked *