2013-12-22 140 views
9

我是新的R所以這個問題是非常基本的,但我一直在努力,並找不到解決方案。我想從同一區域的一些landsat圖像創建一個光柵磚。 他們下載的格式爲HDF-EOS,我使用Modis Reprojection Tool將它們轉換爲.tif如何用不同程度的光柵創建光柵磚?

生成的柵格具有相同的投影,但其範圍,分辨率和來源不同。

我試了幾種方法,這裏總結如下:

  1. 手動定義的一個子集範圍和子集所有的柵格。然後嘗試使用子集柵格製作磚塊

  2. 重新採樣柵格,使其具有相同數量的列和行。理想情況下,這將確保柵格單元對齊,並且可以放入柵格磚中。這個選項創建了一個磚塊,其中柵格沒有值,它們是空的。

我想知道什麼是我應該遵循的概念來糾正程度。創建一個空的光柵是否正確(和高效),我將在稍後使用導入的landsat圖像的值填充它?你能看到我犯了什麼錯誤嗎? 如果它是相關的,我工作的一個的Mac OSX 10.9.1版本,並使用rgdal版本0.8-14

任何幫助將非常感激!

三江源

我這裏補充我一直在使用的代碼:

# .tif files have been creating using the Modis Reprojection Tool. Input 
# files used for this Tool was LANDSAT HDF-EOS imagery. 

library(raster) 
library(rgdal) 

setwd()=getwd() 

# Download the files from dropbox: 
dl_from_dropbox <- function(x, key) { 
    require(RCurl) 
    bin <- getBinaryURL(paste0("https://dl.dropboxusercontent.com/s/", key, "/", x), 
         ssl.verifypeer = FALSE) 
    con <- file(x, open = "wb") 
    writeBin(bin, con) 
    close(con) 
    message(noquote(paste(x, "read into", getwd()))) 
} 
dl_from_dropbox("lndsr.LT52210611985245CUB00-vi.NDVI.tif", "qb1bap9rghwivwy") 
dl_from_dropbox("lndsr.LT52210611985309CUB00-vi.NDVI.tif", "sbhcffotirwnnc6") 
dl_from_dropbox("lndsr.LT52210611987283CUB00-vi.NDVI.tif", "2zrkoo00ngigfzm") 



# Create three rasters 
tif1 <- "lndsr.LT52210611985245CUB00-vi.NDVI.tif" 
tif2 <- "lndsr.LT52210611985309CUB00-vi.NDVI.tif" 
tif3 <- "lndsr.LT52210611987283CUB00-vi.NDVI.tif" 

r1 <- raster(tif1, values=TRUE) 
r2 <- raster(tif2, band=1, values=TRUE) 
r3 <- raster(tif3, band=1, values=TRUE) 

### Display their properties 
# projection is identical for the three rasters 
projection(r1) 
# "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" 
projection(r2) 
projection(r3) 

# Extents are different 
extent(r1) 
# class  : Extent 
# xmin  : -45.85728 
# xmax  : -43.76855 
# ymin  : -2.388705 
# ymax  : -0.5181549 
extent(r2) 
# class  : Extent 
# xmin  : -45.87077 
# xmax  : -43.78204 
# ymin  : -2.388727 
# ymax  : -0.5208711 
extent(r3) 
# class  : Extent 
# xmin  : -45.81952 
# xmax  : -43.7173 
# ymin  : -2.405129 
# ymax  : -0.5154312 

# origin differs for all 
origin(r1) 
# 5.644590e-05 -8.588605e-05 
origin(r2) 
# 0.0001122091 -0.0001045107 
origin(r3) 
# 6.949976e-05 -5.895945e-05 

# resolution differs for r2 
res(r1) 
# 0.0002696872 0.0002696872 
res(r2) 
# 0.0002696875 0.0002696875 
res(r3) 
# 0.0002696872 0.0002696872 


## Try different approaches to create a raster brick 

# a- define a subset extent, and subset all the rasters 
plot(r1, main="layer1 NDVI") 
de <- drawExtent(show=TRUE, col="red") 
de 
# class  : Extent 
# xmin  : -45.36159 
# xmax  : -45.30108 
# ymin  : -2.002435 
# ymax  : -1.949501 
e <- extent(-45.36159,-45.30108,-2.002435,-1.949501) 
# Crop each raster with this extent 
r1c <- crop(r1,e) 
r2c <- crop(r2,e) 
r3c <- crop(r3,e) 
# Make raster brick 
rb_a <- brick(r1c,r2c,r3c) 
# Error in compareRaster(x) : different extent 

# b- Resample each raster 
s <- raster(nrow=6926, ncol=7735) # smallest nrow and ncol among r1,r2 and r3 
r1_res <- resample(r1,s, method="ngb") 
r2_res <- resample(r2,s, method="ngb") 
r3_res <- resample(r3,s, method="ngb") 
# Resampling gives for the three rasters the following message: 
# Warning message: 
# In .local(x, y, ...) : 
# you are resampling y a raster with a much larger cell size, 
# perhaps you should use "aggregate" first 

# Make raster brick 
rb_c <- brick(r1, r2, r3) 
# Error in compareRaster(x) : different extent 
+0

我認爲你忘了還加載'庫(RCurl)'? –

+0

您是否嘗試過在您的方法b中使用** projectRaster()** insted ** ** resample **? –

回答

8

這裏有一些事情來幫助你。由於我沒有.tif文件只是一些提示。你有沒有檢查你的光柵s的程度?這是世界的大小,只有那些列的單元格非常大。因此,在重新採樣之前,必須在柵格中添加一個範圍。從你的信息,我做了這樣的事情:

# create an extent that includes all your data 
e<-extent(-46, -43, -2, -0.6) 

# create a raster with that extent, and the number of rows and colums to achive a 
# similar resolution as you had before, you might have to do some math here.... 
# as crs, use the same crs as in your rasters before, from the crs slot 
s<-raster(e, nrows=7000, ncols=7800, [email protected]) 

# use this raster to reproject your original raster (since your using the same crs, 
# resample should work fine 
r1<-resample(r1, s, method="ngb") 

節日快樂, 本

PS更好的方法來找到你所需要的程度&分辨率:

# dummy extent from your rasters, instead use lapply(raster list, extent) 
a<-extent(-45.85728, -43.76855, -2.388705, -0.5181549) 
b<-extent(-45.87077, -43.78204, -2.388727, -0.5208711) 
c<-extent(-45.81952 ,-43.7173 , -2.405129 ,-0.5154312) 
extent_list<-list(a, b, c) 

# make a matrix out of it, each column represents a raster, rows the values 
extent_list<-lapply(extent_list, as.matrix) 
matrix_extent<-matrix(unlist(extent_list), ncol=length(extent_list) 
rownames(matrix_extent)<-c("xmin", "ymin", "xmax", "ymax") 

# create an extent with the extrem values of your extent 
best_extent<-extent(min(matrix_extent[1,]), max(matrix_extent[3,]), 
min(matrix_extent[2,]), max(matrix_extent[4,])) 

# the range of your extent in degrees 
ranges<-apply(as.matrix(best_extent), 1, diff) 
# the resolution of your raster (pick one) or add a desired resolution 
reso<-res(r1) 
# deviding the range by your desired resolution gives you the number of rows and columns 
nrow_ncol<-ranges/reso 

# create your raster with the following 
s<-raster(best_extent, nrows=nrow_ncol[2], ncols=nrow_ncol[1], [email protected]) 
+0

我真的很感謝你的幫助,謝謝你們倆!本你的代碼工作完美:)謝謝你的廣泛幫助! (對於其他用戶,次要評論:在r1 <-resample(r1,s,method =「nbr」)中,用於最近鄰居的縮寫是「ngb」) Thijs我不需要加載庫Rcurl)創建柵格磚(據我所知),但請注意,它用於從Dropbox下載文件,並已包含在download_from_dropbox函數中。 祝您有美好的假期! – user3127517