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.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360

1 Answers1

2

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.