How to convert .shp file into .csv in R?

maps, r, shapes, spatial

Solution

Once you have loaded your shapefiles into Spatial{Lines/Polygons}DataFrames (classes from the sp-package), you can use the `fortify` generic function to transform them to flat data.frame format. The specific functions for the `fortify` generic are included in the `ggplot2` package, so you'll need to load that first. A code example:

library(ggplot2)
polygon_dataframe = fortify(polygon_spdf)

where `polygon_spdf` is a `SpatialPolygonsDataFrame`. A similar approach works for `SpatialLinesDataFrame`'s.

The difference between my solution and that of @AriBFriedman is that mine includes the `x` and `y` coordinates of the polygons/lines, in addition to the data associated to those polgons/lines. I really like visualising my spatial data with the `ggplot2` package.

Once you have your data in a normal `data.frame` you can simply use `write.csv` to generate a csv file on disk.

Problem

I want to to convert two .shp files into one database that would allow me to draw the maps together. Also, is there a way to convert .shp files into .csv files? I want to be able to personalize and add some data which is easier for me under a .csv format. What I have in mind if to add overlay yield data and precipitation data on the maps. Here are the shapefiles for Morocco, and Western Sahara. Code to plot the two files: ``` # This is code for mapping of CGE_Morocco results # Loading administrative coordinates for Morocco maps library(sp) library(maptools) library(mapdata) # Loading shape files Mor <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp") Sah <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp") # Ploting the maps (raw) png("Morocco.png") Morocco <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp") plot(Morocco) dev.off() png("WesternSahara.png") WesternSahara <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp") plot(WesternSahara) dev.off() ``` After looking into suggestions from @AriBFriedman and @PaulHiemstra and subsequently figuring out how to merge .shp files, I have managed to produce the following map using the following code and data (For .shp data, cf. links above) code: ``` # Merging Mor and Sah .shp files into one .shp file MoroccoData <- rbind(Mor@data,Sah@data) # First, 'stack' the attribute list rows using rbind() MoroccoPolys <- c(Mor@polygons,Sah@polygons) # Next, combine the two polygon lists into a single list using c() summary(MoroccoData) summary(MoroccoPolys) offset <- length(MoroccoPolys) # Next, generate a new polygon ID for the new SpatialPolygonDataFrame object browser() for (i in 1: offset) { sNew = as.character(i) MoroccoPolys[[i]]@ID = sNew } ID <- c(as.character(1:length(MoroccoPolys))) # Create an identical ID field and append it to the merged Data component MoroccoDataWithID <- cbind(ID,MoroccoData) MoroccoPolysSP <- SpatialPolygons(MoroccoPolys,proj4string=CRS(proj4string(Sah))) # Promote the merged list to a SpatialPolygons data object Morocco <- SpatialPolygonsDataFrame(MoroccoPolysSP,data = MoroccoDataWithID,match.ID = FALSE) # Combine the merged Data and Polygon components into a new SpatialPolygonsDataFrame. Morocco@data$id <- rownames(Morocco@data) Morocco.fort <- fortify(Morocco, region='id') Morocco.fort <- Morocco.fort[order(Morocco.fort$order), ] MoroccoMap <- ggplot(data=Morocco.fort, aes(long, lat, group=group)) + geom_polygon(colour='black',fill='white') + theme_bw() ``` Results: New Question: 1- How to eliminate the boundaries data that cuts though the map in half? 2- How to combine different regions within a .shp file? Thanks you all. P.S: the community in stackoverflow.com is wonderful and very helpful, and especially toward beginners like :) Just thought of emphasizing it.

Original source