Creating and cropping grid in R (CRAN)

蹲街弑〆低调 提交于 2020-01-14 03:42:07

问题


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.


回答1:


I found the solution by my own.

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

create random points

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

convert points to spatial point data frame and, then, to raster.

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

Check summary cell size value and remember it

Create convex hull from points. Then, convert "owin" object (the class of the convex hull) to spatial polygons (funtamental step for raster creation)

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)

Set the "cell.size" as the same of the "summary(pnts)" (in this case is set to 0.085).

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

have fun!

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

Note: With vect2rast, if the cell.size is not set for "rast" object, the function automatically calculates the best suited cell size based un points density distribution. In this case the polygon is defined by only its vertices, so we use the cell size computed for the points that we imagine are enclosed by the polygon.



来源:https://stackoverflow.com/questions/12938629/creating-and-cropping-grid-in-r-cran

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!