Skip to content

Commit

Permalink
[WIP]: closes #1248 . working on geohash functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tdadadavid committed Nov 17, 2024
1 parent f2267bd commit 6a7be9a
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
107 changes: 107 additions & 0 deletions internal/eval/geo/geohash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package geo

import (
"fmt"
"strings"
)

type Coordinates struct {
Longitude float64
Latitude float64
}

type GoeHash struct {
Hash string
Neighbors []GoeHash
}

var (
// base32 alphabets
base32 []string = []string{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"b", "c", "d", "e", "f", "g", "h", "j", "k", "m",
"n", "p", "q", "r", "s", "t", "u", "v", "w", "x",
"y", "z",
}

// This coordinate measures north-south position and ranges from -90° (South Pole) to +90° (North Pole).
latitudeRange = []int64{90,-90}

// This coordinate measures east-west position and ranges from -180° (Western Hemisphere) to +180° (Eastern Hemisphere).
longitudeRange = []int64{180,-180}

//
MIN_LATITUDE = -85.05112878

//
MAX_LATITUDE = 85.05112878

//
MIN_LONGITUDE = -180

//
MAX_LONGITUDE = 180


ErrInvalidInput = func (lat, long float64) error {
return fmt.Errorf("invalid positions provided (lat: %.4f, long: %.4f)", lat, long)
}

ErrInvalidHash = func (hash string) error {
return fmt.Errorf("invalid positions provided (hash: %s)", hash)
}
)


func Encode(lat, long float64, precision int) (string, error) {

if precision <= 0 {
precision = 12;
}

if long > float64(MAX_LONGITUDE) || long < float64(MIN_LONGITUDE) || lat < MIN_LATITUDE || lat > MAX_LATITUDE {
return "", ErrInvalidInput(lat, long)
}


// convert them to thier binary equivalent.
latBin := getBinaryString(latitudeRange,lat, precision)
longBin := getBinaryString(longitudeRange, long, precision)

fmt.Println("lat: ", latBin)
fmt.Println("long: ", longBin)


return "", nil

}

func Decode(hash string) (Coordinates, error) {

return Coordinates{}, nil
}


func getBinaryString(cRange []int64, target float64, precision int) string {
var stringBuilder = strings.Builder{}

max := float64(cRange[0])
min := float64(cRange[1])

for i := 0; i < precision; i++ {
mid := (float64(max) + float64(min)) / 2

if(target == float64(mid)) {
stringBuilder.WriteString("1")
min = mid;
}else if (target <= float64(mid)) {
stringBuilder.WriteString("0")
max = mid
}else if (target >= float64(mid)) {
stringBuilder.WriteString("1")
min = mid
}
}

return stringBuilder.String()
}
17 changes: 17 additions & 0 deletions internal/eval/geo/geohash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package geo

import "testing"



func TestGeoHash_Encode(t *testing.T) {

}

func TestGeoHash_Decode(t *testing.T) {

}

func TestGeoHash_Neighbors(t *testing.T) {

}

0 comments on commit 6a7be9a

Please sign in to comment.