Travis-CI Build Status AppVeyor Build Status


An R package providing access to the awesome mapshaper tool by Matthew Bloch, which has both a Node.js command-line tool as well as an interactive web tool.

I started this package so that I could use mapshaper's Visvalingam 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:

The package may be (is probably) buggy. If you run into any bugs, please file an issue


rmapshaper is not on CRAN for now, but you can install it with devtools. You will also need at least version (current dev version) of geojsonio, also available from github:

## install.packages("devtools")


rmapshaper works with geojson strings (character objects of class geo_json) and list geojson objects of class geo_list. These classes are defined in the geojsonio package. It also works with Spatial classes from the sp package.

We will use the states dataset from the geojsonio package and first turn it into a geo_json object:

#> We recommend using rgdal v1.1-1 or greater, but we don't require it
#> rgdal::writeOGR in previous versions didn't write
#> multipolygon objects to geojson correctly.
#> See
#> Attaching package: 'geojsonio'
#> The following object is masked from 'package:base':
#>     pretty

## First convert to json
states_json <- geojson_json(states, geometry = "polygon", group = "group")
#> Assuming 'long' and 'lat' are longitude and latitude, respectively

## For ease of illustration via plotting, we will convert to a `SpatialPolygonsDataFrame`:
states_sp <- geojson_sp(states_json)

## Plot the original

## Now simplify using default parameters, then plot the simplified states
states_simp <- ms_simplify(states_sp)

You can see that even at very high levels of simplification, the mapshaper simplification algorithm preserves the topology, including shared boudaries:

states_very_simp <- ms_simplify(states_sp, keep = 0.001)

Compare this to the output using rgeos::gSimplify, where overlaps and gaps are evident:

#> rgeos version: 0.3-19, (SVN revision 524)
#>  GEOS runtime version: 3.5.0-CAPI-1.9.0 r4084 
#>  Linking to sp version: 1.2-3 
#>  Polygon checking: TRUE
states_gsimp <- gSimplify(states_sp, tol = 1, topologyPreserve = TRUE)

All of the functions are quite fast with geo_json character objects and geo_list list objects. They are slower with the Spatial classes due to internal conversion to/from json. If you are going to do multiple operations on large Spatial objects, it's recommended to first convert to json using geojson_list or geojson_json from the geojsonio package. 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 states_sp as created above:


## First convert 'states' dataframe from geojsonio pkg to json
states_json <- geojson_json(states, lat = "lat", lon = "long", group = "group", 
                            geometry = "polygon")

states_json %>% 
  ms_erase(bbox = c(-107, 36, -101, 42)) %>% # Cut a big hole in the middle
  ms_dissolve() %>% # Dissolve state borders
  ms_simplify(keep_shapes = TRUE, explode = TRUE) %>% # Simplify polygon
  geojson_sp() %>% # Convert to SpatialPolygonsDataFrame
  plot(col = "blue") # plot


This package uses the 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 sp and rgdal), the geojsonio package for converting between geo_list, geo_json, and sp objects, and the jsonlite package for converting between json strings and R objects.

Thanks to timelyportfolio for helping me wrangle the javascript to the point where it works in V8. He also wrote the mapshaper htmlwidget, which provides access to the mapshaper web inteface, right in your R session. We have plans to combine the two in the future.