Skip to content
Prev 2205 / 29559 Next

spsample and NA

It is often the case, at least with raster (grid) objects,
that you are not interested on a part of the raster.
For example because it's outside a non.rectangular region of study
and/or because some types of terrain (some classes) are outside
the scope of the study. In any case, those parts can be
represented by NA.

Unfortunately, while some functions like table() would not
include the NA in the statistics, spsample includes
the NA as just another class, which implies that a fraction
of the resulting coordinates will lay on the NA
part of the raster. This makes it difficult, to get N valid (i.e., not 
on NA parts) points using spsample. Is there any way around this?
For example, if I want 10 points on non-NA cells of
raster X, I would like

spsample(X, 10, "stratified")

to provide 10 positions on non-NA cells.

At the moment, I get:

x <- GDAL.open("C:/ALOBO/dipu2006/STLL_VEG2006/ESTRAT3_OBAC.tif")
ESTRAT3OBAC <- asSGDF_GROD(x, output.dim=c(100,100))
GDAL.close(x)

ESTRAT3OBAC at data[ESTRAT3OBAC at data==55537]<- NA

X <- ESTRAT3OBAC
X$cut <- as.ordered(cut(X$band1, c(0,10,100),include.lowest=TRUE))
table(X$cut)

   [0,10] (10,100]
       83     1961

summary(X$cut)
   [0,10] (10,100]     NA's
       83     1961     7956
table(X$cut)

   [0,10] (10,100]
       83     1961

Xspsample <- spsample(X,10,"stratified")
Xo <- overlay(X,Xspsample)
summary(Xo)
Object of class SpatialPointsDataFrame
Coordinates:
          min       max
x1  410995.6  414954.4
x2 4608002.1 4610566.0
Is projected: TRUE
proj4string : [ +proj=utm +zone=31 +ellps=intl +units=m +no_defs]
Number of points: 9
Data attributes:
      band1             cut
  Min.   :   67   [0,10]  :0
  1st Qu.:   97   (10,100]:3
  Median :  159   NA's    :6
  Mean   :24745
  3rd Qu.:55537
  Max.   :55537

That is, I get 6 positions on NA values of X

Thanks!

Agus