Distance calculation between two latitude and longitude points with python (using spherical trigonometry)

import math


def get_distance_m(lat1, lon1, lat2, lon2):
    """
Distance between two points(m)
Simple distance calculation using spherical trigonometry
Google Map API geometory.computeDistanceBetween logic
    https://www.suzu6.net/posts/167-php-spherical-trigonometry/
    """
    R = 6378137.0  #Equatorial radius
    lat1 = math.radians(lat1)
    lon1 = math.radians(lon1)
    lat2 = math.radians(lat2)
    lon2 = math.radians(lon2)
    diff_lon = lon1 - lon2
    dist = math.sin(lat1) * math.sin(lat2) + math.cos(lat1) * math.cos(lat2) * math.cos(diff_lon)
    return R * math.acos(min(max(dist, -1.0), 1.0))


if __name__ == "__main__":
    #Calculate the distance when moving 1 arcsecond in the latitude and longitude directions
    distance = get_distance_m(lon1=139.0,
                              lat1=35.0,
                              lon2=139.0 + 1.0 / 3600.0,
                              lat2=35.0 + 1.0 / 3600.0)
    print(distance)

Execution result

39.972442531744505

Recommended Posts

Distance calculation between two latitude and longitude points with python (using spherical trigonometry)
Visualization of latitude / longitude coordinate data (assuming meteorological data) using cartopy and matplotlib
Get the address from latitude and longitude
Distance calculation between two latitude and longitude points with python (using spherical trigonometry)
[Rust] Read the latitude / longitude csv data to find the distance between two points
Communicate between Elixir and Python with gRPC
Using Python and MeCab with Azure Databricks
I'm using tox and Python 3.3 with Travis-CI
Latitude / longitude coordinates ↔ UTM coordinate conversion with python
[Python] Find coordinates from two angles and distance
Get latitude / longitude distance in meters with QGIS
Get nearby latitude / longitude points with Python's geoindex library
Start numerical calculation in Python (with Homebrew and pip)
YOLP: Extract latitude and longitude with Yahoo! Geocoder API.
WRF / Python> Read two NetCDF files and display (latitude, longitude, target value)> enumerate (zip (* rows))
Age calculation using python
Numerical calculation with Python
IP spoof using tor on macOS and check with python
Using Python with SPSS Modeler extension nodes ① Setup and visualization
Serial communication control with python and I2C communication (using USBGPIO8 device)
Serial communication control with python and SPI communication (using USBGPIO8 device)
This and that for using Step Functions with CDK + Python
Performance comparison between 2D matrix calculation and for with numpy