Skip to content

Instantly share code, notes, and snippets.

@marvell
Created August 4, 2015 09:33
Show Gist options
  • Save marvell/726f2509d95830b1a317 to your computer and use it in GitHub Desktop.
Save marvell/726f2509d95830b1a317 to your computer and use it in GitHub Desktop.
Get tile hash by ZXY
func getTileHash(z, x, y int) int64 {
positions := make(map[int][2]int)
for currentZoom := 1; currentZoom <= z; currentZoom++ {
tileSize := int(math.Pow(2, float64(z-currentZoom)))
centerX, centerY := tileSize-1, tileSize-1
if currentZoom > 1 {
n := int(math.Pow(2, float64(currentZoom-1)))
centerX += positions[currentZoom-1][0] * n
centerY += positions[currentZoom-1][1] * n
}
positionX, positionY := 0, 0
if x > centerX {
positionX = 1
}
if y > centerY {
positionY = 1
}
positions[currentZoom] = [2]int{positionX, positionY}
}
var hash int64
for currentZoom, p := range positions {
hash += int64(int(math.Pow10(z-currentZoom)) * (2*p[1] + p[0] + 1))
}
return hash
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment