Skip to main content
deleted 29 characters in body
Source Link
Vijay Ramesh
  • 1.3k
  • 16
  • 46

I have a land cover raster which has 15 land cover classes (which previously had 70 land cover classes and I recently aggregated the land cover classes to obtain a single raster with 15 land cover classes as mentioned abovesame).

  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
 
# Resampled raster to achieve same resolution as other data
 Resam_LC <- resample(agg_modal, newelev, method='ngb')

 > Resam_LCNew_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(Resam_LC@data@attributes[[1]]$ID==1New_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"’

Do I simply need 15 different rasters, for Evergreen, Deciduous etc. I need these as I realized I need to keep reclassify-inguse the abovepredict function from the raster as I did previously? Is therepackage for a quicker way?random forest model (Unless I don't really need to create separate rasters).

I have a land cover raster which has 15 land cover classes and I recently aggregated the land cover classes to obtain a single raster with 15 land cover classes as mentioned above.

  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
 
# Resampled raster to achieve same resolution as other data
 Resam_LC <- resample(agg_modal, newelev, method='ngb')

 > Resam_LC
 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(Resam_LC@data@attributes[[1]]$ID==1)
 Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘raster’ for signature 
 ‘"logical"’

Do I need to keep reclassify-ing the above raster as I did previously? Is there a quicker way?

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).

Source Link
Vijay Ramesh
  • 1.3k
  • 16
  • 46

Split a single raster into multiple rasters based on the attributes in R

I have a land cover raster which has 15 land cover classes and I recently aggregated the land cover classes to obtain a single raster with 15 land cover classes as mentioned above.

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

# Resampled raster to achieve same resolution as other data
 Resam_LC <- resample(agg_modal, newelev, method='ngb')

 > Resam_LC
 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(Resam_LC@data@attributes[[1]]$ID==1)
 Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘raster’ for signature 
 ‘"logical"’

Do I need to keep reclassify-ing the above raster as I did previously? Is there a quicker way?