Python calculates the distance between two points by latitude and longitude

Python calculates the distance between two points by latitude and longitude

There are generally two methods for calculating the distance between two points by latitude and longitude, geodesic distance (assuming the earth is an ellipsoid) and great circle distance (assuming the earth is a sphere). The latter is slightly lower in accuracy than the former, but faster, and can be selected according to needs.

1. Geodesic distance

geodesic distance The distance of the shortest path between two points on the Earth’s surface. There are many types of ellipsoid models, and which one is the most accurate depends on where your points are on the globe. The default is the WGS-84 ellipsoid, which is the most accurate globally.

from geopy.distance import geodesic as gd

# Longitude and latitude of two points
p1 = (9.072264, 7.491302)
p2 = (14.716677, -17.467686)

d = gd(p1, p2).m # get the result in meters
d = gd(p1, p2).km # get the result in kilometers

2. Great circle distance

The great circle distance uses the circular model of the earth, first converts the longitude and latitude into radians, and then treats the earth as a 6371 circle to calculate the spherical distance.

1. geopy package

from geopy.distance import great_circle as grc

# Longitude and latitude of two points
p1 = (9.072264, 7.491302)
p2 = (14.716677, -17.467686)

d = grc(p1, p2).m # get the result in meters
d = grc(p1, p2).km # Get the result in kilometers

2. Manual calculation

Theoretical formula

The Haversine formula is known (the derivation process is omitted):

h

a

v

e

r

the s

i

no

(

θ

)

=

sin

?

2

(

θ

/

2

)

=

(

1

?

cos

?

(

θ

)

)

/

2

haversin(\theta) =\sin^2(\theta/2)=(1-\cos(\theta))/2

haversin(θ)=sin2(θ/2)=(1? cos(θ))/2
make

θ

=

d

/

R

\theta=d/R

θ=d/R, where d is the distance between two places, and R is the radius of the earth.
have to:

h

a

v

e

r

the s

i

no

(

θ

)

=

h

a

v

the s

i

no

(

?

1

?

?

2

)

+

cos

?

(

?

1

)

cos

?

(

?

2

)

h

a

v

e

r

the s

i

no

(

lambda

2

?

lambda

1

)

haversin(\theta) =havsin(\phi_1-\phi_2) + \cos(\phi_1)\cos(\phi_2)haversin(\lambda_2-\lambda_1)

haversin(θ)=havsin(?1?2?) + cos(?1?)cos(?2?)haversin(λ2λ1?)

?

1

\phi_1

?1? and

?

2

\phi_2

?2? are the latitudes of two points respectively.

lambda

1

\lambda_1

λ1? and

lambda

2

\lambda_2

λ2? are the longitudes of the two points respectively.

python calculation process

from math import radians, cos, sin, asin, sqrt
p1 = (9.072264, 7.491302)
p2 = (14.716677, -17.467686)

lon1 = radians(p1[0])
lat1 = radians(p1[1])
lon2 = radians(p2[0])
lat2 = radians(p2[1])

theta = 2 * asin(sqrt(sin((lat2 - lat1)/2)**2 + cos(lat1) * cos(lat2) * sin((lon2 - lon1)/2)**2))

r_earth = 6371 # Earth radius (km)

d = theta * r_earth # get the result in kilometers
d = theta * r_earth * 1000 # get the result in meters

Reference

  1. geopy – Official Documentation
  2. Haversine Formula – Alchetron, The Free Social Encyclopedia