Skip to content

Instantly share code, notes, and snippets.

@fuyuntt
Created August 2, 2017 09:38
Show Gist options
  • Save fuyuntt/4bffa4c438219ba62de4e7e814e08609 to your computer and use it in GitHub Desktop.
Save fuyuntt/4bffa4c438219ba62de4e7e814e08609 to your computer and use it in GitHub Desktop.
google-s2 improve
import com.google.common.geometry.{MutableInteger, S2CellId}
/**
* Created by fuyun on 2017/7/25.
*/
object ImproveS2 {
val level = 17
// at level 17
def cellIj(cell: S2CellId): (Int, Int) = {
val i = new MutableInteger(0)
val j = new MutableInteger(0)
val face = cell.toFaceIJOrientation(i, j, null)
val shift = 30 - level
(i.intValue() >> shift, (j.intValue() >> shift) + ((face - 1) << level))
}
def fromIj(i: Int, j: Int): S2CellId = {
val shift = 30 - level
val face = (j >> level) + 1
S2CellId.fromFaceIJ(face, i << shift, (j << shift) << 1 >> 1).parent(level)
}
def id2QuadKey(cellId: S2CellId): String = {
val buf = new Array[Char](cellId.level() + 2)
buf(0) = (cellId.face() + '0').toChar
buf(1) = '/'
val mask = 0x03
val id = cellId.id()
for (i <- 1 to cellId.level()) {
buf(i + 1) = (((id >>> (64 - (i * 2 + 3))) & mask) + '0').toChar
}
new String(buf)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment