簡體   English   中英

將 object 轉換為光柵磚/光柵堆棧

[英]Converting object to a Raster brick/Raster stack

使用 rasterToPoints 創建矩陣后,是否可以讓新的 object 返回原始的 Raster Stack/Raster Brick 形式? 我的 object 被稱為“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"’ 

為什么會出現這個錯誤? 據我了解,光柵化 function 應該將 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"

這是現在光柵化格式嗎?

謝謝,任何幫助將不勝感激!

您是否嘗試過raster(avghist)as.raster(avghist)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM