将对象转换为栅格砖/栅格堆栈

时间:2019-11-19 16:53:16

标签: r rasterizing

在使用rasterToPoints创建矩阵之后,是否可以使新对象恢复为原始的栅格堆栈/栅格砖形式?我的对象称为“ avghist”,具有138个值/层,但是我希望这成为其他用途的栅格堆栈/砖块。这是avghist的创建方式:

data("wrld_simpl")
b<-wrld_simpl

data("wrld_simpl")
b<-wrld_simpl

landhist <- mask(RCP1pctCO2Median,b)
histnew<-rasterToPoints(landhist)
weighthist <- cos(histnew[,"y"]*(pi/180))
histnew[,3:ncol(histnew)] = apply(histnew[,3:ncol(histnew)], 2, function(x) x  
* weighthist)
avghist <- colSums(histnew[,3:ncol(histnew)])/sum(weighthist)

avghist


head(avghist)

layer.1  layer.2  layer.3  layer.4  layer.5  layer.6 

52.75701 50.86984 49.91926 49.35157 52.78295 48.57297 

“ landhist”已经是一个光栅块,但是使用rasterToPoints转换为矩阵,最终获得了“ avghist”。 Landhist看起来像这样:

class       : RasterBrick 
dimensions  : 64, 128, 8192, 138  (nrow, ncol, ncell, nlayers)
resolution  : 2.8125, 2.789327  (x, y)
extent      : -181.4062, 178.5938, -89.25846, 89.25846  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       :   layer.1,   layer.2,   layer.3,   layer.4,   layer.5,   layer.6,   layer.7,     
layer.8,   layer.9,  layer.10,  layer.11,  layer.12,  layer.13,  layer.14,  layer.15, ... 
min values  :  52.88060,  54.93341,  55.79788,  54.18263,  53.85123,  55.68460,  50.17187,    
49.60006,  55.21446,  62.36937,  56.81446,  64.73854,  54.57714,  55.34295,  59.67952, ... 
max values  :  99.60231,  96.26976, 115.46164, 123.63807, 108.63827, 107.94791, 102.06677,   
102.09359, 132.50890, 138.31932, 101.19222,  97.19884, 115.07969, 131.96504, 120.14971, ... 

要将“ avghist”转换为光栅,我尝试了以下操作:

library(raster) 
newly<-rasterize(avghist) 

但是,这样做,我收到此错误:

"Error in (function (classes, fdef, mtable) : unable to find an inherited method for function   ‘rasterize’ for signature ‘"numeric", "missing"’ 

为什么会出现此错误?据我了解,栅格化功能应将avghist更改为栅格砖或堆栈,不是吗?

这是as.raster(avghist / 255)的结果:

最近<-as.raster(avghist / 255)

> print(newly)
     [,1]     
[1,] "#353535"
[2,] "#333333"
[3,] "#323232"
[4,] "#313131"
[5,] "#353535"
[6,] "#313131"
[7,] "#313131"
[8,] "#333333"
[9,] "#333333"
[10,] "#303030"
[11,] "#333333"
[12,] "#363636"
[13,] "#353535"
[14,] "#363636"
[15,] "#333333"
[16,] "#353535"
[17,] "#353535"
[18,] "#353535"
[19,] "#383838"
[20,] "#343434"
[21,] "#353535"
[22,] "#353535"
[23,] "#383838"
[24,] "#383838"
[25,] "#333333"
[26,] "#373737"
[27,] "#363636"
[28,] "#353535"
[29,] "#393939"
[30,] "#343434"
[31,] "#333333"
[32,] "#343434"
[33,] "#353535"
[34,] "#373737"
[35,] "#383838"
[36,] "#3D3D3D"
[37,] "#393939"
[38,] "#373737"
[39,] "#3C3C3C"
[40,] "#353535"
[41,] "#383838"
[42,] "#3C3C3C"
[43,] "#363636"
[44,] "#363636"
[45,] "#3B3B3B"
[46,] "#3B3B3B"
[47,] "#383838"
[48,] "#373737"
[49,] "#383838"
[50,] "#343434"
[51,] "#363636"
[52,] "#393939"
[53,] "#393939"
[54,] "#3C3C3C"
[55,] "#3D3D3D"
[56,] "#383838"
[57,] "#393939"
[58,] "#3B3B3B"
[59,] "#383838"
[60,] "#383838"
[61,] "#383838"
[62,] "#3E3E3E"
[63,] "#3B3B3B"
[64,] "#3C3C3C"
[65,] "#3E3E3E"
[66,] "#404040"
[67,] "#3F3F3F"
[68,] "#393939"
[69,] "#383838"
[70,] "#383838"
[71,] "#393939"
[72,] "#3C3C3C"
[73,] "#3F3F3F"
[74,] "#3B3B3B"
[75,] "#3E3E3E"
[76,] "#3A3A3A"
[77,] "#373737"
[78,] "#3A3A3A"
[79,] "#3E3E3E"
[80,] "#3E3E3E"
[81,] "#434343"
[82,] "#393939"
[83,] "#444444"
[84,] "#454545"
[85,] "#3C3C3C"
[86,] "#404040"
[87,] "#3E3E3E"
[88,] "#444444"
[89,] "#414141"
[90,] "#444444"
[91,] "#464646"
[92,] "#3F3F3F"
[93,] "#434343"
[94,] "#424242"
[95,] "#424242"
[96,] "#404040"
[97,] "#3C3C3C"
[98,] "#454545"
[99,] "#444444"
[100,] "#414141"
[101,] "#484848"
[102,] "#454545"
[103,] "#494949"
[104,] "#434343"
[105,] "#444444"
[106,] "#464646"
[107,] "#424242"
[108,] "#464646"
[109,] "#494949"
[110,] "#414141"
[111,] "#414141"
[112,] "#454545"
[113,] "#3E3E3E"
[114,] "#4B4B4B"
[115,] "#4A4A4A"
[116,] "#424242"
[117,] "#414141"
[118,] "#484848"
[119,] "#424242"
[120,] "#474747"
[121,] "#434343"
[122,] "#444444"
[123,] "#454545"
[124,] "#3F3F3F"
[125,] "#424242"
[126,] "#474747"
[127,] "#464646"
[128,] "#414141"
[129,] "#3E3E3E"
[130,] "#474747"
[131,] "#484848"
[132,] "#414141"
[133,] "#454545"
[134,] "#474747"
[135,] "#444444"
[136,] "#4C4C4C"
[137,] "#4F4F4F"
[138,] "#494949"

这现在是栅格化格式吗?

谢谢,我们将不胜感激!

1 个答案:

答案 0 :(得分:0)

您是否尝试过进行raster(avghist)as.raster(avghist)