Last active
August 30, 2024 12:44
-
-
Save hotdang-ca/6c1ee75c48e515aec5bc6db6e3265e49 to your computer and use it in GitHub Desktop.
Golang code to calculate distance between two lat/lng decimal coordinates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [email protected] ::: | |
//::: ::: | |
//::: 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")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.