An open API service indexing awesome lists of open source software.

https://github.com/JosiahParry/geohash


https://github.com/JosiahParry/geohash

Last synced: 23 days ago
JSON representation

Awesome Lists containing this project

README

        

---
output: github_document
format: gfm
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# geohash

`{geohash}` is an R package to provide geohash encoding and decoding. It is based on the [Rust crate geohash](https://docs.rs/geohash). This package is actively under development.

You can watch me create this package in my [YouTube video](https://youtu.be/yaxfqpECIZ0).

There is a good chance you do not need to use this package. The package `{geohashTools}` is faster in almost every case.

This package serves as an example of how to use R and Rust together.

## Installation

You can install the development version of geohash like so:

``` r
if (!requireNamespace("remotes")) install.packages("remotes")

remotes::install_github("josiahparry/geohash")
```

## Example

Encode x and y coordinates as a geoash using `encode()`

```{r include = FALSE}
set.seed(0)
library(pillar)
library(sf)
```

```{r}
library(geohash)

# create random x & y points
n <- 5
x <- runif(n, -180, 180)
y <- runif(n, -90, 90)

gh <- encode(x, y, 8)
gh
```

The geohashes can be decoded using `decode()` which provides you with their center, and the error in both the x and y directions.

```{r}
decode(gh)
```

Additionally, you can extract the bounding box of the geohashes using `decode_bbox()`. This returns a `bbox` object from the `sf` package.

```{r}
bboxes <- decode_bbox(gh)
bboxes
```

You can use these bounding boxes to create an `sfc` of polygons.

```{r}
do.call(c, lapply(bboxes, sf::st_as_sfc))
```

Alternatively, you can identify the neighboring geohash cells using `neighbor()` and `neighbors()` function. The `neighbor()` function identifies a single neighbor based on a direction whereas `neighbors()` provides all adjacent geohashes.

Here we can find the neighbors to the north and south-east.

```{r}
neighbor(gh, "n")
neighbor(gh, "se")
```

We can also find _all_ adjacent neighbors.

```{r}
neighbors(gh)
```

## Benchmarks

```{r}
n <- 1e6
x <- runif(n, -180, 180)
y <- runif(n, -90, 90)

bench::mark(
geohash = geohash::encode(x, y, 8L),
geohash_par = geohash::encode_par(x, y, 8L),
geohashTools = geohashTools::gh_encode(y, x, 8L),
iterations = 10
)

bench::mark(
geohash = geohash::neighbors(gh),
geohashTools = geohashTools::gh_neighbors(gh),
check = FALSE
)

bench::mark(
geohash = geohash::decode(gh),
geohashTools = geohashTools::gh_decode(gh, TRUE),
check = FALSE
)

```