geo

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2021 License: MIT Imports: 3 Imported by: 1

README

go-geo

Geographic coordinates and distances library written in Go.

Representing coordinates

myLocation := geo.Coordinate{
    Latitude: 39.7684426,
    Longitude: -86.1583112,
}

Calculating distances

a := geo.Coordinate{
    Latitude: 39.7684426,
    Longitude: -86.1583112,
}
b := geo.Coordinate{
    Latitude: 38.3531417,
    Longitude: -81.6388193,
}

// Two ways of calculating the distance:
distance := a.DistanceTo(b)
distance := geo.DistanceBetween(a, b)

fmt.Println("Distance is: ", distance)

// Prints:
// --> 420.6896449044601km

(Note: Ending up with a 420.69km example was a complete accident but I'm not changing it)

Working with geo.Distance

The geo.Distance type represents distances in geographical space. Some predefined constants can be used for convenience:

geo.Millimeter
geo.Centimeter
geo.Meter
geo.Kilometer
geo.Mile

You can construct distances like so:

twoMiles := 2 * geo.Mile
tenKm := 10 * geo.Kilometer

You can also fetch the float64 conversion of a distance represented in any units you like:

// Construct a distance (type geo.Distance)
distance := 15 * geo.Mile

// Get the equivalent number of kilometers (type float64)
inKilometers := distance.Kilometers()

In the above example, the value of inKilometers is equal to 24.14015969842181, which is the number of kilometers equal to 15 miles.

Documentation

Index

Constants

View Source
const (
	EarthRadius       = 6371 * Kilometer
	DoubleEarthRadius = 2 * EarthRadius
	PiOver180         = math.Pi / 180
)

Constants needed for distance calculations

View Source
const (
	Millimeter = Distance(0.001)
	Centimeter = Distance(0.01)
	Meter      = Distance(1)
	Kilometer  = Distance(1000)
	Mile       = Distance(1 / MilesPerKilometer * 1000)
)

Standard length constants

View Source
const MilesPerKilometer = 0.6213712

Constants for conversions

Variables

This section is empty.

Functions

This section is empty.

Types

type Coordinate

type Coordinate struct {
	Latitude, Longitude float64
}

Coordinate represents a specific location on Earth

func (Coordinate) DistanceTo

func (c Coordinate) DistanceTo(other Coordinate) Distance

DistanceTo calculates the distance from this coordinate to another coordinate

func (Coordinate) String

func (c Coordinate) String() string

String implements Stringer, returns a string representation of the coordinate

type Distance

type Distance float64

Distance represents a spacial distance. Fundamentally, the underlying float64 represents the raw number of meters

func DistanceBetween

func DistanceBetween(a, b Coordinate) Distance

DistanceBetween calculates the distance between two coordinates

func (Distance) Centimeters

func (d Distance) Centimeters() float64

Meters gets the number of total centimeters represented by the distance

func (Distance) Equals

func (d Distance) Equals(other, tolerance Distance) bool

func (Distance) Kilometers

func (d Distance) Kilometers() float64

Kilometers gets the number of total kilometers represented by the distance

func (Distance) Meters

func (d Distance) Meters() float64

Meters gets the number of total meters represented by the distance

func (Distance) Miles

func (d Distance) Miles() float64

Miles gets the number of total miles represented by the distance

func (Distance) Millimeters

func (d Distance) Millimeters() float64

Meters gets the number of total millimeters represented by the distance

func (Distance) String

func (d Distance) String() string

String implements Stringer and returns a formatted string representation of the distance

Jump to

Keyboard shortcuts

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