Open In App

Law of Cosines Great Circle Distance in R

Improve
Improve
Like Article
Like
Save
Share
Report

It states that the Shortest distance between two points on the surface of a sphere is measured along the surface of the sphere(arc distance) instead of a line that was passing through the interior parts of the sphere. The formula was proposed based on considering the earth’s radius (6400 Km) as follows:

Formula:

dist=r*\cos^{-1}\left[\cos{l_1}\cos{l_2}\cos{\left(L_1-L_2\right)}\right)+\sin{l_1}\sin{l_2}]

Where,

  • r – the radius of the earth (6400 Km)
  • l1, l2 – are the degrees which represent the position of latitudes
  • L1, L2 – are the degrees that represent the position of longitudes

In R Programming the Great Circle Distance can be calculated using the distCosine() function which was present in the geosphere package.

Calculate Cosine Great Circle Distance in R

Install and load the required package.

# Install geosphere package
install.packages("geosphere")
# Load the installed package]
library(geosphere)

Calculate the Great Circle Distance using the distCosine() function.

Syntax:  distCosine(p1, p2, r)

Where,

  • p1 – The vector of numbers which represents the longitude and latitude respectively of first point
  • p2 – The vector of numbers which represents the longitude and latitude respectively of second point
  • r – the radius of earth [by default set to 6378137 meters]

In this method, we are going to pass the vectors which represent the points which consist of longitude and latitude respectively as follows

Example1:

Let us consider the two points as P1(75.852, 20.345) and P2(85.567, 25.798) then the Cosine Great Distance between them using R.

R

great_dist <- geosphere::distCosine(c(75.852, 20.345),
                                    c(85.567, 25.798))
print(great_dist)

                    

Output:

1164870

Example 2:

Let us consider the two points as P1(0, 80) and P2(80, 36) then the Cosine Great Distance between them using R can be calculated as follows.

R

great_dist <- geosphere::distCosine(c(0, 80),
                                    c(80, 36))
print(great_dist)

                    

Output:

5888463

The other way is to pass the matrix which represents the latitude and longitude of the points respectively as follows.

Example 1:

Let us consider the two points as P1(75.852, 20.345) and P2(85.567, 25.798) then the Cosine Great Distance between them using method 2.

R

# Creation of points(longitude, latitude) matrix
points <- matrix(c(75.852, 85.567,
                   20.345, 25.798),
                   nrow = 2)
# Calculation of Greatest Cosine Distance using R
great_dist <- geosphere::distCosine(points)
print(great_dist)

                    

Output:

1164870

Example 2:

Let us consider the two points as P1(0, 80) and P2(80, 36) and then the Cosine Great Distance between them using method 2.

R

# Creation of points(longitude, latitude) matrix
points <- matrix(c(0, 80,
                  80, 36),
                  nrow = 2)
# Calculation of Greatest Cosine Distance using R
great_dist <- geosphere::distCosine(points)
print(great_dist)

                    

Output:

5888463


Last Updated : 04 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads