[R-sig-Geo] focalFilter problem raster package

Nick Hamm nick at hamm.org
Sun Apr 3 14:24:43 CEST 2011


Dear all

I am experiencing a strange problem when using the focalFilter
function in the raster package.

Whilst checking some code, I tried it on a toy dataset (p267) and a
simple function.  I have a set of filters in a list (w.ij).  These can
be found at http://dl.dropbox.com/u/15122401/Rproblem/toyexample.zip
or http://dl.dropbox.com/u/15122401/Rproblem/toyexample.Rdata

p267 is a 16x16 matrix.  The filters are all 15x15 binary filters.

I take the filter and move it around the matrix (4 centre points
(8,8), (8,9), (9,8), (9,9)). At each of these points I multiply the
filter by the underlying values in p267.  I then sum the new matrix,
as follows

sum(w.ij[[2]] * p267[-16,-16])
sum(w.ij[[2]] * p267[-16,-1])
sum(w.ij[[2]] * p267[-1,-16])
sum(w.ij[[2]] * p267[-1,-1])

I can repeat this for the other filters in the w.ij list.

I then try to do this using raster (eventualy I want to apply such
functions to a raster image not a small matrix), so I try:


p267.r <- raster(p267)

for(i in 1:5){
	tmp	<- focalFilter(p267.r, filter=w.ij[[i]], fun=sum)
	print(tmp at data@values[which(tmp at data@values != "NA")])
}


As you can see, the values obtained from the focalFilter function do
not match my test.  I cannot see why this should be.  Can anybody
help?

Nick





---
> sessionInfo()
R version 2.12.2 (2011-02-25)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United
Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] raster_1.8-3 sp_0.9-79

loaded via a namespace (and not attached):
[1] grid_2.12.2     lattice_0.19-17 tools_2.12.2
>
----



More information about the R-sig-Geo mailing list