簡體   English   中英

將 R 圖中的多邊形導出為 shapefile

[英]Export a polygon from an R plot as a shapefile

我一直在嘗試將繪圖(線/多邊形)的內容導出為可以在 ArcMap 中打開的圖層/形狀文件。 這些是我一直在使用的一些庫,

 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)

我知道如何將點導出/寫入可以在 ArcMap 或類似軟件中打開的 shapefile,

例如:

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

但是,我還沒有找到使用多邊形或折線對象的好方法。 我希望能夠使用 MCP 作為 shapefile 導出/寫入多邊形對象。 有什么建議么?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM