inst/tinytest/test-focal2r.R

expect_equal({
  data(dem)
  dem1 = focal2r(dem, matrix(1,3,3), mean, na.rm = TRUE)
  attr(dem1[[1]], "dim") = setNames(attr(dem1[[1]], "dim"), c("x", "y"))
  dem1[[1]]
},
structure(
  c(
    166.75,153.666666666667,118.666666666667,84.8333333333333,54.6666666666667,36.8333333333333,22.8333333333333,11.8333333333333,5.83333333333333,2.5,1,1,1.5,176,164.111111111111,130.111111111111,96.5555555555556,64.8888888888889,45.5555555555556,28.8888888888889,15.2222222222222,7.22222222222222,3.22222222222222,2.22222222222222,2.33333333333333,2.83333333333333,192,182.333333333333,152.777777777778,120.555555555556,88,65.4444444444444,44,24.8888888888889,12.1111111111111,5.55555555555556,4,4,4.5,201.833333333333,195.888888888889,173.222222222222,145.333333333333,113.444444444444,89.2222222222222,64.5555555555556,41.6666666666667,21.8888888888889,10.1111111111111,5.33333333333333,5,5.5,206.833333333333,204.222222222222,192,169.777777777778,141.555555555556,114.777777777778,89.5555555555556,65.1111111111111,39.3333333333333,19.4444444444444,7.11111111111111,4.22222222222222,4.66666666666667,207.833333333333,207.666666666667,203.333333333333,188.111111111111,165.555555555556,139.555555555556,115.222222222222,90.3333333333333,62.2222222222222,33.7777777777778,12.7777777777778,3.33333333333333,3.16666666666667,205,205.111111111111,205.666666666667,198.666666666667,185.222222222222,162.777777777778,139.555555555556,114.444444444444,87.6666666666667,51.7777777777778,22.2222222222222,3.55555555555556,2.16666666666667,197.5,197.111111111111,197.555555555556,197.555555555556,193.333333333333,180.111111111111,160.333333333333,136.333333333333,111.555555555556,75.6666666666667,39.7777777777778,11.8888888888889,3.5,185.166666666667,184.444444444444,184.555555555556,189.666666666667,192.555555555556,188.444444444444,174.777777777778,156.555555555556,133.111111111111,100.333333333333,58.7777777777778,24.7777777777778,8,170,169.444444444444,169.222222222222,176.222222222222,182.777777777778,186,180.444444444444,169.333333333333,147.555555555556,120.777777777778,76.7777777777778,40.5555555555556,15.3333333333333,161.5,161.166666666667,161.333333333333,169.333333333333,177.166666666667,183.166666666667,181.5,175,154.333333333333,129,83.8333333333333,47,19.5
  ),
  .Dim = c(x=13L, y=11L)
))

expect_equal({
  data(dem)
  dem1 = focal2r(dem, matrix(1,5,5), max, na.rm = FALSE)
  attr(dem1[[1]], "dim") = setNames(attr(dem1[[1]], "dim"), c("x", "y"))
  dem1[[1]]
},
  structure(
    c(
      NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,208,207,203,167,139,112,93,64,39,NA,NA,NA,NA,211,211,211,207,162,140,117,91,69,NA,NA,NA,NA,211,211,211,211,185,167,141,112,87,NA,NA,NA,NA,211,211,211,211,204,188,161,139,113,NA,NA,NA,NA,211,211,211,211,204,196,181,158,135,NA,NA,NA,NA,211,211,211,211,204,196,186,173,163,NA,NA,NA,NA,211,211,211,211,204,196,186,183,166,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA
    ),
    .Dim = c(x=13L, y=11L)
  )
)

expect_equal({
  data(dem)
  dem1 = focal2r(dem, matrix(1,3,3), min, na.rm = TRUE)
  attr(dem1[[1]], "dim") = setNames(attr(dem1[[1]], "dim"), c("x", "y"))
  dem1[[1]]
},
  structure(
    c(
      140,115,64,44,31,16,6,4,2,0,-2,-2,-2,140,115,64,44,31,16,6,4,2,0,-2,-2,-2,167,140,86,60,43,27,14,4,4,0,-2,-2,-2,185,166,108,86,62,41,20,5,5,3,3,3,4,199,183,141,115,91,64,34,15,7,2,1,1,1,206,203,167,139,112,93,64,39,8,2,1,1,1,198,197,197,162,140,117,91,69,27,2,1,1,1,183,183,183,184,167,141,112,87,63,6,1,1,1,166,166,166,168,174,161,139,113,88,11,1,1,1,152,152,152,153,155,175,158,135,109,69,10,4,4,152,152,152,153,155,175,173,163,120,95,26,5,5
    ),
    .Dim = c(x=13L, y=11L)
  )
)

expect_equal({
  data(dem)
  x = dem[, 1:7, 1:7]
  x[[1]][] = 0
  x[[1]][1,2] = 1
  x[[1]][1,3] = 1
  y = focal2r(x, matrix(c(1,rep(0,8)),3,3), mean, na.rm = TRUE)
  attr(y[[1]], "dim") = setNames(attr(x[[1]], "dim"), c("x", "y"))
  y[[1]]
},
  structure(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.111111111111111,
0,0,0,0,0,0,0.111111111111111,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),.Dim=c(x=7L,
y=7L))
)

Try the starsExtra package in your browser

Any scripts or data that you put into this service are public.

starsExtra documentation built on Nov. 18, 2021, 5:08 p.m.