rmapshaper

An R wrapper for the mapshaper javascript library

https://github.com/ateucher/rmapshaper

Science Score: 23.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    1 of 9 committers (11.1%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (17.7%) to scientific vocabulary

Keywords

r rlang rstats

Keywords from Contributors

geos gdal proj book rspatial data-manipulation grammar
Last synced: 9 months ago · JSON representation

Repository

An R wrapper for the mapshaper javascript library

Basic Info
Statistics
  • Stars: 208
  • Watchers: 7
  • Forks: 13
  • Open Issues: 24
  • Releases: 11
Topics
r rlang rstats
Created almost 11 years ago · Last pushed about 1 year ago
Metadata Files
Readme Changelog License

README.Rmd

---
output:
  github_document:
    html_preview: true
---



```{r, echo = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/"
)
options(warnPartialMatchArgs = FALSE)
```


[![Codecov test coverage](https://codecov.io/gh/ateucher/rmapshaper/branch/master/graph/badge.svg)](https://app.codecov.io/gh/ateucher/rmapshaper?branch=master)
[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/rmapshaper)](https://cran.r-project.org/package=rmapshaper)
[![CRAN Downloads per month](http://cranlogs.r-pkg.org/badges/rmapshaper)](https://cran.r-project.org/package=rmapshaper)
[![CRAN total downloads](http://cranlogs.r-pkg.org/badges/grand-total/rmapshaper?color=lightgrey)](https://cran.r-project.org/package=rmapshaper)
[![R-CMD-check](https://github.com/ateucher/rmapshaper/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ateucher/rmapshaper/actions/workflows/R-CMD-check.yaml)


# rmapshaper rmapshaper website

An R package providing access to the awesome [mapshaper](https://github.com/mbloch/mapshaper/) tool by Matthew Bloch, which has both a [Node.js command-line tool](https://github.com/mbloch/mapshaper/wiki/Introduction-to-the-Command-Line-Tool) as well as an [interactive web tool](https://mapshaper.org/).

I started this package so that I could use mapshaper's [Visvalingam](https://bost.ocks.org/mike/simplify/) simplification method in R. There is, as far as I know, no other R package that performs topologically-aware multi-polygon simplification. (This means that shared boundaries between adjacent polygons are always kept intact, with no gaps or overlaps, even at high levels of simplification).

But mapshaper does much more than simplification, so I am working on wrapping most of the core functionality of mapshaper into R functions.

So far, `rmapshaper` provides the following functions:

- `ms_simplify` - simplify polygons or lines
- `ms_clip` - clip an area out of a layer using a polygon layer or a bounding box. Works on polygons, lines, and points
- `ms_erase` - erase an area from a layer using a polygon layer or a bounding box. Works on polygons, lines, and points
- `ms_dissolve` - aggregate polygon features, optionally specifying a field to aggregate on. If no field is specified, will merge all polygons into one.
- `ms_explode` - convert multipart shapes to single part. Works with polygons, lines, and points in geojson format, but currently only with polygons and lines in the `Spatial` classes (not `SpatialMultiPoints` and `SpatialMultiPointsDataFrame`).
- `ms_lines` - convert polygons to topological boundaries (lines)
- `ms_innerlines` - convert polygons to shared inner boundaries (lines)
- `ms_points` - create points from a polygon layer
- `ms_filter_fields` - Remove fields from the attributes
- `ms_filter_islands` - Remove small detached polygons

If you run into any bugs or have any feature requests, please file an [issue](https://github.com/ateucher/rmapshaper/issues/)

### Installation

`rmapshaper` is on CRAN. Install the current version with:

```r
install.packages("rmapshaper")
```

You can install the development version from github with `remotes`:

```r
## install.packages("remotes")
library(remotes)
install_github("ateucher/rmapshaper")
```

### Usage

rmapshaper works with `sf` objects as well as geojson strings (character objects of class `geo_json`). It also works with `Spatial` classes from the `sp` package, though this will likely be retired in the future; users are encouraged to use the more modern `sf` package.

We will use the `nc.gpkg` file (North Carolina county boundaries)
from the `sf` package and read it in as an `sf` object:

```{r}
library(rmapshaper)
library(sf)

file <- system.file("gpkg/nc.gpkg", package = "sf")
nc_sf <- read_sf(file)
```

Plot the original: 

```{r}
plot(nc_sf["FIPS"])
```

Now simplify using default parameters, then plot the simplified North Carolina counties:
```{r}
nc_simp <- ms_simplify(nc_sf)
plot(nc_simp["FIPS"])
```

You can see that even at very high levels of simplification, the mapshaper
simplification algorithm preserves the topology, including shared boundaries. The `keep` 
parameter specifies what proportion of vertices to keep:

```{r}
nc_very_simp <- ms_simplify(nc_sf, keep = 0.001)
plot(nc_very_simp["FIPS"])
```

Compare this to the output using `sf::st_simplify`, where overlaps and gaps are evident:

```{r}

nc_stsimp <- st_simplify(nc_sf, preserveTopology = TRUE, dTolerance = 10000) # dTolerance specified in meters
plot(nc_stsimp["FIPS"])
```

This time we'll demonstrate the `ms_innerlines` function:

```{r}
nc_sf_innerlines <- ms_innerlines(nc_sf)
plot(nc_sf_innerlines)
```

All of the functions are quite fast with `geojson` character objects. They are slower with the `sf` and 
`Spatial` classes due to internal conversion to/from json. If you are going to do multiple 
operations on large `sf` objects, 
it's recommended to first convert to json using `geojsonsf::sf_geojson()`, or `geojsonio::geojson_json()`. 
All of the functions have the input object as the first argument, 
and return the same class of object as the input. As such, they can be chained together. 
For a totally contrived example, using `nc_sf` as created above:

```{r eval=rmapshaper:::v8_version() >= '6'}
library(geojsonsf)
library(rmapshaper)
library(sf)

## First convert 'states' dataframe from geojsonsf pkg to json

nc_sf %>% 
  sf_geojson() |> 
  ms_erase(bbox = c(-80, 35, -79, 35.5)) |>  # Cut a big hole in the middle
  ms_dissolve() |>  # Dissolve county borders
  ms_simplify(keep_shapes = TRUE, explode = TRUE) |> # Simplify polygon
  geojson_sf() |> # Convert to sf object
  plot(col = "blue") # plot
```

### Using the system mapshaper

Sometimes if you are dealing with a very large spatial object in R, `rmapshaper`
functions will take a very long time or not work at all. As of version `0.4.0`, 
you can make use of the system `mapshaper` library if you have it installed.
This will allow you to work with very large spatial objects.

First make sure you have mapshaper installed:

```{r eval=nzchar(Sys.which("mapshaper"))}
check_sys_mapshaper()
```

If you get an error, you will need to install mapshaper. First install node 
(https://nodejs.org/en) and then install mapshaper in a command prompt with:

```
$ npm install -g mapshaper
```


Then you can use the `sys` argument in any rmapshaper function:

```{r eval=nzchar(Sys.which("mapshaper"))}
nc_simp_internal <- ms_simplify(nc_sf)
nc_simp_sys <- ms_simplify(nc_sf, sys = TRUE, sys_mem=8) #sys_mem specifies the amount of memory to use in Gb.  It defaults to 8 if omitted. 

par(mfrow = c(1,2))
plot(st_geometry(nc_simp_internal), main = "internal")
plot(st_geometry(nc_simp_sys), main = "system")
```


### Thanks

This package uses the [V8](https://cran.r-project.org/package=V8)
package to provide an environment in which to run mapshaper’s javascript
code in R. It relies heavily on all of the great spatial packages that
already exist (especially `sf`), and the `geojsonio` and the `geojsonsf` packages for
converting between `geojson`, `sf` and `Spatial`
object.

Thanks to [timelyportfolio](https://github.com/timelyportfolio) for helping me wrangle the javascript to the point where it works in V8. He also wrote the [mapshaper htmlwidget](https://github.com/timelyportfolio/mapshaper_htmlwidget), which provides access to the mapshaper web interface, right in your R session. We have plans to combine the two in the future.

### Code of Conduct

Please note that this project is released with a [Contributor Code of Conduct](https://github.com/ateucher/rmapshaper/blob/master/CONDUCT.md). By participating in this project you agree to abide by its terms.

### LICENSE

MIT

Owner

  • Name: Andy Teucher
  • Login: ateucher
  • Kind: user
  • Location: Victoria, BC, Canada

R developer, educator, and data scientist

GitHub Events

Total
  • Watch event: 6
  • Issue comment event: 10
  • Push event: 1
  • Pull request event: 1
  • Fork event: 1
Last Year
  • Watch event: 6
  • Issue comment event: 10
  • Push event: 1
  • Pull request event: 1
  • Fork event: 1

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 737
  • Total Committers: 9
  • Avg Commits per committer: 81.889
  • Development Distribution Score (DDS): 0.016
Past Year
  • Commits: 3
  • Committers: 2
  • Avg Commits per committer: 1.5
  • Development Distribution Score (DDS): 0.333
Top Committers
Name Email Commits
Andy Teucher a****r@g****m 725
your name e****n@g****m 2
Nikolai B n****b 2
Michael Sumner m****r@g****m 2
David Blodgett d****t@u****v 2
timelyportfolio k****l@t****m 1
baldeagle a****n@g****m 1
Robin R****e 1
Jakub Nowosad N****d 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 95
  • Total pull requests: 24
  • Average time to close issues: 10 months
  • Average time to close pull requests: 23 days
  • Total issue authors: 44
  • Total pull request authors: 9
  • Average comments per issue: 3.21
  • Average comments per pull request: 1.75
  • Merged pull requests: 20
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 2
  • Pull requests: 2
  • Average time to close issues: 1 day
  • Average time to close pull requests: about 19 hours
  • Issue authors: 2
  • Pull request authors: 2
  • Average comments per issue: 5.0
  • Average comments per pull request: 2.5
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • ateucher (43)
  • rsbivand (4)
  • mdsumner (3)
  • Nowosad (3)
  • Robinlovelace (2)
  • dblodgett-usgs (2)
  • ericemc3 (1)
  • fm429 (1)
  • butellyn (1)
  • abasees (1)
  • gittybobomber (1)
  • JWusagi (1)
  • rafapereirabr (1)
  • sandra-ab (1)
  • al-obrien (1)
Pull Request Authors
  • ateucher (11)
  • dblodgett-usgs (3)
  • mdsumner (2)
  • baldeagle (2)
  • benjazehr (2)
  • Nowosad (2)
  • elipousson (2)
  • atsyplenkov (1)
  • Robinlovelace (1)
Top Labels
Issue Labels
bug (12) enhancement (7) idea (3) documentation (1) wontfix (1)
Pull Request Labels

Packages

  • Total packages: 3
  • Total downloads:
    • cran 4,495 last-month
  • Total docker downloads: 90,219
  • Total dependent packages: 24
    (may contain duplicates)
  • Total dependent repositories: 68
    (may contain duplicates)
  • Total versions: 28
  • Total maintainers: 1
proxy.golang.org: github.com/ateucher/rmapshaper
  • Versions: 11
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.5%
Average: 5.7%
Dependent repos count: 5.9%
Last synced: 9 months ago
cran.r-project.org: rmapshaper

Client for 'mapshaper' for 'Geospatial' Operations

  • Versions: 11
  • Dependent Packages: 24
  • Dependent Repositories: 67
  • Downloads: 4,495 Last month
  • Docker Downloads: 90,219
Rankings
Stargazers count: 2.2%
Dependent repos count: 3.0%
Dependent packages count: 3.1%
Downloads: 5.6%
Forks count: 6.8%
Average: 7.8%
Docker downloads count: 26.1%
Maintainers (1)
Last synced: 9 months ago
conda-forge.org: r-rmapshaper
  • Versions: 6
  • Dependent Packages: 0
  • Dependent Repositories: 1
Rankings
Dependent repos count: 24.4%
Stargazers count: 27.6%
Average: 37.2%
Forks count: 45.0%
Dependent packages count: 51.6%
Last synced: 9 months ago

Dependencies

DESCRIPTION cran
  • V8 >= 3.4.2 imports
  • geojsonio >= 0.9.4 imports
  • geojsonlint >= 0.4.0 imports
  • jsonlite >= 1.7.0 imports
  • methods * imports
  • readr >= 1.4.0 imports
  • sf >= 0.9 imports
  • sp >= 1.4 imports
  • covr * suggests
  • knitr * suggests
  • magrittr * suggests
  • rgdal * suggests
  • rgeos * suggests
  • rmarkdown * suggests
  • testthat >= 2.1.0 suggests
  • units * suggests
.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pkgdown.yaml actions
  • JamesIves/github-pages-deploy-action v4.4.1 composite
  • actions/checkout v3 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • actions/upload-artifact v3 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
scratch/docker/fedora-test/Dockerfile docker
  • rhub/fedora-gcc latest build
scratch/docker/linux-check/Dockerfile docker
  • jakubnowosad/rspatial_proj7 latest build