Created
September 9, 2016 08:31
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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