Calculating Distance Between Latitude And Longitude In Python?
Solution 1:
Based on the question it sounds like you would like to calculate the distance between all pairs of points. Scipy has built in functionality to do this.
My suggestion is to first write a function that calcuates distance. Or use an exisitng one like the one in geopy mentioned in another answer.
def get_distance(point1, point2):
R = 6370
lat1 = radians(point1[0]) #insert value
lon1 = radians(point1[1])
lat2 = radians(point2[0])
lon2 = radians(point2[1])
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
return distance
Then you can pass this function into scipy.spatial.distance.cdist
all_points = df[[latitude_column, longitude_column]].values
dm = scipy.spatial.distance.cdist(all_points,
all_points,
get_distance)
As a bonus you can convert the distance matrix to a data frame if you wish to add the index to each point:
pd.DataFrame(dm, index=df.index, columns=df.index)
NOTE: I realized I am assuming, possibly incorrectly that you are using pandas
Solution 2:
If you don't mind importing a few libraries, this can be done very simply.
With pandas, you can read your text file into a dataframe, which makes working with tabular data like this super easy.
import pandas as pd
df = pd.read_csv('YOURFILENAME.txt', delimiter=' ', header=None, names=('latitude', 'longitude'))
Then you can use the geopy library to calculate the distance.
Solution 3:
Another solution is using the haversine equation with numpy
to read in the data and calculate the distances. Any of the previous answers will also work using the other libraries.
import numpy as np
#read in the file, check the data structure using data.shape()
data = np.genfromtxt(fname)
#Create the function of the haversine equation with numpy
def haversine(Olat,Olon, Dlat,Dlon):
radius = 6371. # km
d_lat = np.radians(Dlat - Olat)
d_lon = np.radians(Dlon - Olon)
a = (np.sin(d_lat / 2.) * np.sin(d_lat / 2.) +
np.cos(np.radians(Olat)) * np.cos(np.radians(Dlat)) *
np.sin(d_lon / 2.) * np.sin(d_lon / 2.))
c = 2. * np.arctan2(np.sqrt(a), np.sqrt(1. - a))
d = radius * c
return d
#Call function with your data from some specified point
haversine(10,-110,data[:,1],data[:,0])
In this case, you can pass a single number for Olat,Olon
and an array for Dlat,Dlon
or vice versa and it will return an array of distances.
For example:
haversine(20,-110,np.arange(0,50,5),-120)
OUTPUT
array([2476.17141062, 1988.11393057, 1544.75756103, 1196.89168113,
1044.73497113, 1167.50120561, 1499.09922502, 1934.97816445,
2419.40097936, 2928.35437829])
Post a Comment for "Calculating Distance Between Latitude And Longitude In Python?"