[R-sig-Geo] Variable window on a raster
Robert Hijmans
r.hijmans at gmail.com
Mon Mar 14 00:48:03 CET 2011
Etienne,
What you are doing makes sense to me. To avoid border problems, perhaps you
can first 'expand' the input raster by adding sufficient rows and columns at
each side such that there are no relevant cells within the filter when it
reaches the border.
By the way, here:
> r2 <- focal(r, nb=2, fun=vf)
nb should be ngb, and typically ngb would not be an even number.
Robert
> I'd like to vary the size of my window on a moving window filter. The size
> of the radius is set by a function of the center pixel (the current pixel
> processed). I picked the raster package, but as focal() is using a fix
> radius for the analysis, I thought I could use a large radius and apply a
> function according to the value of the center pixel inside the raster.
> Now,
> when reaching the borders of the raster, it's hard to tell where's the
> center pixel or what's the value of the current pixel processed.
>
> I did a little code to illustrate the problem of finding the center :
>
> library(raster)
> r <- raster(matrix(1:25, nrow=5))
>
> vf <- function(x, ...){
> x[length(x)/2+.5] # that' how I (wrongly) find the center pixel
> }
>
> r2 <- focal(r, nb=2, fun=vf)
> # result :
> as.matrix(r2-r)
>
>
> # [,1] [,2] [,3] [,4] [,5]
> #[1,] 1 0 0 0 -4
> #[2,] 1 0 0 0 -4
> #[3,] 1 0 0 0 -4
> #[4,] 1 0 0 0 -4
> #[5,] 0 -1 -1 -1 -5
>
>
> Do you know any way I could implement my function or if such a
> functionality
> is already implemented in another package ?
>
> Thanks,
> Etienne
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Variable-window-on-a-raster-tp6155312p6167442.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
More information about the R-sig-Geo
mailing list