I need help calculating the distance between two points-- in this case, the two points are longitude and latitude. I have a .txt file that contains longitude and latitude in columns like this:
-116.148000 32.585000
-116.154000 32.587000
-116.159000 32.584000
The columns do not have headers. I have many more latitudes and longitudes.
So far, i have come up with this code:
from math import sin, cos, sqrt, atan2, radians
R = 6370
lat1 = radians() #insert value
lon1 = radians()
lat2 = radians()
lon2 = radians()
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 (distance)
Many of the answers/code i've seen on stack overflow for calculating the distance between longitude and latitude have had longitude and latitude assigned as specific values.
I would like the longitude and latitude to equal the values in the columns they are in and for the equation to go through all of the longitudes and latitudes and calculate the distance.
I have not been able to come up with something to do this. Any help would be appreciated