-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode_bound_box.go
63 lines (52 loc) · 1.29 KB
/
decode_bound_box.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package ggeohash
import "strings"
import "fmt"
//
// Bounded box for parsing latitude and longitude
//
type DecodedBoundBox struct {
Min, Max *PreciseLocation
}
func MakeDecodedBoundBox() *DecodedBoundBox {
output := &DecodedBoundBox{}
// -90, -180
output.Min = MakePreciseLocationLowerBound()
// +90, +180
output.Max = MakePreciseLocationUpperBound()
return output
}
func DecodeBoundBox(hash_string string) *DecodedBoundBox {
// Downcase the input string
hash_string = strings.ToLower(hash_string)
output := MakeDecodedBoundBox()
var islon bool = true
for i, c := range hash_string {
byte_at := byte(c)
index_at := convertByteToIndex(byte_at)
if index_at < 0 {
msg := fmt.Sprintf("[DecodeBoundBox] Unknown byte at index=%d, rune='%v' in string='%v'", i, c, hash_string)
panic(msg)
}
index_of := uint8(index_at)
for bits := 4; bits >= 0; bits-- {
bit := (index_of >> uint8(bits)) & 1
if islon {
mid := (output.Max.Longitude + output.Min.Longitude) / 2
if bit == 1 {
output.Min.Longitude = mid
} else {
output.Max.Longitude = mid
}
} else {
mid := (output.Max.Latitude + output.Min.Latitude) / 2
if bit == 1 {
output.Min.Latitude = mid
} else {
output.Max.Latitude = mid
}
}
islon = !islon
}
}
return output
}