morton: Convert to/from 2D Morton code

Description Usage Arguments Details Value Author(s) Examples

Description

toMorton converts 2D coordinates x, y to Morton code

fromMorton converts from Morton code to 2D x, y coordinates

Usage

1
2

Arguments

x

numeric, non-negative, x coordinate

y

numeric, non-negative, y coordinate

m

morton-encoded integer

Details

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.

Value

toMorton returns a real vector

fromMorton returns a list with components

x

x coordinate

y

y coordinate

Author(s)

Simon Urbanek

Examples

 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)

s-u/morton documentation built on May 28, 2019, 10:48 a.m.