geo

package module
v0.0.0-...-27f64a4 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2023 License: MIT Imports: 13 Imported by: 1

README

geo

Geographic utilities

Documentation

Index

Constants

View Source
const (
	// DegreeToKilometer is a "constant" for latitude but varies for longitude
	DegreeToKilometer     = 111.111 //111.321
	MilesToKilometer      = 1.609344
	EarthRadiusInKM       = 6371.1 // 6378.1
	SquareKmPerSquareMile = 0.386102
)
View Source
const Radian = math.Pi / 180.0

Variables

View Source
var (
	ErrInvalidCoordinates = errors.New("invalid coordinates")
)
View Source
var ErrNotFound = errors.New("not found")

Functions

func ApproximateDistance

func ApproximateDistance(lat1, lon1, lat2, lon2 float64) float64

ApproximateDistanceGeo returns the approximate distance between 2 points It uses the pythagarean distance calc which is meant for 2d operations but is "good enough" for shorter distances (which we primarily care about) It is about 7 times faster than the "proper way"

func ApproximateDistanceGeo

func ApproximateDistanceGeo(lat1, lon1, lat2, lon2 GeoType) float64

ApproximateDistanceGeo returns the approximate distance between 2 points It uses the pythagarean distance calc which is meant for 2d operations but is "good enough" for shorter distances (which we primarily care about) It is about 7 times faster than the "proper way"

func AreaInKm

func AreaInKm(lat1, lon1, lat2, lon2 float64) float64

func AreaInMiles

func AreaInMiles(lat1, lon1, lat2, lon2 float64) float64

func Bestest

func Bestest(g GeoPoints, pt Point, deltaKm float64) (int, float64)

Bestest searches for a matching point within the distance (in Km) of the specified point. It returns the index of the closest point and the distance from the target If nothing is found, it returns the Len() of the points list and -1 distance

NOTE: this is an adaptation of Bestest, but distances are approximated to

minimize computational load

TODO: the len return is in line w/ Go sort.Search, but perhaps -1 would be better? TODO part too: use distance func to share same routine w/ approx and haversine calcs?

func Closest

func Closest(g GeoPoints, pt Point, deltaKm float64) (int, float64)

Closest searches for a matching point within the distance (in Km) of the specified point. It returns the index of the closest point and the distance from the target If nothing is found, it returns the Len() of the points list and -1 distance

NOTE: this is an adaptation of Bestest, but distances are approximated to

minimize computational load

TODO: the len return is in line w/ Go sort.Search, but perhaps -1 would be better? TODO part too: use distance func to share same routine w/ approx and haversine calcs?

func Distance

func Distance(lat1, lon1, lat2, lon2 float64) float64

Distance returns the distance in kM between 2 geographic points It uses the Haversine formula for spherical calculations

func Distance32

func Distance32(lat1, lon1, lat2, lon2 float32) float64

func DistanceGeoType

func DistanceGeoType(lat1, lon1, lat2, lon2 GeoType) float64

func LoadLines

func LoadLines(filename string, fn func(string) error) error

func LonKilos

func LonKilos(lat float64) float64

LonKilos returns the kilometers per degree longitude at the given latitude

func LongitudeKilometerDegrees

func LongitudeKilometerDegrees(lat, kilometers float64) float64

LongitudeKilometerDegrees converts the distance given in kilometers at that latitude to degrees

func LongitudeKilometers

func LongitudeKilometers(lat, lon float64) float64

LongitudeKilometers returns the distance of the degrees lon, at latitude lat

func LookupLonKmPerLat

func LookupLonKmPerLat(lat float64) float64

LookupLonKmPerLat returns the ratio of kilometers to degrees longitude at the given latitude.

Accuracy is with 1% under 80 degrees, which is good enough for most work

func SquareKmInMiles

func SquareKmInMiles(k float64) float64

func Within

func Within(lat, lon, minLat, minLon, maxLat, maxLon GeoType) bool

Types

type Container

type Container interface {
	ContainsPoint(Point) bool
}

type Decoder

type Decoder interface {
	Decode([]byte) error
	Size() int // size of struct
	Point() Point
	// Less(Point) bool
	JSON(w io.Writer) error
}

type GeoPoints

type GeoPoints interface {
	IndexPoint(int) Point
	Len() int
}

GeoPoints provides abstraction for slices of data with coordinates

type GeoType

type GeoType float32

GeoType for coordinates with slightly less accuracy a float32 has 7 digits of precision, which is within ~11cm

Virtually all geo data is "close enough" using this, and for data that heavily comprises these points, one can reduce memory footprint by one half (for points)

func Coords

func Coords(query string) ([]GeoType, error)

func ToGeoType

func ToGeoType(value interface{}) (GeoType, error)

type Iter

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

func (*Iter) Get

func (m *Iter) Get(i int) interface{}

func (*Iter) IndexPoint

func (m *Iter) IndexPoint(i int) Point

func (*Iter) JSON

func (m *Iter) JSON(w io.Writer)

func (*Iter) Len

func (m *Iter) Len() int

func (*Iter) Less

func (m *Iter) Less(pt Point) bool

func (*Iter) Load

func (m *Iter) Load(i int)

func (*Iter) Ranger

func (m *Iter) Ranger(from, to Point, fn func(interface{}), ctr Container) error

type LineInfo

type LineInfo struct {
	Index    int
	Line     string
	Distance float64
}

func Nearest

func Nearest(filename string, pt Point, latLon bool) (LineInfo, error)

Nearest scans a csv file with lon,lat coordinates and returns the line that is closest to the given point

type MFile

type MFile struct {
	B []byte
}

func Mmap

func Mmap(filename string) (*MFile, error)

func (*MFile) Close

func (m *MFile) Close() error

func (*MFile) NewIter

func (m *MFile) NewIter(d Decoder) *Iter

func (*MFile) ReadAt

func (m *MFile) ReadAt(p []byte, i int64) (int, error)

type Pair

type Pair [2]float64

func DecodePair

func DecodePair(buf []byte) Pair

func QueryCoords

func QueryCoords(s string) (Pair, error)

type Point

type Point struct {
	Lat, Lon GeoType
}

func DecodePoint

func DecodePoint(buf []byte) Point

func GeoPoint

func GeoPoint(lat, lon float64) Point

func QueryPoint

func QueryPoint(s string) (Point, error)

func (Point) Approximately

func (p Point) Approximately(x Point) float64

func (Point) Distance

func (p Point) Distance(x Point) float64

func (Point) Label

func (p Point) Label() string

Label returns a consistent string representation of the coordinates

func (Point) Less

func (p Point) Less(x Point) bool

Less returns true if it is less than the given point

type Rect

type Rect [2]Pair

func AreaInRange64

func AreaInRange64(pt Pair, distance float64) Rect

AreaInRange64 is like AreaInRange but using float64

func Expand

func Expand(lat, lon, radiusKM float64) Rect

Expand returns the a box with a radius in kM for

Directories

Path Synopsis
cmd
dist command
nearest command

Jump to

Keyboard shortcuts

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