Hi all,
I'm a bit baffled by this - perhaps I'm just being really dumb but I
cant understand the following output & have been staring at it for too
long. I'm using the focalFilter function in the raster package to
process a raster DEM. I've cropped the data back to a tiny area to make
it easier to see. I am expecting focalFilter to apply a matrix of
weights (in this case 1s in a 5x5 grid) to the cells surrounding each
cell, and then evaluate the supplied function for the result i.e in the
first case below simply the max value of all the cells in a 5x5 grid
centred on each cell in turn. This works as expected.
However the "min" function doesnt - the value 63.4079 is in the extreme
top-right corner of the raster, so only falls within the 5x5 grid around
the 16th cell (top-right of the 4 evaluated) yet this value is also
evaluated as the result for the other 3 cells as well, despite not
falling within the 5x5 matrix around these cells. Min works with 3x3 but
not 5x5 and larger. This is only for illustration not something I'm
actually want to do - I want to apply "median" with a 27x27 grid, and I
also get the same problem here but the data for that's not so obvious ;-)
Can someone please point out my obvious mistake?
Cheers
Andy
> rc_elev[]
[1] 66.1713 66.5426 67.8564 66.8574 65.6665 63.4079
[7] 67.5778 67.5254 67.2219 67.5153 67.3972 66.3184
[13] 68.3040 69.2129 67.9709 68.5446 68.2876 68.5754
[19] 68.9119 68.9613 69.2422 68.9341 68.9922 69.1553
[25] 69.8958 69.9700 70.9934 71.1945 71.6998 68.7679
[31] 72.6752 72.8435 74.9098 74.6324 75.1180 75.8749
> rc_max <- focalFilter(rc_elev, filter=matrix(1, nrow=5, ncol=5), fun=max)
> rc_max[]
[1] NA NA NA NA NA NA
[7] NA NA NA NA NA NA
[13] NA NA 71.6998 71.6998 NA NA
[19] NA NA 75.1180 75.8749 NA NA
[25] NA NA NA NA NA NA
[31] NA NA NA NA NA NA
> rc_min <- focalFilter(rc_elev, filter=matrix(1, nrow=5, ncol=5), fun=min)
> rc_min[]
[1] NA NA NA NA NA NA
[7] NA NA NA NA NA NA
[13] NA NA 63.4079 63.4079 NA NA
[19] NA NA 63.4079 63.4079 NA NA
[25] NA NA NA NA NA NA
[31] NA NA NA NA NA NA
> rc_elev
class : RasterLayer
filename :
nrow : 6
ncol : 6
ncell : 36
min value : 63.4079
max value : 75.8749
projection : +proj=tmerc +lat_0=53.5 +lon_0=-8 +k=1.000035 +x_0=200000
+y_0=250000 +a=6377341.89 +b=6356036.143 +towgs84=0,0,0,0,0,0,0 +units=m
+no_defs
extent : 167375, 167675, 305775, 306075 (xmin, xmax, ymin, ymax)
resolution : 50, 50 (x, y)
[[alternative HTML version deleted]]