https://github.com/mdsumner/simpler
https://github.com/mdsumner/simpler
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mdsumner/simpler
- Owner: mdsumner
- Created: 2018-12-12T01:36:02.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-12T22:47:28.000Z (over 7 years ago)
- Last Synced: 2025-04-12T14:55:51.061Z (about 1 year ago)
- Language: R
- Size: 352 KB
- Stars: 12
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
---
output: github_document
editor_options:
chunk_output_type: console
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# simpler
The goal of simpler is to provide the [Visvalingam algorithm](https://bost.ocks.org/mike/simplify/) to
simplifying paths.
Very much WIP!
TODO:
* distinguish shared boundaries from isolated lines, and drop short one
* provide methods for various formats, especially polygons to shared boundaries
* methods to recompose shared paths as polygons
* avoid recalculating every triangle area every iteration fixed by vectorizing *removal*, rather than tricky in-place re-indexing
Notes:
- calculating triangle area is fast, the slowness comes from iterating over removals of focal points
- how to avoid intersections that might occur (need examples)
## Installation
You can install this **R** package with **remotes** using:
```r
remotes::install_github("mdsumner/simpler")
```
If you don't have the **remotes** package, that can be installed with:
```r
install.packages("remotes")
```
## Example
The USA example, from Natural Earth. See https://bost.ocks.org/mike/simplify/
```{r usa}
library(simpler)
w <- rnaturalearth::ne_countries(scale = 10, country = "United States of America", returnclass = "sf")
x <- w$geometry[[1]][[1]][[1]]
op <- par(mfrow = c(3, 3), mar = rep(0, 4))
plot(x, type = "l", axes = FALSE)
text(-100, 40, sprintf("vertices: %i", nrow(x)))
for (kpc in c(0.4, rep(0.6, 7))) {
x <- simplify_path(x, keep_pc = kpc)
plot(x, type = "l", axes = FALSE)
text(-100, 40, sprintf("vertices: %i", nrow(x)))
}
par(op)
```
Simplify a bunch of shared lines (need better examples).
```{r example}
library(sf)
library(silicate)
get_pts <- function(x, i) {
x$arc_link_vertex %>% dplyr::filter(arc_ == x$object_link_arc$arc_[i]) %>%
dplyr::inner_join(x$vertex, "vertex_") %>% dplyr::select(x_, y_) %>% as.matrix()
}
nc <- read_sf(system.file("gpkg/nc.gpkg", package = "sf"))
nc1 <- nc[c(1, 2, 3, 10, 19, 18, 23, 25, 22, 34, 41, 42, 32, 34, 41, 39, 50, 43, 46, 52), ]
x <- ARC(nc1)
sfx <- vector("list", nrow(x$object_link_arc))
op <- par(mar = rep(0, 4))
#plot(x$vertex$x_, x$vertex$y_, pch = ".", asp = "")
plot(nc1$geom, reset = FALSE)
for (i in seq_along(x$object_link_arc$arc_)) {
pts <- get_pts(x, i)
path <- simplify_path(pts, keep_pc = 0.1)
lines(path, col = rainbow(10)[i %% 10 + 1], lwd = 2)
sfx[[i]] <- sf::st_linestring(path)
}
par(op)
points(x$vertex$x_, x$vertex$y_, pch = 19, cex = 0.3)
x <- ARC(inlandwaters)
plot(x)
sfx <- vector("list", nrow(x$object_link_arc))
plot(x$vertex$x_, x$vertex$y_, pch = ".", asp = 1, xlim = c(4e5, 2e6), ylim = c(-1e6, 0))
for (i in seq_along(x$object_link_arc$arc_)) {
pts <- get_pts(x, i)
path <- simplify_path(pts, keep_pc = 0.001)
lines(path, col = rainbow(10)[i %% 10 + 1], lwd = 3)
sfx[[i]] <- sf::st_linestring(path)
}
## this is a cheat because the polygons have to store any shared boundaries twice,
pryr::object_size(sf::st_sfc(sfx))
pryr::object_size(inlandwaters)
plot(sf::st_sfc(sfx))
```