Calculate the distance in kilometers between two coordinates with Go

Calculate the distance in kilometers between two coordinates with Go

The Go language (Golang) offers a perfect environment to implement this type of operation efficiently. In this article, I will walk you through the steps to calculate the distance in kilometers between two coordinates using the Haversine formula.

Calculating the distance between two geographic coordinates (latitude and longitude) is a common problem in the development of applications that require the calculation of distances between points on the Earth's surface. The Go language (Golang) offers a perfect environment to implement this type of operation efficiently. In this article, I will walk you through the steps to calculate the distance in kilometers between two coordinates using the Haversine formula.

The haversine formula is an equation that allows you to calculate the distance of the shortest path (the "orthodromic distance") between two points on a sphere, specifically on the Earth's surface, considering the curvature of the planet.

The formula is as follows:

a=sin2(Δφ2)+cos(φ1)cos(φ2)sin2(Δλ2)a = \sin^2\left(\frac{\Delta \varphi}{2}\right) + \cos(\varphi_1) \cdot \cos(\varphi_2) \cdot \sin^2\left(\frac{\Delta \lambda}{2}\right)

c=2atan2(a,1a)c = 2 \cdot \text{atan2}\left(\sqrt{a}, \sqrt{1-a}\right)

d=Rcd = R \cdot c

Here is an example of how to implement the distance calculation using the Haversine formula:


package main

import (
  "fmt"
  "math"
)

// Function to convert degrees to radians
func toRadians(degrees float64) float64 {
  return degrees * math.Pi / 180
}

// Function to calculate the distance between two coordinates
func haversine(lat1, lon1, lat2, lon2 float64) float64 {
  const R = 6371 // Radius of the Earth in km

  // Convert coordinates from degrees to radians
  lat1Rad := toRadians(lat1)
  lon1Rad := toRadians(lon1)
  lat2Rad := toRadians(lat2)
  lon2Rad := toRadians(lon2)

  // Differences between coordinates
  deltaLat := lat2Rad - lat1Rad
  deltaLon := lon2Rad - lon1Rad

  // Apply the Haversine formula
  a := math.Sin(deltaLat/2)*math.Sin(deltaLat/2) +
       math.Cos(lat1Rad)*math.Cos(lat2Rad)*
       math.Sin(deltaLon/2)*math.Sin(deltaLon/2)
  c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))

  // Calculate the distance
  distance := R * c
  return distance
}

func main() {
  // Example coordinates
  lat1 := 41.9028 // Rome
  lon1 := 12.4964
  lat2 := 48.8566 // Paris
  lon2 := 2.3522

  // Calculate the distance
  distance := haversine(lat1, lon1, lat2, lon2)

  // Print the result
  fmt.Printf("The distance between the coordinates is: %.2f km\n", distance)
}

Code explanation:

  1. toRadians: This function converts degrees to radians, which is essential for using trigonometric functions.
  2. haversine: This function takes the coordinates of two points (latitude and longitude) as input and returns the distance in kilometers. It is based on the Haversine formula.
  3. main: Here, the example points (Rome and Paris) are defined, and the distance between them is calculated and printed.

Some considerations:

  • Accuracy: The Haversine formula is accurate for most distances, but may not be perfect on very small scales or at very high latitudes, where the curvature of the Earth becomes more significant. In these cases, you can consider more complex formulas such as the Vincenty formula.

  • Performance: The Haversine calculation is computationally inexpensive and can be performed quickly even on large datasets of geographic coordinates.

Conclusion

Calculating the distance between two geographic coordinates is a common problem that can be easily solved in Go using the Haversine formula. In just a few steps, we have seen how to implement this formula and obtain precise distances in kilometers. Thanks to Go's simplicity and powerful mathematical libraries, this type of calculation is fast and efficient, making Go an excellent choice for applications that require geographic operations.