geo.go
geo.go - Overview
This file provides geographical functions, including distance calculations and GeoHash encoding/decoding.
Detailed Documentation
const earthRadius float64
const earthRadius float64 = 6372797.560856
- Purpose: Defines the Earth's radius in meters.
const bitPrecision int
const bitPrecision = 52
- Purpose: Defines the bit precision for GeoHash.
const bitPrecisionString int
const bitPrecisionString = 10
- Purpose: Defines the bit precision for GeoHash strings.
func DegToRad(deg float64) float64
func DegToRad(deg float64) float64 {
return math.Pi * deg / 180.0
}
- Purpose: Converts degrees to radians.
- Parameters:
- deg: The angle in degrees.
- Returns: The angle in radians.
- Parameters:
func RadToDeg(rad float64) float64
func RadToDeg(rad float64) float64 {
return 180.0 * rad / math.Pi
}
- Purpose: Converts radians to degrees.
- Parameters:
- rad: The angle in radians.
- Returns: The angle in degrees.
- Parameters:
func GetDistance(lon1 float64, lat1 float64, lon2 float64, lat2 float64) float64
func GetDistance(
lon1,
lat1,
lon2,
lat2 float64,
) float64 {
lon1r := DegToRad(lon1)
lon2r := DegToRad(lon2)
v := math.Sin((lon2r - lon1r) / 2)
// if v == 0 we can avoid doing expensive math when lons are practically the same
if v == 0.0 {
return GetLatDistance(lat1, lat2)
}
lat1r := DegToRad(lat1)
lat2r := DegToRad(lat2)
u := math.Sin((lat2r - lat1r) / 2)
a := u*u + math.Cos(lat1r)*math.Cos(lat2r)*v*v
return 2.0 * earthRadius * math.Asin(math.Sqrt(a))
}
- Purpose: Calculates the distance between two points on Earth using the Haversine formula.
- Parameters:
- lon1: Longitude of the first point in degrees.
- lat1: Latitude of the first point in degrees.
- lon2: Longitude of the second point in degrees.
- lat2: Latitude of the second point in degrees.
- Returns: The distance between the two points in meters.
- Parameters:
func GetLatDistance(lat1 float64, lat2 float64) float64
func GetLatDistance(lat1, lat2 float64) float64 {
return earthRadius * math.Abs(DegToRad(lat2)-DegToRad(lat1))
}
- Purpose: Calculates the distance between two points on the same longitude.
- Parameters:
- lat1: Latitude of the first point in degrees.
- lat2: Latitude of the second point in degrees.
- Returns: The distance between the two points in meters.
- Parameters:
func EncodeInt(lat float64, lon float64) float64
func EncodeInt(lat, lon float64) float64 {
h := geohash.EncodeIntWithPrecision(lat, lon, bitPrecision)
return float64(h)
}
- Purpose: Encodes latitude and longitude into a GeoHash integer.
- Parameters:
- lat: Latitude in degrees.
- lon: Longitude in degrees.
- Returns: The GeoHash as a float64.
- Parameters:
func DecodeInt(hash float64) (lat float64, lon float64)
func DecodeInt(hash float64) (lat, lon float64) {
lat, lon = geohash.DecodeIntWithPrecision(uint64(hash), bitPrecision)
return lat, lon
}
- Purpose: Decodes a GeoHash integer into latitude and longitude.
- Parameters:
- hash: The GeoHash as a float64.
- Returns:
- lat: Latitude in degrees.
- lon: Longitude in degrees.
- Parameters:
func EncodeString(lat float64, lon float64) string
func EncodeString(lat, lon float64) string {
return geohash.EncodeWithPrecision(lat, lon, bitPrecisionString)
}
- Purpose: Encodes latitude and longitude into a GeoHash string.
- Parameters:
- lat: Latitude in degrees.
- lon: Longitude in degrees.
- Returns: The GeoHash string.
- Parameters:
func ConvertDistance(distance float64, unit string) (converted float64, err []byte)
func ConvertDistance(
distance float64,
unit string,
) (converted float64, err []byte) {
switch unit {
case "m":
return distance, nil
case "km":
return distance / 1000, nil
case "mi":
return distance / 1609.34, nil
case "ft":
return distance / 0.3048, nil
default:
return 0, errors.NewErrWithMessage("ERR unsupported unit provided. please use m, km, ft, mi")
}
}
- Purpose: Converts a distance from meters to the specified unit.
- Parameters:
- distance: The distance in meters.
- unit: The unit to convert to ("m", "km", "mi", "ft").
- Returns:
- converted: The converted distance.
- err: An error message if the unit is unsupported.
- Parameters: