Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/joelgombin/concaveman

A very fast 2D concave hull algorithm
https://github.com/joelgombin/concaveman

concave-hull r spatial

Last synced: about 2 months ago
JSON representation

A very fast 2D concave hull algorithm

Awesome Lists containing this project

README

        

---
output:
md_document:
variant: gfm
---

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

## concaveman

[![R build status](https://github.com/joelgombin/concaveman/workflows/R-CMD-check/badge.svg)](https://github.com/joelgombin/concaveman/actions)


A very fast 2D concave hull algorithm [in JavaScript by Vladimir Agafonkin](https://github.com/mapbox/concaveman), wrapped in R (generates a general outline of a point set).

```{r example, warning=FALSE}
library(concaveman)
library(dplyr)
library(purrr)
library(sf)
library(tmap)
data(points)
polygons <- map(unique(points$k),
~ concaveman(points[points$k %in% .,])
) %>%
map2(unique(points$k), ~ mutate(.x, k = .y)) %>%
reduce(rbind)
tm_shape(points) +
tm_dots(col = "k", size = 0.1, legend.show = FALSE) +
tm_shape(polygons) +
tm_fill(col = "k", alpha = 0.5, legend.show = FALSE) +
tm_borders() +
tm_layout(frame = FALSE)

```

### Installation

`concaveman` can be installed from CRAN:

```{r cran, echo = TRUE, eval = FALSE}
install.packages("concaveman")
```

You can also install the dev version from github:

```{r install, echo = TRUE, eval = FALSE}
devtools::install_github("joelgombin/concaveman")
```

### Usage

```{r usage, echo=TRUE}
library(concaveman)
library(dplyr)
library(purrr)
library(sf)
library(tmap)
data(points)
polygons <- concaveman(points)
polygons

polygons2 <- map(unique(points$k),
~ concaveman(points[points$k %in% .,])
) %>%
map2(unique(points$k), ~ mutate(.x, k = .y)) %>%
reduce(rbind)
tm_shape(points) +
tm_dots(col = "k", size = 0.1, legend.show = FALSE) +
tm_shape(polygons2) +
tm_fill(col = "k", alpha = 0.5, legend.show = FALSE) +
tm_borders() +
tm_layout(frame = FALSE)

```

Signature: `concaveman(points, concavity = 2, lengthThreshold = 0)`

- `points` Can be represented as a matrix of coordinates or an `sf` object.
- `concavity` is a relative measure of concavity. 1 results in a relatively detailed shape, Infinity results in a convex hull. You can use values lower than 1, but they can produce pretty crazy shapes.
- `length_threshold`: when a segment length is under this threshold, it stops being considered for further detalization. Higher values result in simpler shapes.

### Algorithm

The algorithm is based on ideas from the paper [A New Concave Hull Algorithm and Concaveness Measure for n-dimensional Datasets, 2012](http://www.iis.sinica.edu.tw/page/jise/2012/201205_10.pdf) by Jin-Seo Park and Se-Jong Oh.

This implementation by Vladimir Agafonkin dramatically improves performance over the one stated in the paper (`O(rn)`, where `r` is a number of output points, to `O(n log n)`) by introducing a fast *k nearest points to a segment* algorithm, a modification of a depth-first kNN R-tree search using a priority queue.