2

I need to create a mask grid for spatial interpolation in gstat library. In details, I have different sampling points randomly distributed and I need to create the minimum convex polygon enclosing these points. Then, I have to create a spatial grid that should be cropped by the computed hull just to limit the interpolation to the extent of this polygon. I'd be very grateful if someone could explain me the detailed procedure also providing some examples. Thank you in advance.

4

1 回答 1

2

我自己找到了解决方案。

library(spatstat)
library(sp)
library(plotKML)
library(maptools)

创建随机点

x<-rnorm(100,3)
y<-rnorm(100,3)
plot(x,y)
xy<-cbind(x,y)
xy<-as.data.frame(xy)

将点转换为空间点数据框,然后转换为栅格。

coordinates(xy)=c("x","y")
pnts<-vect2rast(xy)
summary(pnts)

检查汇总单元格大小值并记住它

从点创建凸包。然后,将“owin”对象(凸包类)转换为空间多边形(创建栅格的基本步骤)

conv<-convexhull.xy(x,y)
SpP<-as(conv,  "SpatialPolygons")
plot(SpP)
points(x,y)
attr  =  data.frame(a=1,  b=1)
SrDf  =  SpatialPolygonsDataFrame(SpP,  attr)

将“cell.size”设置为与“summary(pnts)”相同(在本例中设置为 0.085)。

rast <- vect2rast(SrDf,cell.size=0.085)

玩得开心!

plot(rast)
image(rast)
points(x,y)

注意:使用vect2rast,如果没有为“rast”对象设置cell.size,该函数会自动计算最适合的单元格大小,基于un点密度分布。在这种情况下,多边形仅由其顶点定义,因此我们使用为我们想象被多边形包围的点计算的像元大小。

于 2012-10-19T10:14:20.223 回答