Skip to content

Instantly share code, notes, and snippets.

@rmnppt
Created September 9, 2016 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmnppt/69df10e7356b3e8229a79fcb79ff401d to your computer and use it in GitHub Desktop.
Save rmnppt/69df10e7356b3e8229a79fcb79ff401d to your computer and use it in GitHub Desktop.
Script that takes a .shp file as an input and outputs a dataframe with the polygon information.
# process shapefile data
library(rgdal) # requires sp, will use proj.4 if installed
library(gpclib)
library(maptools)
library(ggplot2)
library(plyr)
gpclibPermit()
shpToDf <- function(directory, layer) {
map <- readOGR(dsn=directory, layer=layer)
map@data$id <- rownames(map@data)
map.points <- fortify(map, region = "id")
map.df <- join(map.points, map@data, by = "id")
}
df <- shpToDf("[Directory]", "[Filename without .shp suffix]")
saveRDS(df, "map_shape.rds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment