geodist

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 2 Imported by: 28 modules, 23 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

geodist

A Go package for computing the distance between two geographic coordinates using the Haversine or Vincenty formula.

Installation

go get github.com/jftuga/geodist

Algorithm Comparison

Both functions return distances in miles and kilometers, in that order. Coordinates are in degrees, with latitude in [-90, 90] and any finite longitude (normalized modulo 360). West longitudes and south latitudes are negative.

VincentyDistance returns -1, -1, error for invalid coordinates or failure to converge. Some nearly antipodal points do not converge. HaversineDistance returns NaN, NaN for invalid coordinates; it uses a sphere with a radius of 6,378,100 meters.

Example

The following complete program is also available in example/example.go.

package main

import (
	"fmt"

	"github.com/jftuga/geodist"
)

func main() {
	var newYork = geodist.Coord{Lat: 40.7128, Lon: -74.0060}
	var sanDiego = geodist.Coord{Lat: 32.7157, Lon: -117.1611}
	miles, km, err := geodist.VincentyDistance(newYork, sanDiego)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf(" [Vincenty] New York to San Diego: %.3f mi, %.3f km\n", miles, km)
	miles, km = geodist.HaversineDistance(newYork, sanDiego)
	fmt.Printf("[Haversine] New York to San Diego: %.3f mi, %.3f km\n", miles, km)

	fmt.Println()

	var elPaso = geodist.Coord{Lat: 31.7619, Lon: -106.4850}
	var stLouis = geodist.Coord{Lat: 38.6270, Lon: -90.1994}
	miles, km, err = geodist.VincentyDistance(elPaso, stLouis)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf(" [Vincenty] El Paso to St. Louis: %.3f mi, %.3f km\n", miles, km)
	miles, km = geodist.HaversineDistance(elPaso, stLouis)
	fmt.Printf("[Haversine] El Paso to St. Louis: %.3f mi, %.3f km\n", miles, km)
}

Run it from the repository root:

go run ./example

Output:

 [Vincenty] New York to San Diego: 2432.880 mi, 3915.341 km
[Haversine] New York to San Diego: 2430.504 mi, 3911.517 km

 [Vincenty] El Paso to St. Louis: 1033.858 mi, 1663.833 km
[Haversine] El Paso to St. Louis: 1033.678 mi, 1663.543 km

Online Calculators

When comparing results, check the calculator's units and Earth model. Spherical calculators may use a different Earth radius from this package.

Acknowledgements

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HaversineDistance

func HaversineDistance(p1, p2 Coord) (float64, float64)

HaversineDistance returns the great-circle distance in miles and kilometers, using a spherical approximation of the Earth with a radius of 6,378,100 meters. Coordinates are supplied in degrees. Invalid coordinates return NaN, NaN.

http://en.wikipedia.org/wiki/Haversine_formula

func VincentyDistance

func VincentyDistance(p1, p2 Coord) (float64, float64, error)

VincentyDistance returns the distance in miles and kilometers on the WGS-84 ellipsoid. Coordinates are supplied in degrees. It returns -1, -1, and an error for invalid coordinates or if the algorithm fails to converge, which can occur for nearly antipodal points.

Types

type Coord

type Coord struct {
	Lat float64
	Lon float64
}

Coord represents a geographic coordinate in degrees. Latitude must be in [-90, 90]; longitude may be any finite value and is normalized modulo 360.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址