Compute the Distance Between Points (Vector, GPS) using Python
This is a collection of different ways to find the distance between points.
You guys won’t believe it and it is funny also, I always cross the road diagonally. I wait for two signals to turn red.
This Pythagoras Theorem of calculating hypotenuse is kind of in my heart. Joke apart is not it true that diagonal is a way to find the shortest distance? It might not be the best way.
To calculate the distance AB between point A(x1,y1) and B(x2,y2), first, draw a right triangle that has the segment and calculate the hypotenuse.
Now in machine learning these are the most widely used distance metrics
Euclidean Distance
p1 = (1, 2, 3), p2 = (4, 5, 6)
euclidean = distance.euclidean(p1, p2)
Then Manhattan Distance, Minkowski Distance and Hamming Distance are used widely. I really like the explanation listed here(https://www.analyticsvidhya.com/blog/2020/02/4-types-of-distance-metrics-in-machine-learning/)
Calculus to find distance:
r(t)=⟨f(t),g(t),h(t)⟩ is a function of one variable
In the simpler case of a function y=s(t), in which tt represents time and s(t) is position on a line, we have seen that the derivative s′(t)represents velocity (https://www.whitman.edu/mathematics/calculus_online/section13.02.html)
Δr=r(t+Δt)−r(t) means — it is a vector that points from the head of r(t) to the head of r(t+Δt)
Tangent vector:
Compute Distance Between GPS Points using Python
1. Geodesic
Calculus of variations
The critical points of the first variation are precisely the geodesics. The second variation is defined by
2.Great-circle distance
Python: https://introcs.cs.princeton.edu/python/12types/greatcircle.py.html
This method computes the shortest distance path of two points on the surface of the sphere.
Python calculator: https://pypi.org/project/great-circle-calculator/
Vector method:
3. Haversine formula
Package: $ pip install haversine
from haversine import haversine_vector, Unit
lyon = (45.7597, 4.8422) # (lat, lon)
paris = (48.8567, 2.3508)
new_york = (40.7033962, -74.2351462)
haversine_vector([lyon, lyon], [paris, new_york], Unit.KILOMETERS)
>> array([ 392.21725956, 6163.43638211])
Reference: https://pypi.org/project/haversine/
Assumption: Earth is a perfect sphere which leads to errors of up to 0.5%
lambdaλ = longitude
R = 6371 (Earth’s mean radius in km)
R = 6373.0 radius of the Earth
lattitude1 = math.radians(52.22) #coordinates
longitude1 = math.radians(21.01)
lattitude2 = math.radians(52.40)
longitude2 = math.radians(16.92)
dlongitude = longitude2 — longitude1 change in coordinates
dlattitude = lattitude2 —lattitude1
a = math.sin(dlattitude / 2)**2 + math.cos(lattitude1) * math.cos(lattitude2) * math.sin(dlongitude/ 2)**2 Haversine formula
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 — a)) distance = R * c
print(distance): 278.54
4. Spherical law of cosines
Spherical triangles have been studied since antiquity because of their importance in navigation and astronomy, but the Spherical Law of Cosines is first found in the work of Regiomontanus (1464). (Ref: https://www.theoremoftheday.org/GeometryAndTrigonometry/SphericalCos/TotDSphericalCos.pdf)
5. Geodesics on an ellipsoid
https://en.wikipedia.org/wiki/Geodesics_on_an_ellipsoid
Distance Calculator for Maps:
Draft Logic:
https://www.daftlogic.com/projects-google-maps-distance-calculator.htm
I hope these links and references will be helpful to understand the concept as well as I tried to cover the resources to verify with existing calculators.
References: