Law of Cosines Great Circle Distance in R
Last Updated :
04 Jan, 2023
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:
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
points <- matrix ( c (75.852, 85.567,
20.345, 25.798),
nrow = 2)
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
points <- matrix ( c (0, 80,
80, 36),
nrow = 2)
great_dist <- geosphere:: distCosine (points)
print (great_dist)
|
Output:
5888463
Share your thoughts in the comments
Please Login to comment...