earth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

README

pg/earth

The earthdistance extension for sqlb. Needs CREATE EXTENSION cube then CREATE EXTENSION earthdistance, and a GiST index on ll_to_earth(lat, lon) for the box search.

SQL Helper
ll_to_earth(lat, lon) LLToEarth
earth_distance(a, b) Distance
earth_box(p, r) Box
box @> p BoxContains

Radius search, box first for the index, exact distance second:

here := earth.LLToEarth(sqlb.Param(lat), sqlb.Param(lon))
there := earth.LLToEarth(PlaceLat, PlaceLon)
sel.Where(earth.BoxContains(earth.Box(here, radius), there)).
	Where(sqlb.LessEq(earth.Distance(here, there), radius))

Beyond "what is within r metres", use pg/postgis.

Documentation

Overview

Package earth is the earthdistance extension (with cube): great-circle distance and bounding boxes on the sphere. Needs CREATE EXTENSION cube and CREATE EXTENSION earthdistance. For anything beyond a radius search, use pg/postgis.

Index

Constants

This section is empty.

Variables

View Source
var Cube = pg.Extension{Name: "cube"}

Cube is the extension earthdistance depends on. CREATE EXTENSION does not install it unless CASCADE is written, which this module leaves out, so it is named here to be installed first.

View Source
var Extension = pg.Extension{Name: "earthdistance"}

Extension is the earthdistance extension this package's constructs need, for pg.CreateExtension(Extension), so a library installs what it uses without spelling its name.

Functions

func Box

func Box(p sqlb.Expr[Point], radiusMetres float64) sqlb.Expr[Point]

Box renders earth_box(point, radiusMetres), the index-served pre-filter.

func BoxContains

func BoxContains(box, p sqlb.Expr[Point]) sqlb.Predicate

BoxContains renders box @> point. Pair it with Distance for the exact cut, because the box is square and the radius is round.

func Distance

func Distance(a, b sqlb.Expr[Point]) sqlb.Expr[float64]

Distance renders earth_distance(a, b) in metres.

func LLToEarth

func LLToEarth(lat, lon sqlb.Expr[float64]) sqlb.Expr[Point]

LLToEarth renders ll_to_earth(lat, lon).

Types

type Point

type Point string

Point is an earth value (a cube). Only ever produced by the server; scan it as text if you need to read it back.

Jump to

Keyboard shortcuts

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