I have a classified image with three categories (specific grass species: 1, ground: 2, other vegetation: 3). I am trying to calculate the area of each category. For the classification process I used the superClass RStoolbox method. For the calculating area method, I am using another code in a similar question, but am having some trouble with my image being in a superClass format type. I tried exporting and importing the file as a new raster and ended up getting a different error. Any suggestions?
img <- brick("C:/Users/name/location/Blung/null.tif")
shp <- st_read("C:/Users/name/location/Blung/Training.shp")
shpS4 <- as_Spatial(shp)
NDVI.Overlay <- function(b1, b4) {
NDVI.Calc <- (b4 - b1) / (b4 + b4)
return(NDVI.Calc)
}
NDVI <- overlay(img[[1]], img[[4]], fun = NDVI.Overlay)
img_update <- addLayer(img, NDVI)
names(img_update) <- c('b1', 'b2', 'b3', 'b4', 'NDVI')
SC <- superClass(img, trainData = shpS4, responseCol = "id", model = "rf", tuneLength = 1, trainPartition = 0.8)
SC[] = sample(1:3, ncell(SC), replace=TRUE)
counts <- tapply(area(SC), SC[], sum)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘area’ for signature ‘"superClass"’
writeRaster(SC$map, filename = "C:/Users/name/location/Blung_classification_R.tif")
img_processing <- raster("C:/Users/name/location/Blung/Blung_classification_R.tif")
img_processing[] = sample(1:3, ncell(img_processing), replace=TRUE)
counts <- tapply(area(img_processing), img_processing[], sum)
Error in res[i] <- readBin(x@file@con, what = dtype, n = 1, size = dsize, :
replacement has length zero
In addition: Warning messages:
1: In .local(x, ...) :
This function is only useful for Raster* objects with a longitude/latitude coordinates
2: In .rasterFromRasterFile(grdfile, band = band, objecttype, ...) :
size of values file does not match the number of cells (given the data type)