[GIS] Merging multiple attributes of a raster into a single attribute using R

editingmerger

I have a raster that has 50 land cover classes. I am trying to combine multiple attributes from the attribute table into a single attribute. For example, I would like "Evergreen", "Semi-Evergreen" as – EVERGREEN.

I used the Editor Toolbar in ArcGIS and selected these features, but the Merge button on the Editor toolbar is greyed out. I read another solution that asked me to "delete the Shared Features tool on the Topology toolbar", but I was unable to do so. Apparently, deleting the "Shared Features" tool will activate the merge tool?

Either way, I would like to combine the attributes into a single attribute in R.

What I have tried in R so far (just loading and viewing the attributes)

> lulc_2015 <- raster("E:\\MasterThesis\\Absence Approach\\Data\\LandCover_Data\\2015_lulc")
> lulc_2015
class       : RasterLayer 
dimensions  : 26782, 11768, 315170576  (nrow, ncol, ncell)
resolution  : 60, 60  (x, y)
extent      : 163125, 869205, 855611, 2462531  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=43 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
data source : E:\2015_lulc 
names       : X2015_lulc 
values      : 1, 134  (min, max)
attributes  :
    ID     COUNT CLASS_NAME
from:   1 118944856           
to  : 134   3589862 Settlement

> lulc_2015@data@attributes
[[1]]
ID     COUNT                       CLASS_NAME
1    1 118944856                                 
2    2   2397462                        Evergreen
3    6     39397         Sub-tropical broadleaved
4   11   2457007                   Semi-evergreen
5   12   8798892                  Moist deciduous
6   13       184        Sal mixed moist deciduous
7   14   1076081       Teak mixed moist deciduous
8   15   6675337                    Dry deciduous
9   17   1790087         Teak mixed dry deciduous
10  18    476694                     Thorn forest
11  19    247793                     Bamboo mixed
12  20        16             Temperate coniferous
13  22      1109                              Sal
14  23    708833                             Teak
15  25    138858                           Bamboo
16  26         4                             Pine
17  30     85417                       Hardwickia
18  33      2179                        Boswellia
19  35       210               Anogeissus pendula
20  41    107796                         Mangrove

How do I combine these classes?


For those providing a possible solution, please note I am currently using Reclassify tool from the Spatial Analyst extension in ArcGIS to achieve the same, but if there is an alternative, please provide the same below.

Best Answer

if you have, for example, 50 classes, and only classes 14,15,17 and 21 need to be aggregated all into 14, you can use raster:reclassify

require(raster)

r1 <- raster(xmn=0,xmx=10,ymn=0,ymx=10)
r1[] <- sample(1:50,100,rep=T)

# reclassification map must be a matrix
rc <- as.matrix(data.frame(from=c(14,15,17,21),to=c(14,14,14,14)))

r.rc <- reclassify(r1, rc)

#check 
freq(r1)
freq(r.rc)

i've found this to be the quickest way. If your raster is huge, reclassify can be done in parallel very quickly

Obviously you can make a large classification map with the matrix.