Go · Geospatial Metrics

Geospatial Metrics

import "github.com/ashvardanian/NumKong" 8.0.0-rc.1 · b1f866ab

The Go package also exposes Haversine and Vincenty helpers, from every point of one set to every point of another. Inputs are in radians. Outputs are written into caller-owned slices, one row of distances per point of the first set.

package main

import (
	"fmt"

	nk "github.com/ashvardanian/NumKong/golang"
)

func main() {
	// Statue of Liberty (40.6892°N, 74.0445°W) → Big Ben (51.5007°N, 0.1246°W)
	libertyLatitudes := []float64{0.7101605100}
	libertyLongitudes := []float64{-1.2923203180}
	bigBenLatitudes := []float64{0.8988567821}
	bigBenLongitudes := []float64{-0.0021746802}
	distances := make([]float64, 1)

	nk.VincentysF64(libertyLatitudes, libertyLongitudes, bigBenLatitudes, bigBenLongitudes, distances)  // ≈ 5,589,857 m on WGS-84
	nk.HaversinesF64(libertyLatitudes, libertyLongitudes, bigBenLatitudes, bigBenLongitudes, distances) // ≈ 5,574,848 m on the mean sphere

	fmt.Println(distances[0])
}

The output slice is caller-owned. That keeps allocation behavior explicit and predictable.

API

Functions

HaversinesF64HaversinesF64 writes the great-circle distance in meters from each a point to each b point, with float64 coordinates in radians, into results, row-major with len(bLatitudes) columns.
numkong.go:17 ↗
func HaversinesF64(
    aLatitudes, aLongitudes, bLatitudes, bLongitudes, results []float64,
)

Each latitude slice matches its longitude slice, and results holds len(aLatitudes) * len(bLatitudes) entries.

VincentysF64VincentysF64 writes the ellipsoidal geodesic distance in meters from each a point to each b point, with float64 coordinates in radians, into results, row-major with len(bLatitudes) columns.
numkong.go:31 ↗
func VincentysF64(
    aLatitudes, aLongitudes, bLatitudes, bLongitudes, results []float64,
)

Each latitude slice matches its longitude slice, and results holds len(aLatitudes) * len(bLatitudes) entries.