使用其符号系统编写分类栅格 [英] Write classified raster with its symbology

查看:54
本文介绍了使用其符号系统编写分类栅格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对很多栅格进行了分类,我想用我定义的符号系统来编写它们.

I've classified lots of rasters and I'd like to write them with the symbology I've defined.

这是我正在处理的栅格类型:

Here is the kind of raster I'm dealing with:

library(raster)
library(rasterVis)
r <- raster(nrow=10, ncol=10)
r[] = 1
r[51:100] = 3
r[3:6, 1:5] = 5
r <- ratify(r)

rat <- levels(r)[[1]]
rat$landcover <- c('Pine', 'Oak', 'Meadow')
rat$class <- c('A1', 'B2', 'C3')
levels(r) <- rat
levelplot(r, col.regions=c('palegreen', 'midnightblue', 'indianred1'))

推荐答案

我在这个很棒的 网站

addColorTable <- function(inRstName, outRstName, rat.df){
  library(rgdal)
  r<- readGDAL(inRstName)
  rat.df$color<- as.character(rat.df$color)
  rat.df$attribute<- as.character(rat.df$attribute)
  outRst <- writeGDAL(r, outRstName, type="Byte", 
  colorTable=list(rat.df$color), 
  catNames=list(rat.df$attribute), mvFlag=11L)
  return(raster(outRst))
}


library(rgdal)
library(raster)

# create dummy data set
r <- raster(nrow=10, ncol=10)
r[] <- 0
r[51:100] <- 1
r[3:6, 1:5] <- 2
r[1, 1] <- 3
writeRaster(r,'dummy_raster.tif',overwrite=T)

# This defines the values, the color and the attribute
valT <- c(0,1,2,3)
colT <-  c("#FF0000", "#FF9900" ,"#99FF00","#0000FF")
attT <- c('Forest','Water body','City','Cropland')
rat.df <- data.frame(value=valT,color=colT,attribute=attT)

# apply the magic function
rnew <- addColorTable('dummy_raster.tif', 'dummy_raster_with_symbology.tif', rat.df)

# plot the results and tadaaaa
spplot(rnew, col.regions=colT)

这篇关于使用其符号系统编写分类栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆