Skip to content

Commit

Permalink
Add DecodeCenter function (#11)
Browse files Browse the repository at this point in the history
Solves issue #10 by adding DecodeCenter function.
  • Loading branch information
dc0d authored and mmcloughlin committed Jun 25, 2018
1 parent f062401 commit 3b756d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions extensive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ func TestDecode(t *testing.T) {
}
}

// Test decoding at various precisions.
func TestDecodeCenter(t *testing.T) {
for _, c := range decodecases {
lat, lng := DecodeCenter(c.hash)
if !c.box.Contains(lat, lng) {
t.Errorf("hash %s decoded to %f,%f should lie in %+v",
c.hash, lat, lng, c.box)
}
}
}

// Test roundtrip decoding then encoding again.
func TestDecodeThenEncode(t *testing.T) {
for _, c := range decodecases {
Expand Down
6 changes: 6 additions & 0 deletions geohash.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ func Decode(hash string) (lat, lng float64) {
return box.Round()
}

// DecodeCenter decodes the string geohash to the central point of the bounding box.
func DecodeCenter(hash string) (lat, lng float64) {
box := BoundingBox(hash)
return box.Center()
}

// DecodeIntWithPrecision decodes the provided integer geohash with bits of
// precision to a (lat, lng) point.
func DecodeIntWithPrecision(hash uint64, bits uint) (lat, lng float64) {
Expand Down

0 comments on commit 3b756d8

Please sign in to comment.