geo

package
v1.92.1 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package geo provides functionality to represent and process geographical locations on a spherical Earth.

Index

Constants

View Source
const (
	Degree Degrees  = 1
	Radian Radians  = 1
	Turn   Turns    = 1
	Meter  Distance = 1
)
View Source
const (
	// EarthMeanRadius is the volumetric mean radius of the Earth.
	EarthMeanRadius = 6_371_000 * Meter
	// EarthMeanCircumference is the volumetric mean circumference of the Earth.
	EarthMeanCircumference = 2 * math.Pi * EarthMeanRadius
)

Earth Fact Sheet https://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html

View Source
const MinSeparation = 50_000 * Meter

MinSeparation is the minimum separation between two points after quantizing. Point.Quantize guarantees that two points will either be snapped to exactly the same point, which conflates multiple positions together, or that the two points will be far enough apart that successfully performing most reverse lookups would be highly improbable.

Variables

View Source
var ErrBadPoint = errors.New("not a valid point")

ErrBadPoint indicates that the point is malformed.

Functions

This section is empty.

Types

type Degrees

type Degrees float64

Degrees represents a latitude or longitude, in decimal degrees.

func MustParseDegrees

func MustParseDegrees(s string) Degrees

MustParseDegrees parses s as decimal degrees, but panics on error.

func ParseDegrees

func ParseDegrees(s string) (Degrees, error)

ParseDegrees parses s as decimal degrees.

func (Degrees) AppendText

func (d Degrees) AppendText(b []byte) ([]byte, error)

AppendText implements encoding.TextAppender. The output is formatted in decimal degrees, prefixed by either the appropriate + or - sign.

func (Degrees) AppendZeroPaddedText

func (d Degrees) AppendZeroPaddedText(b []byte, nint int) []byte

AppendZeroPaddedText appends d formatted as decimal degrees to b. The number of integer digits will be zero-padded to nint.

func (Degrees) Radians

func (d Degrees) Radians() Radians

Radians converts d into radians.

func (Degrees) String

func (d Degrees) String() string

String implements the [Stringer] interface. The output is formatted in decimal degrees, prefixed by either the appropriate + or - sign, and suffixed by a ° degree symbol.

func (Degrees) Turns

func (d Degrees) Turns() Turns

Turns converts d into a number of turns.

type Distance

type Distance float64

Distance represents a great-circle distance in meters.

func DistanceOnEarth

func DistanceOnEarth(t Turns) Distance

DistanceOnEarth converts t turns into the great-circle distance, in meters.

func MustParseDistance

func MustParseDistance(s string) Distance

MustParseDistance parses s as distance in meters, but panics on error.

func ParseDistance

func ParseDistance(s string) (Distance, error)

ParseDistance parses s as distance in meters.

func (Distance) String

func (d Distance) String() string

String implements the [Stringer] interface.

type Point

type Point struct {
	// contains filtered or unexported fields
}

Point represents a pair of latitude and longitude coordinates.

func MakePoint

func MakePoint(latitude, longitude Degrees) Point

MakePoint returns a Point representing a given latitude and longitude on a WGS 84 ellipsoid. The Coordinate Reference System is EPSG:4326. Latitude is wrapped to [-90°, +90°] and longitude to (-180°, +180°].

func (Point) AppendBinary

func (p Point) AppendBinary(b []byte) ([]byte, error)

AppendBinary implements encoding.BinaryAppender. The output consists of two float32s in big-endian byte order: latitude and longitude offset by 180°. If p is not a valid, the output will be an 8-byte zero value.

func (Point) AppendText

func (p Point) AppendText(b []byte) ([]byte, error)

AppendText implements encoding.TextAppender. The output is a point formatted as OGC Well-Known Text, as "POINT (longitude latitude)" where longitude and latitude are in decimal degrees. If p is not valid, the output will be "POINT EMPTY".

func (Point) DistanceTo

func (p Point) DistanceTo(q Point) (Distance, error)

DistanceTo reports the great-circle distance between p and q, in meters.

func (Point) EqualApprox

func (p Point) EqualApprox(q Point, tol float64) bool

EqualApprox reports if p and q are approximately equal: that is the absolute difference of both latitude and longitude are less than tol. If tol is negative, then tol defaults to a reasonably small number (10⁻⁵). If tol is zero, then p and q must be exactly equal.

func (Point) IsZero

func (p Point) IsZero() bool

IsZero reports if p is the zero value.

func (Point) LatLng

func (p Point) LatLng() (lat, lng Degrees, err error)

LatLng reports the latitude and longitude.

func (Point) LatLngFloat64

func (p Point) LatLngFloat64() (lat, lng float64, err error)

LatLng reports the latitude and longitude in float64. If err is nil, then lat and lng will never both be 0.0 to disambiguate between an empty struct and Null Island (0° 0°).

func (Point) MarshalBinary

func (p Point) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaller. The output matches that of calling Point.AppendBinary.

func (Point) MarshalText

func (p Point) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaller. The output matches that of calling Point.AppendText.

func (Point) MarshalUint64

func (p Point) MarshalUint64() (uint64, error)

MarshalUint64 produces the same output as MashalBinary, encoded in a uint64.

func (Point) Quantize

func (p Point) Quantize() Point

Quantize returns a new Point after throwing away enough location data in p so that it would be difficult to distinguish a node among all the other nodes in its general vicinity. One caveat is that if there’s only one point in an obscure location, someone could triangulate the node using additional data.

This method is stable: given the same p, it will always return the same result. It is equivalent to snapping to points on Earth that are at least MinSeparation apart.

func (Point) SphericalAngleTo

func (p Point) SphericalAngleTo(q Point) (Radians, error)

SphericalAngleTo returns the angular distance from p to q, calculated on a spherical Earth.

func (Point) String

func (p Point) String() string

String returns a space-separated pair of latitude and longitude, in decimal degrees. Positive latitudes are in the northern hemisphere, and positive longitudes are east of the prime meridian. If p was not initialized, this will return "nowhere".

func (*Point) UnmarshalBinary

func (p *Point) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler. It expects input that was formatted by Point.AppendBinary: in big-endian byte order, a float32 representing latitude followed by a float32 representing longitude offset by 180°. If latitude and longitude fall outside valid ranges, then an error is returned.

func (*Point) UnmarshalUint64

func (p *Point) UnmarshalUint64(v uint64) error

UnmarshalUint64 expects input formatted by MarshalUint64.

func (Point) Valid

func (p Point) Valid() bool

Valid reports if p is a valid point.

type Radians

type Radians float64

Radians represents a latitude or longitude, in radians.

func MustParseRadians

func MustParseRadians(s string) Radians

MustParseRadians parses s as radians, but panics on error.

func ParseRadians

func ParseRadians(s string) (Radians, error)

ParseRadians parses s as radians.

func (Radians) Degrees

func (r Radians) Degrees() Degrees

Degrees converts r into decimal degrees.

func (Radians) String

func (r Radians) String() string

String implements the [Stringer] interface.

func (Radians) Turns

func (r Radians) Turns() Turns

Turns converts r into a number of turns.

type Turns

type Turns float64

Turns represents a number of complete revolutions around a sphere.

func (Turns) Degrees

func (o Turns) Degrees() Degrees

Degrees converts t into decimal degrees.

func (Turns) Radians

func (o Turns) Radians() Radians

Radians converts t into radians.

func (Turns) String

func (o Turns) String() string

String implements the [Stringer] interface.

Jump to

Keyboard shortcuts

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