geo_range

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

geo-range

A library with encoding/decoding geohashes and searching geohashes by coordinates and radius

Usage

go get -u github.com/hjhsamuel/geo-range
GeoHash

Example Code:

hashes := geo_range.RadiusSearch(30.2027354, 120.22543349, 5000, 0.8, nil)
combined := geo_range.MergeHashes(hashes, 20)

Note:

Geohash range searches are imprecise, sometimes you should implement PrecisionDynamicFunc and use your own function

Coordinate

If you prefer not to use GeoHash-based tiling for retrieval, you can also use a simple bounding box (MBR) match followed by distance filtering. This approach also delivers excellent performance in MySQL 8.0+.

create table tb_marker (
    id biging unsigned primary key auto_increment,
    geohash char(12) not null,
    location point not null
) charset = utf8mb4;

create index idx_geohash on tb_marker (geohash);

create spatial index idx_location on tb_marker (location);

Example Code:

point := &geo_hash.Location{Lng: 120.22543349, Lat: 30.2027354}
northPoint := geo_hash.GetPointAtDistance(point, 5000, 0)
eastPoint := geo_hash.GetPointAtDistance(point, 5000, 90)
southPoint := geo_hash.GetPointAtDistance(point, 5000, 180)
westPoint := geo_hash.GetPointAtDistance(point, 5000, 270)

rightTop := &Location{Lng: eastPoint.Lng, Lat: northPoint.Lat}
leftBottom := &Location{Lng: westPoint.Lng, Lat: southPoint.Lat}

then you could use MBR match in MySQL:

select * from tb_marker where MBRIntersects(ST_GEOMFROMTEXT('LINESTRING(lat1 lng1, lat2 lng2)', 4326), location)

of course, you could use ST_DISTANCE_SPHERE for further precise filtering.

Specialized scenarios

  1. When the search range crosses the antimeridian (180th meridian), you must split the bounding box into two separate rectangles.
  2. When the search range encompasses a pole, the longitude span collapses to [-180, 180]. Since a traditional MBR becomes semantically invalid or excessively bloated in this case, it is recommended to constrain the search scope and perform the retrieval based solely on the latitude range.

Digits and precision

geohash length Lat bits lng bits lat error lng error km error
1 2 3 ±23 ±23 ±2500
2 5 5 ±2.8 ±5.6 ±630
3 7 8 ±0.70 ±0.7 ±78
4 10 10 ±0.087 ±0.18 ±20
5 12 13 ±0.022 ±0.022 ±2.4
6 15 15 ±0.0027 ±0.0055 ±0.61
7 17 18 ±0.00068 ±0.00068 ±0.076
8 20 20 ±0.000086 ±0.000172 ±0.01911
9 22 23 ±0.000021 ±0.000021 ±0.00478
10 25 25 ±0.00000268 ±0.00000536 ±0.0005971
11 27 28 ±0.00000067 ±0.00000067 ±0.0001492
12 30 30 ±0.00000008 ±0.00000017 ±0.0000186

Documentation

Index

Constants

View Source
const (
	DirectionBaseAngle  float64 = 45
	DirectionSplitAngle float64 = 22.5
)
View Source
const EarthRadius = 6371008.7714

Variables

This section is empty.

Functions

func GetHash

func GetHash(lat, lng float64) (string, error)

func Haversine

func Haversine(a, b *Location) float64

Haversine

Calculate the distance between two points

func IsCoordinateValid

func IsCoordinateValid(lat, lng float64) bool

func IsNearPolyline

func IsNearPolyline(point *Location, polyline []*Location, threshold float64) bool

IsNearPolyline

Is point near the line

Param:

point: the location of point
polyline: the list of point of line
threshold: distance(meter)

func MergeHashes

func MergeHashes(hashes []string, minMergedCnt int) []string

func PointToSegmentDistance

func PointToSegmentDistance(point, start, end *Location) float64

PointToSegmentDistance

The distance from point to line

Param:

point: the location of point
start: the start location of line
end: the end location of line

func RadiusSearch

func RadiusSearch(lat, lng, radius, tolerance float64, f PrecisionDynamicFunc) []string

RadiusSearch Get geohashes by coordinate and radius

Params:

lat: latitude
lng: longitude
radius: radius(meter)
tolerance: the tolerance to check is rectangle is intersected with circle, (0, 1.0]
f: the custom function to get start and maximum precision by radius

Types

type Direction

type Direction int
const (
	North Direction = iota
	NorthEast
	East
	SouthEast
	South
	SouthWest
	West
	NorthWest
)

func GuessDirection

func GuessDirection(brg float64) Direction

type GeoTrieRoot

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

func NewGeoTrie

func NewGeoTrie() *GeoTrieRoot

func (*GeoTrieRoot) AllLeaf

func (t *GeoTrieRoot) AllLeaf() []string

func (*GeoTrieRoot) Has

func (t *GeoTrieRoot) Has(key string) bool

func (*GeoTrieRoot) Insert

func (t *GeoTrieRoot) Insert(key string) bool

type Location

type Location struct {
	Lng float64
	Lat float64
}

func GetPointAtDistance

func GetPointAtDistance(point *Location, radius float64, angle float64) *Location

GetPointAtDistance

Calculates the destination coordinates given a starting point, distance (radius), and bearing (direction)

Param:

point: point location
radius: the distance to travel from the center, in meters
angle: the direction of travel in degrees (0 = North, 90 = East, 180 = South, 270 = West)

func SplitLine

func SplitLine(start, end *Location, distance float64) []*Location

SplitLine

Split a long line to multiple short lines

Param:

start: the start point location of line
end : the end point location of line
distance: the max distance of each short line(meter)

type Precision

type Precision uint
const (
	Country     Precision = 2 // ±630km
	Province    Precision = 3 // ±78km
	City        Precision = 4 // ±20km
	District    Precision = 5 // ±2km
	Street      Precision = 6 // ±500m
	Village     Precision = 7 // less than 100m
	Building    Precision = 8 // ±20m
	HouseNumber Precision = 9 // less than 10m
)

func GetPrecisionDynamic

func GetPrecisionDynamic(radius float64) (start, max Precision)

GetPrecisionDynamic

get the start precision and maximum precision, this is only a preset configuration, you could replace it with your function

type PrecisionDynamicFunc

type PrecisionDynamicFunc func(radius float64) (start, max Precision)

Jump to

Keyboard shortcuts

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