Skip to content

Instantly share code, notes, and snippets.

@Robinlovelace
Created January 7, 2016 16:58
Show Gist options
  • Save Robinlovelace/ad5f28d7b32cf08a0d49 to your computer and use it in GitHub Desktop.
Save Robinlovelace/ad5f28d7b32cf08a0d49 to your computer and use it in GitHub Desktop.
Demonstrates the use of mapshaper called from R to simplify spatial objects: https://github.com/ropensci/stplanr
# # # # # # # # # # # # # # # # # # # #
# Simplify sp objects w. mapshaper #
# Needs https://nodejs.org installed #
# Tested on Windows and Linux #
# # # # # # # # # # # # # # # # # # # #
# # # # # #
# R setup #
# # # # # #
if(!require(devtools)) install.packages("devtools") # installs devtools if needs be
devtools::install_github("ropensci/stplanr") # install dev. version of stplanr
library(stplanr) # load the stplanr library
mapshape_available() # is mapshaper available to system()?
# # # # # # # # # # #
# data to simplify #
# # # # # # # # # # #
rnet <- overline(routes_fast)
rnet <- overline(routes_fast, attrib = "All")
# # # # # # #
# simplify #
# # # # # # #
rnet5 <- mapshape(rnet, percent = 5)
rnet1 <- mapshape(rnet, percent = 1)
rnets <- mapshape(rnet, ms_options = "snap-interval=0.001")
# # # # #
# plot #
# # # # #
par(mfrow = c(2,2))
plot(rnet, main = "No simplification")
plot(rnet5, main = "5% simplification")
plot(rnet1, main = "1% simplification")
plot(rnets, main = "snap interval simplification")
@timelyportfolio
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment