Export a polygon from an R plot as a shapefile

20,020

rgdal is superb for this kind of thing. http://www.gdal.org/ has most of the information you could every want on what formats are supported.

In this case you want the writeOGR function

# this will create a shapefile called poly within the working directory
library(rgdal)
writeOGR(poly, dsn = '.', layer = 'poly', driver = "ESRI Shapefile")

You could just as easily use it to write any shape file (point, polygon etc) but they must be SpatialxxxDataFrame objects

coorddf <-  SpatialPointsDataFrame(coord, data = data.frame(dummy = rep(1,nrow(coord@coords))))
writeOGR(coorddf, dsn = '.', layer = 'mypoints', driver = "ESRI Shapefile")
Share:
20,020
user1626688
Author by

user1626688

Updated on July 15, 2022

Comments

  • user1626688
    user1626688 almost 2 years

    I have been trying to export the content of a plot (lines/polygons) as a layer/shapefile that I can open in ArcMap. These are some of the libraries that I have been using,

    library(shapefiles)
    library(PBSmapping)
    library(adehabitatHR)
    library(maptools)
    library(maps)
    library(rgdal)
    library(igraph)
    

    My lat/long data looks like this:

    tagdata<-read.table(text="meanlat  meanlong
    -18.63327 147.0248
    -18.6368  147.0238
    -18.62068 147.294
    -18.62953 147.2942
    -18.62953 147.2942
    -18.62091 147.2938
    -18.62953 147.2942
    -18.62466 147.2926
    -18.73393 147.2816
    -18.73393 147.2816
    -18.75383 147.2541
    -18.75383 147.2541
    -18.75383 147.2541
    -18.75383 147.2541
    -18.6368  147.0238
    -18.63063 147.0256
    -18.63063 147.0256
    -18.68133 147.1164
    -18.6368  147.0238
    -18.63063 147.0256
    -18.63063 147.0256
    -18.75383 147.2541
    -18.61273 147.0682
    -18.69655 147.09
    -18.6368  147.0238
    -18.63063 147.0256
    -18.63063 147.0256
    -18.63217 147.0251
    -18.75383 147.2541
    -18.75383 147.2541
    -18.75383 147.2541
    -18.63063 147.0256
    -18.68133 147.1164
    -18.68133 147.1164
    -18.63217 147.0251
    -18.69922 147.0909
    -18.73393 147.2816
    -18.63632 147.0792
    -18.69522 147.0896
    -18.6368  147.0238
    -18.75383 147.2541
    -18.75383 147.2541
    -18.75383 147.2541",header=TRUE)
    

    I plotted the locations and calculated the Minimum Convex Polygon (MCP) using the AdehabitatHR package.

    plot(tagdata$meanlong,tagdata$meanlat, col="red",pch=1)                  
    
    loc<-tagdata[ ,c("meanlong","meanlat")]
    coord<-SpatialPoints(loc)
    poly<-mcp(coord,percent=100)
    plot(poly,add=TRUE)
    

    I know how to export/write points as shapefile that I can open in ArcMap or similar softwares,

    For example:

    loc<-SpatialPoints(loc) # #convert loc to spatial points
    rem<-tagdata[c(-1:-2)] 
    SpatialPointsDataFrame(coords=loc,data=rem) 
    obj<-SpatialPointsDataFrame(coords=loc,data=rem)
    writePointsShape(obj,"myshape.shp")
    

    However, I haven't found a good way to do it with a polygon o polyline object. I would loke to be able to export/write the poly object with the MCP as a shapefile. Any suggestions?

    • mnel
      mnel over 11 years
    • Spacedman
      Spacedman over 11 years
      Obligatory Open Source Evangelism: Why are you using ArcMap? Why not try Quantum GIS? Or if you have further analysis to do, do it all in R!
  • user1626688
    user1626688 over 11 years
    So how can I convert my Ploy<-MCP "object" that I plotted into SpatialxxxDataFrame. I know how to do it with points, but how can I do it with objects that don't really have a clear spatial reference?
  • mnel
    mnel over 11 years
    Add an example of what you mean within your question
  • mnel
    mnel over 11 years
    Your object poly is a SpatialPolygonsDataFrame
  • mnel
    mnel over 11 years
    By SpatialxxxDataFrame i mean SpatialPointsDataFrame , SpatialPolygonsDataFrame or similar
  • user1626688
    user1626688 over 11 years
    Got it! Thanks a lot! I was not sure if I need it an X/Y attribute table link to my polygon to export it as a shapefile.