I have a raster layer ranging from 0 (minimum) to 100 (maximum) and I would like to change its values to range from 1 to 100. It is important that data do not change, basically I want to have the same raster but with a different range of values.
I can work both on R and QGIS.
Thanks everyone for help! :)
CodePudding user response:
Let's take a simple example in R, where we have a raster with values ranging between 0 and 100, as we can see by the min value
and max value
fields:
library(raster)
#> Loading required package: sp
r <- raster(matrix(c(0, 25, 100, 15, 0, 22, 86, 54, 67), nrow = 3))
r
#> class : RasterLayer
#> dimensions : 3, 3, 9 (nrow, ncol, ncell)
#> resolution : 0.3333333, 0.3333333 (x, y)
#> extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax)
#> crs : NA
#> source : memory
#> names : layer
#> values : 0, 100 (min, max)
To overwrite the 0 values with 1s, we can do
r[][r[] == 0] <- 1
Resulting in
r
#> class : RasterLayer
#> dimensions : 3, 3, 9 (nrow, ncol, ncell)
#> resolution : 0.3333333, 0.3333333 (x, y)
#> extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax)
#> crs : NA
#> source : memory
#> names : layer
#> values : 1, 100 (min, max)
r[] = rescale(r[], to=c(1,100))
plot(r)