To avoid empty rasters we should first set the pixels equal to NaN and then equal to the threshold.
The problem is seen in outer_procedure.r:513 and plot_results.r:90.
Let's look at the code. The issue is in the following two lines b)
and c)
of the code marked as <---------
for (i in 1:(length(files) + 1)) {
if (i == (length(files) + 1)) {
dummy <- raster::raster(files[(i - 1)]) <--------------- a)
dummy[dummy < thres[(i - 1)]] <- i <--------------- b)
dummy[dummy >= thres[(i - 1)]] <- NA <--------------- c)
class[[i]] <- dummy
} else {
dummy <- raster::raster(files[i])
dummy[dummy < thres[i]] <- NA
dummy[dummy >= thres[i]] <- i
class[[i]] <- dummy
}
}
For the last iteration of this loop, the class "something else"
(it means all pixels not belonging to any of the 7 classes) if the threshold
value (number of models selected for the last class) is less or equal to the step
number (in this case 8 ), we will build an empty raster, this is, a raster full of NaNs, which is incorrect.
Let's run the 3 lines above:
# Line a): initial raster
> dummy <- raster::raster("/home/romulo/gitlab/gui_prototype/hasa/test12h/Results//step_07_xeric_grass.tif")
> dummy
class : RasterLayer
dimensions : 115, 161, 18515 (nrow, ncol, ncell)
resolution : 10, 10 (x, y)
extent : 746231, 747841, 5883760, 5884910 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs
source : step_07_xeric_grass.tif
names : step_07_xeric_grass
values : 1, 2 (min, max)
# Line b): set to the step value (look at the min and max of the dummy raster)
> dummy[dummy < thres[7]] <- 8
> dummy
class : RasterLayer
dimensions : 115, 161, 18515 (nrow, ncol, ncell)
resolution : 10, 10 (x, y)
extent : 746231, 747841, 5883760, 5884910 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs
source : memory
names : step_07_xeric_grass
values : 2, 8 (min, max)
# Line c): set to NaN (look at the min and max of the dummy raster)
> dummy[dummy >= thres[7]] <- NA
> dummy
class : RasterLayer
dimensions : 115, 161, 18515 (nrow, ncol, ncell)
resolution : 10, 10 (x, y)
extent : 746231, 747841, 5883760, 5884910 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs
source : memory
names : step_07_xeric_grass
values : NA, NA (min, max)
As we can see the result raster is incorrect. I think the solution is swap line b
with line c
, this is, set first the NaNs
and then the step value as it is done in the else
clause.
@carstenn what do you think?