从点创建凸包多边形并保存为shapefile。

24

我需要帮忙解决R语言中的转换问题。

我已经计算出了一个点云的凸包(convex hull)。我想利用形成凸包的点构建一个多边形对象(polygon object),并将其保存为可被GIS软件(如ArcMap等)读取的shapefile

我的代码如下:

gps <- read.csv(f)  ##reads the lat-long coordinates  file 
x <- gps$LONGITUDE  ##tells R which columns is which
y <- gps$LATITUDE
z<-chull(x,y)       ##calculates the convex hull --this is just a list of x-y points, N vertex 
dfHull <-cbind(x[z],y[z])  ##the convex hull expressed as a list of selected x-y points
plot(dfHull)     ##this plots the vertex of the polygon, just a check
lines(dfhull)    ##plots the polygon in screen

##generate polygon shapefile, from dfHull, and save it externally as a shapefile ???

源文件仅包含纬度-经度坐标,例如:

52.73336     N  0.365974
52.7332  N  0.366051
52.73289     N  0.36636
52.73297     N  0.366258
52.73298     N  0.366243
52.733   N  0.366112
52.73308     N  0.365942
52.73317     N  0.365881
52.73321     N  0.36593
52.73328     N  0.365942
52.73352     N  0.36579
52.73362     N  0.365678
52.73391     N  0.365536
52.7373  N  0.36543
52.73289     N  0.36728

我知道有一些包(rgdal、maptools等)可以帮助做这些事情,但我对空间相关的东西非常不熟悉。实际上,我只需要生成多边形对象并将其保存为shapefile。

非常感谢任何帮助。先谢谢了,dev。


1
shapefiles包提供了一个简单的示例,展示如何将简单的R对象写入shapefile。 - nicola
1个回答

32

这里是一个简单的示例,用于创建SpatialPolygonsDataFrame,可以使用rgdal::writeOGR()将其保存为shapefile格式:

set.seed(1)
dat <- matrix(stats::rnorm(2000), ncol = 2)
ch <- chull(dat)
coords <- dat[c(ch, ch[1]), ]  # closed polygon

plot(dat, pch=19)
lines(coords, col="red")

凸包

library("sp")
library("rgdal")

sp_poly <- SpatialPolygons(list(Polygons(list(Polygon(coords)), ID=1)))
# set coordinate reference system with SpatialPolygons(..., proj4string=CRS(...))
# e.g. CRS("+proj=longlat +datum=WGS84")
sp_poly_df <- SpatialPolygonsDataFrame(sp_poly, data=data.frame(ID=1))
writeOGR(sp_poly_df, "chull", layer="chull", driver="ESRI Shapefile")

还需要测试,但看起来完美解决了我的问题。非常感谢! - user3310782
嗨,rcs, 有一个快速的问题。我收到了“错误:找不到函数”writeOGR“”。这是因为您需要在计算机上安装ArcGIS吗?第二个问题:形状文件是否仅包括外壳点、多边形形状还是全部? - user3310782
writeOGR在rgdal包中,您需要安装和加载此包(不需要ArcGIS)。 shp文件包含由外壳点定义的多边形。 - rcs
我尝试在南极周围使用点,但它不起作用... 我认为在执行chull之前需要处理投影。 - J. Win.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接