将R图中的多边形导出为shapefile

时间:2012-12-18 05:25:44

标签: r export polygon shapefile

我一直在尝试将图表的内容(线条/多边形)导出为我可以在ArcMap中打开的图层/ shapefile。这些是我一直在使用的一些库,

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

我的纬度/经度数据如下所示:

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)

我使用AdehabitatHR包绘制了位置并计算了最小凸多边形(MCP)。

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)

我知道如何将点导出/写入shapefile,我可以在ArcMap或类似软件中打开,

例如:

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")

然而,我还没有找到一个使用多边形o折线对象的好方法。我愿意能够使用MCP作为shapefile导出/写入多边形对象。有什么建议吗?

1 个答案:

答案 0 :(得分:13)

rgdal对于这种事情非常棒。 http://www.gdal.org/拥有您可能希望支持的格式的大部分信息。

在这种情况下,您需要writeOGR功能

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

您可以轻松地使用它来编写任何形状文件(点,多边形等),但它们必须是SpatialxxxDataFrame个对象

coorddf <-  SpatialPointsDataFrame(coord, data = data.frame(dummy = rep(1,nrow(coord@coords))))
writeOGR(coorddf, dsn = '.', layer = 'mypoints', driver = "ESRI Shapefile")