constructMask: Constructs a mask for a grid

View source: R/mask.R

constructMaskR Documentation

Constructs a mask for a grid

Description

Constructs a mask for a grid

Usage

constructMask(grid, method = "maxdist", maxval = NULL, x = NULL)

Arguments

grid

a grid, see details for more info

method

which construction method? currently one of 'maxdist', 'sillprop' or 'point2polygon'

maxval

for maxdist and sillprop methods, maximum reference value

x

extra information for the grid construction, see details

Details

Method 'maxdist' defines the mask as all points within a maximum distance (must be given in maxval) from the reference data (given in x: this is expected to be the original complete data, with coordinates and variables). For method 'sillprop' the mask is defined by those points which total kriging variance is below a fixed proportion (given in maxval, default=0.99) of the total variogram model sill (variogram model given in x, of class "variogramModelList"). In this method, the argument grid is expected to be the output of a cokriging analysis. Finally, method 'point2poly' created the mask by taking the points internal to a "SpatialPolygon" object (given in x).

Value

a logical vector with as many elements as points in the grid, with TRUE for those points within the mask, and FALSE for those outside the mask.

See Also

Other masking functions: getMask(), print.mask(), setMask(), unmask()

Examples

## with data.frame
x = 1:23
y = 1:29
xy = expand.grid(x=x, y=y)
xyz.df = data.frame(xy, z = rnorm(29*23)*ifelse(abs(xy$x-xy$y)<3, 1, NA)+(xy$x+xy$y)/2)
mask.df = constructMask(grid = xy, method = "maxdist", maxval = 3, x=xyz.df)
image(mask.df)
oldpar = par(mfrow = c(1,1))
mask.df
xyz.df.masked = setMask(xyz.df, mask.df)
dim(xyz.df.masked)
summary(xyz.df.masked)
xyz.df.unmasked = unmask(xyz.df.masked)
dim(xyz.df.unmasked)
length(x)*length(y)
summary(xyz.df.unmasked)
## with SpatialGrid
library(sp)
library(magrittr)
xy.sp = sp::SpatialPoints(coords = xy)
meandiff = function(x) mean(diff(x))
xy.gt = GridTopology(c(min(x),min(y)), c(meandiff(x), meandiff(y)), c(length(x),length(y)))
aux = sp::SpatialPixelsDataFrame(grid = xy.gt, data=xyz.df, points = xy.sp)
xyz.sgdf = as(aux, "SpatialGridDataFrame")
image_cokriged(xyz.sgdf, ivar="z")
## reorder the data in the grid and plot again
par(mfrow=c(1,1))
ms = function(x) sortDataInGrid(x, grid=xy.gt)
mask.gt = constructMask(grid = xy.gt, method = "maxdist", maxval = 3, x=xyz.sgdf)
image(x,y,matrix(ms(xyz.sgdf@data$z), nrow=23, ncol=29))  
image(x,y,matrix(ms(mask.gt), nrow=23, ncol=29))  
image(mask.gt)
## work with the mask and plot again
par(mfrow=c(1,1))
xyz.sgdf.masked = setMask(x = xyz.sgdf, mask = mask.gt)
getMask(xyz.sgdf.masked)
image(x,y,matrix(ms(xyz.sgdf@data$z), nrow=23, ncol=29))  
points(xyz.sgdf.masked@coords)
par(oldpar)

gmGeostats documentation built on April 18, 2023, 5:08 p.m.