Open In App

Calculate Great Circle Distances Between Pairs of Points Using OSMnx Module

Last Updated : 18 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Great Circle Distance evaluates the shortest distance between two points considering Earth as a sphere. It is an arc linking two points on a sphere. Here, we will see how to calculate great circle distances between pairs of points using the OSMnx distance module.

Syntax of osmnx.distance.great_circle() Function

The vectorized function calculates the great-circle distance between two points’ coordinates or between arrays of points’ coordinates using the haversine formula.

osmnx.distance.great_circle(lat1, lon1, lat2, lon2, earth_radius=6371009)

Note: coordinates in decimal degrees.

Parameters:

  • lat1 (float or numpy.array of float) – first point’s latitude coordinate
  • lon1 (float or numpy.array of float) – first point’s longitude coordinate
  • lat2 (float or numpy.array of float) – second point’s latitude coordinate
  • lon2 (float or numpy.array of float) – second point’s longitude coordinate
  • earth_radius (float) – earth’s radius in units in which distance will be returned (default is meters)

Returns: dist – distance from each (lat1, lon1) to each (lat2, lon2) in units of earth_radius

Return Type: float or numpy.array of float

Calculate Great Circle Distances Between Pairs of Points Using OSMnx module

Below are the code example by which we can understand how to Calculate Great Circle Distances Between pairs of points Using OSMnx distance module in Python:

Calculate Great Circle Distance

Let’s calculate the great circle distance between Thiruvananthapuram and New Delhi. The code as follows: code uses the OSMnx library to calculate the great circle distance (in meters) between two geographical coordinates: New Delhi (28.6139° N, 77.2090° E) and Thiruvananthapuram (8.50606° N, 76.96153° E), considering the Earth as a sphere with a radius of 6371009 meters.

Python3
import osmnx as ox
# coordinates
tvm_lat, tvm_lon = 8.50606, 76.96153
nwdelhi_lat, nwdelhi_lon = 28.6139, 77.2090

# great circle distance
ox.distance.great_circle(
    nwdelhi_lat, nwdelhi_lon, tvm_lat, tvm_lon,
    earth_radius=6371009)

Output:

2236043.023561823

The great circle distance between Thiruvananthapuram and New Delhi is 2236043.02 meters or 2236.04km.

Pass Array of Coordinates as Parameter

Let’s calculate the Great Circle Distance between Thiruvananthapuram – Mumbai and New Delhi – Thiruvananthapuram. Let’s look at the Python code. Below code uses the OSMnx library and NumPy to calculate the great circle distances (in meters) for an array of coordinate pairs. It computes the distances between Thiruvananthapuram (8.50606° N, 76.96153° E) and New Delhi (28.6139° N, 77.2090° E).

Python3
import osmnx as ox
import numpy as np

# coordinates
tvm_lat, tvm_lon = 8.50606, 76.96153
mum_lat, mum_lon = 19.0760, 72.8777
dlh_lat, dlh_lon = 28.6139, 77.2090

# set array of coordinates
from_lat = np.array([tvm_lat, dlh_lat])
from_lon = np.array([tvm_lon, dlh_lon])
to_lat = np.array([mum_lat, tvm_lat])
to_lon = np.array([mum_lon, tvm_lon])

# great circle distance for array of params
ox.distance.great_circle(
    from_lat, from_lon, to_lat, to_lon,
    earth_radius=6371009)

Output:

array([1255079.11904625, 2236043.02356182])

From the output, it’s clear that the Great Circle Distance between Thiruvananthapuram and Mumbai is 1255079.119 meters or 1255.079 Km and for New Delhi and Thiruvananthapuram it is 2236043.023 meters or 2236.043 Km.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads