简体   繁体   中英

How to access class names in a raster categorical map

I have some classified raster layers as categorical land cover maps. All the layers having exactly the same categories (lets say: "water", "Trees", "Urban","bare soil" ) but they are from different time points (eg 2005 and 2015) I load them into memory using the raster function like this:

comp <- raster("C:/workingDirectory4R/rasterproject/2005marsh3.rst")
ref <-  raster("C:/workingDirectory4R/rasterproject/2013marsh3.rst")

"comp" is the comparison map at time t+1 and "ref" is the reference map from time t . Then I used the crosstab function to generate the confusion table . This table can be used to explore the changes in categories through the time interval.

contingency.Matrix <- crosstab(comp, ref)

The result is in the matrix format with the "comp" categories in the column and "ref" in the rows . And column and row names labeled with numbers numbers 1 to 4 .

Now I have 2 questions and I really appreciate any help on how to solve them.

1- I want to assign the category names to the columns and rows of the matrix to facilitate it's interpretation.

2- Now let's say I have three raster layers for 2005, 2010 and 2015. This means I would have two confusion tables one for 2005-2010 and another one for 2010-2015. What's the best procedure to automate this process with the minimal interaction from user.

I thought to ask the user to load the raster layers, then the code save them in a list. Then I ask for a vector of years from the user but the problem is how can I make sure that the order of raster layers and the years are the same? And is there a more elegant way to do this.

Thanks

I found a partial answer to my first question. If the categorical map is created in TerrSet(IDRISI) software with the ".rst" extention then I can extract the category names like this:

comp <- raster("C:/rasterproject/2005subset.rst")
attributes <- data.frame(comp@data@attributes)
categories <- as.character(attributes[,8])

and I get a vector with the name of categories. However if the raster layers are created with a different extension then the code won't work. For instance if the raster is created in ENVI then the third line of the code should get changed to:

categories <- as.character(attributes[,2])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM