I have a land cover raster which has 15 land cover classes (which previously had 70 land cover classes and I aggregated the same). However, I realized that I need 15 different rasters based on each of the land cover types. Is there any way I can achieve this in R?
lulc_2015 <- raster("E:\\LandCover_Data\\2015_lulc")
#Load matrix to be used for reclassification
rec <- read.csv("E:\\LandCover_ReclassifyMatrix.csv")
#Reclassifying the Raster to convert 70 LC to 15 LC
rc <- as.matrix(data.frame(from=rec$V2, to=rec$To))
New_LULC_2015 <- reclassify(lulc_2015,rc)
New_LULC_2015 <- ratify(New_LULC_2015)
rat <- data.frame(
ID= 1:15,
LandCover = c("Evergreen","Deciduous","Mixed Forest",
"grass","Degraded Forest","Scrubland",
"Dry Grassland","Wet Grassland","Plantation",
"Settlement","High Elevation Plantation","Coastal",
"Barren Land","Cropland","Wetlands")
)
levels(New_LULC_2015)<- rat
> New_LULC_2015
class : RasterLayer
dimensions : 642, 382, 245244 (nrow, ncol, ncell)
resolution : 1000, 1000 (x, y)
extent : 461951, 843951, 892583.3, 1534583 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : layer
values : 1, 15 (min, max)
attributes :
ID LandCover
from: 1 Evergreen
to : 15 Wetlands
If I try creating a new raster, I get this error.
> raster(New_LULC_2015@data@attributes[[1]]$ID==1)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘raster’ for signature
‘"logical"’
I simply need 15 different rasters, for Evergreen, Deciduous etc. I need these as I realized I need to use the predict function from the raster package for a random forest model (Unless I don't really need to create separate rasters).
mask()
, is faster:r.evergreen <- mask(ew_LULC_2015, ew_LULC_2015[ew_LULC_2015 != 15], mask.value=0)
. Also, take a look ofassign()
to create different objects inside a loop