Help us shape the future of Go! Take the 2026 Go Developer Survey

haversine

package module
v0.0.0-...-26f756d Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2020 License: MIT Imports: 1 Imported by: 2

README

GoDoc Reference Build Status Go Report Card Coverage Status

Haversine

Package haversine provides the great circle distance between two points on the surface of the earth. Points are identified by latitude and longitude, and distance results can be returned in nautical miles, statute miles, or kilometers.
Earth great circle

The curvature of the earth dictates that the shortest distance between two points cannot be a straight line between the points. The Haversine formula provides an approximation for the shortest great circle route over the surface of earth that connects the two points. In this figure the dotted yellow line is the arc of a great circle. Image courtesy USGS.

Installation

go get github.com/FATHOM5/haversine

Usage

The example below shows how far I'd have to walk in my boots to go visit my co-founder.

package main

import (
    "fmt"

    "github.com/FATHOM5/haversine"
)

func main() {
    austin := haversine.Coord{ // Austin, Texas
        Lat: 30.2672,
        Lon: -97.7431,
    }
    paloAlto := haversine.Coord{ // Palo Alto, California
        Lat: 37.4419,
        Lon: -122.1430,
    }

    nm := haversine.Distance(austin, paloAlto)

    fmt.Printf("%.1f miles is a long walk to Silicon Valley.\n", nm)

// Output: 1286.1 miles is a long walk to Silicon Valley.
}

Documenation

References

License

See LICENSE for rights and limitations (MIT).

Documentation

Overview

Package haversine provides the great circle distance between two points on the surface of the earth. Points are identified by latitude and longitude, and distance results are returned in nautical miles. Functions are also provided to return the distance in kilometers or statute miles.

The below example shows how to calculate the shortest path between two coordinates on the surface of the Earth.

package main

import (
    "fmt"

    "github.com/FATHOM5/haversine"
)

func main() {
    austin := haversine.Coord{ // Austin, Texas
        Lat: 30.2672,
        Lon: -97.7431,
    }
    paloAlto := haversine.Coord{ // Palo Alto, California
        Lat: 37.4419,
        Lon: 122.1430,
    }

    nm := haversine.Distance(austin, paloAlto)

    fmt.Printf("%.1f miles is a long walk to Silicon Valley.\n", nm)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Distance

func Distance(p, q Coord) (nm float64)

Distance calculates the shortest path between two coordinates on the surface of the Earth and returns the result in nautical miles.

Example
package main

import (
	"fmt"

	"github.com/FATHOM5/haversine"
)

func main() {
	austin := haversine.Coord{ // Austin, Texas
		Lat: 30.2672,
		Lon: -97.7431,
	}
	paloAlto := haversine.Coord{ // Palo Alto, California
		Lat: 37.4419,
		Lon: -122.1430,
	}

	nm := haversine.Distance(austin, paloAlto)

	fmt.Printf("%.1f miles is a long walk to Silicon Valley.\n", nm)

}
Output:
1286.1 miles is a long walk to Silicon Valley.

func DistanceKm

func DistanceKm(p, q Coord) (km float64)

DistanceKm calculates the shortest path between two coordinates on the surface of the Earth and returns the result in kilometers.

func DistanceMi

func DistanceMi(p, q Coord) (mi float64)

DistanceMi calculates the shortest path between two coordinates on the surface of the Earth and returns the result in statute miles.

Types

type Coord

type Coord struct {
	Lat float64
	Lon float64
}

Coord represents a geographic coordinate.

Jump to

Keyboard shortcuts

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