Files
openfsd/postoffice/geohash_util.go
Reese Norris 57d54d6705 v0.1.0-alpha
Changes:
- Implement bootstrapping library for managing several concurrent internal services
- Refactor concurrency model for connections/logical clients and their associated I/O
- Refactor server context singleton
- Refactor error handling
    - Most errors are now gracefully sent to the FSD client directly encoded as an $ER packet,
      enhancing visibility and debugging
    - Most errors are now rightfully treated as non-fatal
- Refactor package/dependency graph
- Refactor calling conventions/interfaces for many packages
- Refactor database package
- Refactor post office

Features:
- Add VATSIM-esque HTTP/JSON "data feed"
- Add ephemeral in-memory database option
- Add user management REST API
- Add improved web interface
- Add MySQL support (drop SQLite support)
2024-10-07 12:50:39 -07:00

56 lines
1.3 KiB
Go

package postoffice
import "github.com/mmcloughlin/geohash"
// Number of bits to encode for a full precision geohash.
// This is the bit-depth of the Geohash() value stored in each Address.
const geohashFullPrecisionBits = 30
// Number of bits to utilize for determining which bucket a geohash falls into.
const geohashBucketPrecisionBits = 15
// Bits of precision to use for general-proximity broadcasts
const geohashGeneralProximityPrecisionBits = geohashBucketPrecisionBits
// Bits of precision to use for close-proximity broadcasts
const geohashCloseProximityPrecisionBits = 25
type Geohash struct {
val uint64
precision int
}
func NewGeohash(lat, lon float64) Geohash {
return Geohash{
val: geohash.EncodeIntWithPrecision(lat, lon, geohashFullPrecisionBits),
precision: geohashFullPrecisionBits,
}
}
func NewGeohashManual(hash uint64, precision int) Geohash {
return Geohash{
val: hash,
precision: precision,
}
}
func (g Geohash) Neighbors(precision int) []uint64 {
return geohash.NeighborsIntWithPrecision(g.AsPrecision(precision), uint(precision))
}
func (g Geohash) Precision() int {
return g.precision
}
func (g Geohash) Val() uint64 {
return g.val
}
func (g Geohash) AsPrecision(precision int) uint64 {
shift := g.precision - precision
if shift <= 0 {
return g.val
}
return g.val >> shift
}