Find the distance from latitude and longitude (considering the roundness of the earth).

When calculating the distance from latitude and longitude, it is not difficult if the roundness of the earth is taken into consideration and the earth can be approximated to a true sphere.

from math import sin, cos, acos, radians
earth_rad = 6378.137

def latlng_to_xyz(lat, lng):
    rlat, rlng = radians(lat), radians(lng)
    coslat = cos(rlat)
    return coslat*cos(rlng), coslat*sin(rlng), sin(rlat)

def dist_on_sphere(pos0, pos1, radius=earth_rad):
    xyz0, xyz1 = latlng_to_xyz(*pos0), latlng_to_xyz(*pos1)
    return acos(sum(x * y for x, y in zip(xyz0, xyz1)))*radius

Osaka = 34.702113, 135.494807
Tokyo = 35.681541, 139.767103
London = 51.476853, 0.0

print(dist_on_sphere(Osaka, Tokyo)) # 403.63km
print(dist_on_sphere(London, Tokyo)) # 9571.22km

If you want to calculate more accurately, you need to approximate it with a spheroid, and there is room for improvement such as the error or large in the vicinity of 1 for the ʻacos` function, but for casual purposes, this is enough.

Anyway, it's not that complicated even if you consider the roundness. For reference when APIs and libraries cannot be used.

Recommended Posts

Find the distance from latitude and longitude (considering the roundness of the earth).
Find the waypoint from latitude and longitude (considering the roundness of the earth).
[Python] Find coordinates from two angles and distance
Find the distance from latitude and longitude (considering the roundness of the earth).
Get the address from latitude and longitude
How to get the pixel value of the point from the satellite image by specifying the latitude and longitude
Find the inertial spindle and moment of inertia from the inertial tensor with NumPy
[GPS] Use pyproj to calculate distance, azimuth, and elevation from GPS latitude and longitude
[Python] Find coordinates from two angles and distance
Download Geographical Survey tiles from latitude and longitude
[Rust] Read the latitude / longitude csv data to find the distance between two points
Give latitude and longitude point sequence data and try to identify the road from OpenStreetMap data
DJango Note: From the beginning (simplification and splitting of URLConf)
Find the critical path of PERT using breadth-first search and depth-first search
Find out the age and number of winnings of prefectural governors nationwide
Capture GeneratorExit and detect the end of iteration from the generator side
Find out the mystery change of Pokédex description by Levenshtein distance
Find the intersection of a circle and a straight line (sympy matrix)
Find the definition of the value of errno
The story of Python and the story of NaN
Existence from the viewpoint of Python
Find the Levenshtein Distance with python
Approximation of distance between two points on the surface of a spheroid (on the surface of the earth)
Visualization of latitude / longitude coordinate data (assuming meteorological data) using cartopy and matplotlib
Find the general terms of the Tribonacci sequence with linear algebra and Python
Find out the name of the method that called it from the method that is python
Internal / external judgment with Python: Obtain the city / town / village name from the latitude / longitude information of any point