Skip to content

Instantly share code, notes, and snippets.

@hotdang-ca
Last active March 14, 2024 06:58
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save hotdang-ca/6c1ee75c48e515aec5bc6db6e3265e49 to your computer and use it in GitHub Desktop.
Save hotdang-ca/6c1ee75c48e515aec5bc6db6e3265e49 to your computer and use it in GitHub Desktop.
Golang code to calculate distance between two lat/lng decimal coordinates.
package main
import (
"math"
"fmt"
)
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::: :::
//::: This routine calculates the distance between two points (given the :::
//::: latitude/longitude of those points). It is based on free code used to :::
//::: calculate the distance between two locations using GeoDataSource(TM) :::
//::: products. :::
//::: :::
//::: Definitions: :::
//::: South latitudes are negative, east longitudes are positive :::
//::: :::
//::: Passed to function: :::
//::: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :::
//::: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :::
//::: optional: unit = the unit you desire for results :::
//::: where: 'M' is statute miles (default, or omitted) :::
//::: 'K' is kilometers :::
//::: 'N' is nautical miles :::
//::: :::
//::: Worldwide cities and other features databases with latitude longitude :::
//::: are available at https://www.geodatasource.com :::
//::: :::
//::: For enquiries, please contact sales@geodatasource.com :::
//::: :::
//::: Official Web site: https://www.geodatasource.com :::
//::: :::
//::: Golang code James Robert Perih (c) All Rights Reserved 2018 :::
//::: :::
//::: GeoDataSource.com (C) All Rights Reserved 2017 :::
//::: :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
func distance(lat1 float64, lng1 float64, lat2 float64, lng2 float64, unit ...string) float64 {
radlat1 := float64(math.Pi * lat1 / 180)
radlat2 := float64(math.Pi * lat2 / 180)
theta := float64(lng1 - lng2)
radtheta := float64(math.Pi * theta / 180)
dist := math.Sin(radlat1) * math.Sin(radlat2) + math.Cos(radlat1) * math.Cos(radlat2) * math.Cos(radtheta);
if dist > 1 {
dist = 1
}
dist = math.Acos(dist)
dist = dist * 180 / math.Pi
dist = dist * 60 * 1.1515
if len(unit) > 0 {
if unit[0] == "K" {
dist = dist * 1.609344
} else if unit[0] == "N" {
dist = dist * 0.8684
}
}
return dist
}
// usage:
func main() {
type coordinate struct {
lat float64
lng float64
}
winnipeg := coordinate{ 49.895077, -97.138451 }
regina := coordinate{ 50.445210, -104.618896 }
fmt.Println(distance(winnipeg.lat, winnipeg.lng, regina.lat, regina.lng, "N"))
}
@semyon-dev
Copy link

thank you a lot)

@hotdang-ca
Copy link
Author

thank you a lot)

it's not the safest. But it works!

@dele454
Copy link

dele454 commented Jun 13, 2022

how does this compare to using ST_Distance()?

@hotdang-ca
Copy link
Author

hotdang-ca commented Aug 16, 2022

how does this compare to using ST_Distance()?

ST_Distance likely uses code that accomplishes the same.
If you require the other GIS functions ST-2 provides, you can use that library (creating, modelling, manipulating spheres).

If all you need is the distance between two points, save on code size, and use just this.

This code doesn't provide any guarantees, and is strictly a golang port of existing code snippets that geodatasource provides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment