morton {morton} R Documentation

## Convert to/from 2D Morton code

### Description

`toMorton` converts 2D coordinates x, y to Morton code

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

### Usage

```toMorton(x, y)
fromMorton(m)
```

### 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

Simon Urbanek

### Examples

```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)

```

[Package morton version 0.1-1 Index]