# Geospatial Distance Between Two Points in R

Geospatial distance, also known as the geographical distance between any two points in space is referred as the distance measured along the surface of the earth. This distance is measured in terms of the position of the points with respect to the latitude and longitudinal position. There are different aspects and formulae to compute this distance.

In R Programming Language, an external package “**geosphere**” is available in order to compute the distances and the respective measures for angular (longitude/latitude) locations. This package implements the methods that compute various aspects of distance, direction, area, etc. for geographic coordinate positions. The package can be installed into the working space using the following command :

install.packages(“geosphere”)

Different types of distances are available between the points taking into consideration the shape of the earth, the assumed radius of the earth, etc. The points specified for each of the distance computation methods may be a vector of two numbers containing the respective x and y coordinates, a matrix of 2 columns, first one is longitude, followed by latitude. In case the two points are equal, the distance is considered to be 0 for all practical purposes.

**Haversine Distance**– This shortest distance is based on the assumption of the earth being spherical, ignoring ellipsoidal effects.

Syntax:distHaversine(pt1, pt2, r=6378137)

Parameter :

- pt1 and pt2 – longitude/latitude of point(s).
- r – radius of the earth; default = 6378137 m

**Example:**

## R

`# installing required library ` `library ` `(` `"geosphere"` `) ` ` ` `# declaring two points ` `point1 <- ` `c` `(82.13452, 23.430502) ` `point2 <- ` `c` `(43.23245,51.12356) ` ` ` `point_mat <- ` `matrix` `(` `c` `(point1, point2), ncol =2 ) ` ` ` `print ` `(` `"Original Matrix"` `) ` `print ` `(point_mat) ` ` ` `# haversine distance ` `print ` `(` `"Haversine Distance"` `) ` `distHaversine` `(point_mat) ` |

**Output**

[1] “Haversine Distance”

[1] 4405533

**Geo distance –**Highly accurate estimate of the shortest distance between two points on an ellipsoidal surface of the earth. The distm() method of this package is used for the computation of the distance matrix of a set (a pair) of points.

Syntax:distm(xpos, ypos, fun=distGeo)

Parameter :

- xpos – longitude/latitude of point(s).
- ypos – Default : same as x , in case of missing
- fun – Distance computation function (e.g., distCosine or distGeo)

**Example:**

## R

`# installing required library ` `library ` `(` `"geosphere"` `) ` ` ` `# declaring two points ` `point1 <- ` `c` `(82.13452, 23.430502) ` `point2 <- ` `c` `(43.23245,51.12356) ` ` ` `point_mat <- ` `matrix` `(` `c` `(point1, point2), ncol =2 ) ` ` ` `print ` `(` `"Original Matrix"` `) ` `print ` `(point_mat) ` ` ` `# applying distm method ` `geospatial_dist <- ` `distm` `(point_mat, fun = distGeo) ` `print ` `(` `"Distance Matrix"` `) ` `print ` `(geospatial_dist) ` |

**Output**

[1] “Original Matrix”

[,1] [,2]

[1,] 82.13 43.23

[2,] 23.43 51.12

[1] “Distance Matrix”

[,1] [,2]

[1,] 0 4412901

[2,] 4412901 0

**Cosine Distance**– This shortest distance is based on the assumption of the earth being spherical, ignoring ellipsoidal effects.

Syntax:distCosine(pt1, pt2, r=6378137)

Parameter :

- pt1 and pt2 – longitude/latitude of point(s).
- r – radius of the earth; default = 6378137 m

**Meeus Distance**is the shortest distance between two points on an ellipsoid (the â€™geodeticâ€™).

**Example:**

## R

`# installing required library ` `library ` `(` `"geosphere"` `) ` ` ` `# declaring two points ` `point1 <- ` `c` `(82.13452, 23.430502) ` `point2 <- ` `c` `(43.23245,51.12356) ` ` ` `point_mat <- ` `matrix` `(` `c` `(point1, point2), ncol =2 ) ` ` ` `print ` `(` `"Original Matrix"` `) ` `print ` `(point_mat) ` ` ` `# cosine distance ` `print ` `(` `"Cosine Distance"` `) ` `distCosine` `(point_mat) ` ` ` `# Meeus distance ` `print ` `(` `"Meeus Distance"` `) ` `distMeeus` `(point_mat) ` |

**Output**

[1] “Original Matrix”

[,1] [,2]

[1,] 82.13 43.23

[2,] 23.43 51.12

[1] “Cosine Distance”

[1] 4405533

[1] “Meeus Distance”

[1] 4412894