Description Usage Arguments Details Value Author(s) Examples
toMorton
converts 2D coordinates x, y to Morton code
fromMorton
converts from Morton code to 2D x, y coordinates
1 2 | toMorton(x, y)
fromMorton(m)
|
x |
numeric, non-negative, x coordinate |
y |
numeric, non-negative, y coordinate |
m |
morton-encoded integer |
Two-dimensional Morton code maps 2D coordinates to a one dimension while preserving some locatily of the points. This implementation uses 32-bit integer x, y coordinates and coverts them to 64-bit integer interleaving the bits (with x having the LSB). In R the result is stored in a real vector which is only guaranteed to preserve 52 bits and thus results with more than 26 bits will have small errors in the lower bits when decoded.
toMorton
returns a real vector
fromMorton
returns a list with components
x |
x coordinate |
y |
y coordinate |
Simon Urbanek
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | toMorton(c(0,1,0,1), c(0,0,1,1))
m <- toMorton(123, 456)
fromMorton(m)
## typically, it is used to encode coordinates
## as a short geohash
d <- iencode64(m)
(d)
fromMorton(idecode64(d))
## it is reliable in 26-bit range
x <- floor(runif(1000,, 2^26))
y <- floor(runif(1000,, 2^26))
m <- toMorton(x, y)
r <- fromMorton(m)
stopifnot(x == r$x && y == r$y)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.