# focalFilter problem raster package

5 messages
Open this post in threaded view
|

## focalFilter problem raster package

 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.zipor http://dl.dropbox.com/u/15122401/Rproblem/toyexample.Rdatap267 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@data@values[which(tmp@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 > ---- _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Open this post in threaded view
|

## Re: focalFilter problem raster package

 > I am experiencing a strange problem when using the focalFilter > function in the raster package. Nick, Could you provide a self-contained example that does not require downloading files? Given your description that should not be very hard. Robert
Open this post in threaded view
|

## Re: focalFilter problem raster package

 Dear Robert I hope the following example is clear.  you should be able to cut and paste... Nick library(raster) tmp <- c(55, 56, 54, 58, 65, 75, 82, 77, 74, 74, 69, 61, 62, 71, 73, 63, 62, 63, 64, 59, 85, 88, 95, 106, 110, 99, 89, 82, 79, 84, 97, 79, 55, 55, 56, 60, 91, 95, 86, 98, 115, 105, 110, 107, 101, 89, 85, 68, 55, 54, 53, 61, 82, 102, 88, 93, 96, 94, 110, 114, 109, 103, 92, 68, 59, 58, 60, 64, 88, 99, 82, 81, 71, 80, 89, 89, 89, 102, 104, 75, 63, 57, 58, 58, 77, 92, 82, 71, 59, 90, 105, 92, 79, 98, 110, 83, 62, 55, 56, 56, 80, 90, 99, 88, 64, 91, 112, 94, 76, 91, 100, 85, 62, 59, 55, 61, 99, 97, 93, 80, 65, 87, 107, 80, 59, 60, 67, 66, 65, 62, 68, 72, 102, 94, 90, 83, 74, 81, 96, 69, 52, 50, 51, 54, 62, 62, 86, 85, 55, 59, 64, 72, 75, 70, 70, 62, 66, 61, 55, 57, 52, 59, 61, 56, 41, 40, 43, 44, 46, 48, 50, 52, 68, 69, 60, 61, 42, 43, 44, 43, 42, 41, 42, 44, 43, 43, 44, 47, 58, 59, 55, 61, 44, 41, 39, 42, 44, 43, 42, 42, 42, 43, 43, 49, 56, 53, 53, 61, 43, 42, 40, 42, 42, 42, 41, 42, 42, 43, 42, 53, 66, 61, 51, 62, 40, 42, 41, 40, 43, 49, 46, 42, 42, 43, 43, 49, 59, 62, 53, 62, 40, 41, 42, 43, 49, 54, 47, 44, 42, 44, 43, 46, 52, 56, 56, 61) tmp <- matrix(tmp, ncol=16, nrow=16, byrow=T) p267 <- tmp rm(tmp) p267.r <- raster(p267) w.ij.2 <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) w.ij.2 <- matrix(w.ij.2, 15, 15) sum(w.ij.2 * p267[-16,-16]) sum(w.ij.2 * p267[-16,-1]) sum(w.ij.2 * p267[-1,-16]) #Cell 136 in the raster sum(w.ij.2 * p267[-1,-1]) #Cell 137 in the raster tmp <- focalFilter(p267.r, filter=w.ij.2, fun=sum) tmp[136] tmp[137] On 5 April 2011 07:23, Robert Hijmans <[hidden email]> wrote: > >> I am experiencing a strange problem when using the focalFilter >> function in the raster package. > > Nick, > Could you provide a self-contained example that does not require downloading > files? Given your description that should not be very hard. > Robert > > -- > View this message in context: http://r-sig-geo.2731867.n2.nabble.com/focalFilter-problem-raster-package-tp6235925p6241121.html> Sent from the R-sig-geo mailing list archive at Nabble.com. > > _______________________________________________ > R-sig-Geo mailing list > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-geo> _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo