[
  {
    "path": ".gitignore",
    "content": "# Binaries for programs and plugins\n*.exe\n*.exe~\n*.dll\n*.so\n*.dylib\n\n# Test binary, built with `go test -c`\n*.test\n\n# Output of the go coverage tool, specifically when used with LiteIDE\n*.out\n\n# Dependency directories (remove the comment below to include it)\n# vendor/\n.idea/\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2021 John Taylor\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# geodist\nGoLang package to compute the distance between two geographic latitude, longitude coordinates\n\n## Algorithm Comparison\n* `Vincenty` is more accurate than `Haversine` because is considers the Earth's [ellipticity](https://www.dictionary.com/browse/ellipticity) when performing the calculation, but takes a longer time to compute.\n* [Wikipedia: Haversine](https://en.wikipedia.org/wiki/Haversine_formula)\n* [Wikipedia: Vincenty](https://en.wikipedia.org/wiki/Vincenty%27s_formulae)\n* [Is the Haversine Formula or the Vincenty's Formula better for calculating distance?](https://stackoverflow.com/q/38248046/452281)\n\n## Example\n* [example.go](example/example.go)\n\n```go\n\tvar newYork = geodist.Coord{Lat: 40.7128, Lon: 74.0060}\n\tvar sanDiego = geodist.Coord{Lat: 32.7157, Lon: 117.1611}\n\tmiles, km, ok := geodist.VincentyDistance(newYork, sanDiego)\n\tif !ok {\n\t\tfmt.Println(\"Unable to compute Vincenty Distance.\")\n\t\treturn\n\t}\n\tfmt.Printf(\" [Vincenty] New York to San Diego: %.3f m, %.3f km\\n\", miles, km)\n\n\tvar elPaso = geodist.Coord{Lat: 31.7619, Lon: 106.4850}\n\tvar stLouis = geodist.Coord{Lat: 38.6270, Lon: 90.1994}\n\tmiles, km = geodist.HaversineDistance(elPaso, stLouis)\n\tfmt.Printf(\"[Haversine] El Paso to St. Louis:  %.3f m, %.3f km\\n\", miles, km)\n```\n\n## Online Calculators\n* **Great Circle**: [NOAA: Latitude/Longitude Distance Calculator](https://www.nhc.noaa.gov/gccalc.shtml)\n* **Haversine**: [Moveable Type: Calculate distance, bearing and more between Latitude/Longitude points](https://www.movable-type.co.uk/scripts/latlong.html)\n* **Vincenty**: [CQSRG: WGS-84 World Geodetic System Distance Calculator](https://www.cqsrg.org/tools/GCDistance/)\n\n## Acknowledgements\n* [Haversine Algorithm](https://gist.github.com/cdipaolo/d3f8db3848278b49db68) in `GoLang`\n* [Vincenty Algorithm](https://web.archive.org/web/20181109001358/http://www.5thandpenn.com/GeoMaps/GMapsExamples/distanceComplete2.html) in `JavaScript`\n"
  },
  {
    "path": "example/example.go",
    "content": "package main\n\nimport (\n\t\"fmt\"\n\t\"github.com/jftuga/geodist\"\n)\n\nfunc main() {\n\tvar newYork = geodist.Coord{Lat: 40.7128, Lon: 74.0060}\n\tvar sanDiego = geodist.Coord{Lat: 32.7157, Lon: 117.1611}\n\tmiles, km, err := geodist.VincentyDistance(newYork, sanDiego)\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\treturn\n\t}\n\tfmt.Printf(\" [Vincenty] New York to San Diego: %.3f m, %.3f km\\n\", miles, km)\n\tmiles, km = geodist.HaversineDistance(newYork, sanDiego)\n\tfmt.Printf(\"[Haversine] New York to San Diego: %.3f m, %.3f km\\n\", miles, km)\n\n\tfmt.Println()\n\n\tvar elPaso = geodist.Coord{Lat: 31.7619, Lon: 106.4850}\n\tvar stLouis = geodist.Coord{Lat: 38.6270, Lon: 90.1994}\n\tmiles, km, err = geodist.VincentyDistance(elPaso, stLouis)\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\treturn\n\t}\n\tfmt.Printf(\" [Vincenty] El Paso to St. Louis: %.3f m, %.3f km\\n\", miles, km)\n\tmiles, km = geodist.HaversineDistance(elPaso, stLouis)\n\tfmt.Printf(\"[Haversine] El Paso to St. Louis: %.3f m, %.3f km\\n\", miles, km)\n}\n"
  },
  {
    "path": "go.mod",
    "content": "module github.com/jftuga/geodist\n\ngo 1.17\n"
  },
  {
    "path": "haversine.go",
    "content": "/*\n\nhaversine.go\n-John Taylor\n\nCompute the distance between two geographic points when given a pair of latitude-longitude coordinates\n\nHaversine formula:\nhttps://en.wikipedia.org/wiki/Haversine_formula\n\nThe code below was adapted from Conner DiPaolo:\nhttps://gist.github.com/cdipaolo/d3f8db3848278b49db68\n\n*/\n\npackage geodist\n\nimport (\n\t\"math\"\n)\n\n// adapted from: https://gist.github.com/cdipaolo/d3f8db3848278b49db68\n// haversin(θ) function\nfunc hsin(theta float64) float64 {\n\treturn math.Pow(math.Sin(theta/2), 2)\n}\n\n// HaversineDistance returns the distance (in miles) between two points of\n//\t a given longitude and latitude relatively accurately (using a spherical\n//\t approximation of the Earth) through the Haversin Distance Formula for\n//\t great arc distance on a sphere with accuracy for small distances\n//\n// point coordinates are supplied in degrees and converted into rad. in the func\n//\n// http://en.wikipedia.org/wiki/Haversine_formula\nfunc HaversineDistance(p1, p2 Coord) (float64, float64) {\n\t// convert to radians\n\t// must cast radius as float to multiply later\n\tvar la1, lo1, la2, lo2, r float64\n\n\tpiRad := math.Pi / 180\n\tla1 = p1.Lat * piRad\n\tlo1 = p1.Lon * piRad\n\tla2 = p2.Lat * piRad\n\tlo2 = p2.Lon * piRad\n\n\tr = 6378100 // Earth radius in METERS\n\n\t// calculate\n\th := hsin(la2-la1) + math.Cos(la1)*math.Cos(la2)*hsin(lo2-lo1)\n\n\tmeters := 2 * r * math.Asin(math.Sqrt(h))\n\tkilometers := meters / 1000\n\tmiles := kilometers * 0.621371\n\treturn miles, kilometers\n}\n"
  },
  {
    "path": "haversine_test.go",
    "content": "package geodist\n\nimport (\n\t\"testing\"\n)\n\nfunc TestHaversineDistance(t *testing.T) {\n\t// distance NY-SD: 3915340.577 m\n\t// distance SD-EP: 1011300.217 m\n\t// distance EP-SL: 1663833.491 m\n\t// distance SL-NY: 1406519.972 m\n\tvar newYork = Coord{40.7128, 74.0060}\n\tvar sanDiego = Coord{32.7157, 117.1611}\n\tvar elPaso = Coord{31.7619, 106.4850}\n\tvar stLouis = Coord{38.6270, 90.1994}\n\n\tvar miles, km float64\n\n\tmiles, km = HaversineDistance(newYork, sanDiego)\n\tif int(miles) != 2430 || int64(km) != 3911 {\n\t\tt.Errorf(\"Computed values: %v %10f\\n\", miles, km)\n\t\tt.Errorf(\"Incorrect computation between New York and San Diego: %v %v\\n\", int(miles), int64(km))\n\t}\n\n\tmiles, km = HaversineDistance(sanDiego, elPaso)\n\tif int(miles) != 627 || int64(km) != 1010 {\n\t\tt.Errorf(\"Computed values: %v %10f\\n\", miles, km)\n\t\tt.Errorf(\"Incorrect computation between San Diego and El Paso: %v %v\\n\", int(miles), int64(km))\n\t}\n\n\tmiles, km = HaversineDistance(elPaso, stLouis)\n\tif int(miles) != 1033 || int(km) != 1663 {\n\t\tt.Errorf(\"Computed values: %v %10f\\n\", miles, km)\n\t\tt.Errorf(\"Incorrect computation between El Paso and St. Louis: %v %v\\n\", int(miles), int64(km))\n\t}\n\n\tmiles, km = HaversineDistance(stLouis, newYork)\n\tif int(miles) != 872 || int(km) != 1404 {\n\t\tt.Errorf(\"Computed values: %v %10f\\n\", miles, km)\n\t\tt.Errorf(\"Incorrect computation between St. Louis and New York: %v %v\\n\", int(miles), int64(km))\n\t}\n\n\tmiles, km = HaversineDistance(newYork, newYork)\n\tif int(miles) != 0 || int(km) != 0 {\n\t\tt.Errorf(\"Computed values: %v %10f\\n\", miles, km)\n\t\tt.Errorf(\"Incorrect computation between New York and New York: %v %v\\n\", int(miles), int64(km))\n\t}\n}\n"
  },
  {
    "path": "shared.go",
    "content": "/*\nshared.go\n-John Taylor\n\nshared components between the distance algorithms\n\n*/\n\npackage geodist\n\nconst version string = \"1.0.0\"\n\n// Coord represents a geographic coordinate\ntype Coord struct {\n\tLat float64\n\tLon float64\n}\n"
  },
  {
    "path": "vincenty.go",
    "content": "/*\n\nvincenty.go\n-John Taylor\n\nCompute the distance between two geographic points when given a pair of latitude-longitude coordinates\n\nVincenty formula:\nhttps://en.wikipedia.org/wiki/Vincenty%27s_formulae\n\nThe code below was ported from Chris Veness's JavaScript version:\nhttps://web.archive.org/web/20181109001358/http://www.5thandpenn.com/GeoMaps/GMapsExamples/distanceComplete2.html\n\n*/\n\npackage geodist\n\nimport (\n\t\"errors\"\n\t\"math\"\n)\n\n// these constants are used for vincentyDistance()\n// reference: https://en.wikipedia.org/wiki/World_Geodetic_System#1984_version\nconst a = 6378137\nconst b = 6356752.3142\nconst f = 1 / 298.257223563 // WGS-84 ellipsiod\n\n/*\nVincentyDistance computes the distances between two georgaphic coordinates\n\nArgs:\n\tp1: the 'starting' point, given in latitude, longitude as a Coord struct\n\n\tp2: the 'ending' point\n\nReturns:\n\tA 3 element tuple: distance between the 2 points given in (1) miles and (2) kilometers\n\tThe 3rd element will return true upon a successful computation or\n\tfalse if the algorithm fails to converge. -1, -1, false is returned upon failure\n*/\nfunc VincentyDistance(p1, p2 Coord) (float64, float64, error) {\n\t// convert from degrees to radians\n\tpiRad := math.Pi / 180\n\tp1.Lat = p1.Lat * piRad\n\tp1.Lon = p1.Lon * piRad\n\tp2.Lat = p2.Lat * piRad\n\tp2.Lon = p2.Lon * piRad\n\n\tL := p2.Lon - p1.Lon\n\n\tU1 := math.Atan((1 - f) * math.Tan(p1.Lat))\n\tU2 := math.Atan((1 - f) * math.Tan(p2.Lat))\n\n\tsinU1 := math.Sin(U1)\n\tcosU1 := math.Cos(U1)\n\tsinU2 := math.Sin(U2)\n\tcosU2 := math.Cos(U2)\n\n\tlambda := L\n\tlambdaP := 2 * math.Pi\n\titerLimit := 20\n\n\tvar sinLambda, cosLambda, sinSigma float64\n\tvar cosSigma, sigma, sinAlpha, cosSqAlpha, cos2SigmaM, C float64\n\n\tfor {\n\t\tif math.Abs(lambda-lambdaP) > 1e-12 && (iterLimit > 0) {\n\t\t\titerLimit -= 1\n\t\t} else {\n\t\t\tbreak\n\t\t}\n\t\tsinLambda = math.Sin(lambda)\n\t\tcosLambda = math.Cos(lambda)\n\n\t\tsinSigma = math.Sqrt((cosU2*sinLambda)*(cosU2*sinLambda) + (cosU1*sinU2-sinU1*cosU2*cosLambda)*(cosU1*sinU2-sinU1*cosU2*cosLambda))\n\t\tif sinSigma == 0 {\n\t\t\treturn 0, 0, nil // co-incident points\n\t\t}\n\n\t\tcosSigma = sinU1*sinU2 + cosU1*cosU2*cosLambda\n\t\tsigma = math.Atan2(sinSigma, cosSigma)\n\t\tsinAlpha = cosU1 * cosU2 * sinLambda / sinSigma\n\t\tcosSqAlpha = 1 - sinAlpha*sinAlpha\n\t\tcos2SigmaM = cosSigma - 2*sinU1*sinU2/cosSqAlpha\n\t\tif math.IsNaN(cos2SigmaM) {\n\t\t\tcos2SigmaM = 0 // equatorial line: cosSqAlpha=0\n\t\t}\n\n\t\tC = f / 16 * cosSqAlpha * (4 + f*(4-3*cosSqAlpha))\n\t\tlambdaP = lambda\n\t\tlambda = L + (1-C)*f*sinAlpha*(sigma+C*sinSigma*(cos2SigmaM+C*cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)))\n\t}\n\tif iterLimit == 0 {\n\t\treturn -1, -1, errors.New(\"vincenty algorithm failed to converge\") // formula failed to converge\n\t}\n\n\tuSq := cosSqAlpha * (a*a - b*b) / (b * b)\n\tA := 1 + uSq/16384*(4096+uSq*(-768+uSq*(320-175*uSq)))\n\tB := uSq / 1024 * (256 + uSq*(-128+uSq*(74-47*uSq)))\n\tdeltaSigma := B * sinSigma * (cos2SigmaM + B/4*(cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)-B/6*cos2SigmaM*(-3+4*sinSigma*sinSigma)*(-3+4*cos2SigmaM*cos2SigmaM)))\n\tmeters := b * A * (sigma - deltaSigma)\n\tkilometers := meters / 1000\n\tmiles := kilometers * 0.621371\n\treturn miles, kilometers, nil\n}\n"
  },
  {
    "path": "vincenty_test.go",
    "content": "package geodist\n\nimport (\n\t\"testing\"\n)\n\nfunc TestVincentyDistance(t *testing.T) {\n\t// distance NY-SD: 3915340.577 m\n\t// distance SD-EP: 1011300.217 m\n\t// distance EP-SL: 1663833.491 m\n\t// distance SL-NY: 1406519.972 m\n\tvar newYork = Coord{40.7128, 74.0060}\n\tvar sanDiego = Coord{32.7157, 117.1611}\n\tvar elPaso = Coord{31.7619, 106.4850}\n\tvar stLouis = Coord{38.6270, 90.1994}\n\n\tvar miles, km float64\n\tvar err error\n\n\tmiles, km, err = VincentyDistance(newYork, sanDiego)\n\tif int(miles) != 2432 || int64(km) != 3915 || err != nil {\n\t\tt.Errorf(\"Computed values: %v %10f %v\\n\", miles, km, err)\n\t\tt.Errorf(\"Incorrect computation between New York and San Diego: %v %v %v\\n\", int(miles), int64(km), err)\n\t}\n\n\tmiles, km, err = VincentyDistance(sanDiego, elPaso)\n\tif int(miles) != 628 || int64(km) != 1011 || err != nil {\n\t\tt.Errorf(\"Computed values: %v %10f %v\\n\", miles, km, err)\n\t\tt.Errorf(\"Incorrect computation between San Diego and El Paso: %v %v %v\\n\", int(miles), int64(km), err)\n\t}\n\n\tmiles, km, err = VincentyDistance(elPaso, stLouis)\n\tif int(miles) != 1033 || int(km) != 1663 || err != nil {\n\t\tt.Errorf(\"Computed values: %v %10f %v\\n\", miles, km, err)\n\t\tt.Errorf(\"Incorrect computation between El Paso and St. Louis: %v %v %v\\n\", int(miles), int64(km), err)\n\t}\n\n\tmiles, km, err = VincentyDistance(stLouis, newYork)\n\tif int(miles) != 873 || int(km) != 1406 || err != nil {\n\t\tt.Errorf(\"Computed values: %v %10f %v\\n\", miles, km, err)\n\t\tt.Errorf(\"Incorrect computation between St. Louis and New York: %v %v %v\\n\", int(miles), int64(km), err)\n\t}\n\n\tmiles, km, err = VincentyDistance(newYork, newYork)\n\tif int(miles) != 0 || int(km) != 0 || err != nil {\n\t\tt.Errorf(\"Computed values: %v %10f %v\\n\", miles, km, err)\n\t\tt.Errorf(\"Incorrect computation between New York and New York: %v %v %v\\n\", int(miles), int64(km), err)\n\t}\n}\n"
  }
]