2

我正在使用空间数据为分析做准备——我在我的研究区域的所需范围内有一个 DEM,尽管我在全国范围内有大约 39 个其他图层(美国)。有没有办法一次将所有这 39 层裁剪到与 DEM 相同的程度?

此外,我将在不同的投影中将输出与其他层叠加。是否可以调整输出层的投影和像素大小?

我正在尝试尽可能多地使用免费软件来处理我的数据......

4

1 回答 1

3

我遇到了上面的问题,但是已经在 R 中编写了一个函数来批量完成所有这些 - 见下文。我有 39 个美国大陆规模的气候数据层(来自 PRISM 气候数据组;http://www.prism.oregonstate.edu/),并希望将它们剪辑到加利福尼亚南部的 DEM 范围内,重新投影它们,并导出它们以便于导入并与 SAGA GIS 中的其他图层一起使用。下面是代码,以及如何运行的示例,将工作目录设置为包含您要裁剪的图层的文件夹,并且只有这些图层。

在处理过程中,所有数据都存储在内存中,因此对于庞大的数据集,它可能会因为内存不足而挂起……这可能是一个需要改进的地方。

此外,R 论坛上的回复也提供了一种更短、更优雅的方式: http: //permalink.gmane.org/gmane.comp.lang.r.geo/18320

我希望有人觉得它有用!

#########################################
#BatchCrop Function ###
#by Mike Treglia, mtreglia@gmail.com ###
###Tested in R Version 3.0.0 (64-bit), using 'raster' version 2.1-48 and 'rgdal' version 0.8-10
########################################
#This function crops .asc raster files in working directory to extent of another layer (referred to here as 'reference' layer), converts to desired projection, and saves as new .asc files in the working directory. It is important that the original raster files and the reference layer are all in the same projection, though different pixel sizes are OK. The function can easily be modified to use other raster formats as well
#Note, Requires package 'raster'
#Function Arguments:
#'Reference'  refers to name of the layer with the desired extent; 'OutName' represents the intended prefix for output files; 'OutPrj' represents the desired output projection; and 'OutRes' represents the desired Output Resolution
BatchCrop<-function(Reference,OutName,OutPrj,OutRes){
        filenames <- list.files(pattern="*.asc", full.names=TRUE)   #Extract list of  file names from working directory
        library(raster) #Calls 'raster' library
        #Function 'f1' imports data listed in 'filenames' and assigns projection
            f1<-function(x,z) {
            y <- raster(x)
            projection(y) <- CRS(z)
            return(y)
            }
            import <- lapply(filenames,f1,projection(Reference))
        cropped <- lapply(import,crop,Reference)    #Crop imported layers to reference layer, argument 'x'
        #Function 'f2' changes projectection of cropped layers
            f2<-function(x,y) {
            x<-projectRaster(x, crs=OutPrj, res=OutRes)
            return(x)
            }
            output <- lapply(cropped,f2,OutPrj)
        #Use a 'for' loop to iterate writeRaster function for all cropped layers
        for(i in (1:max(length(filenames)))){           #
            writeRaster(output[[i]],paste(deparse(substitute(OutName)), i), format='ascii')
            }
            }
#############################################
###Example Code using function 'BatchCrop'###
#############################################
#Data layers to be cropped downloaded from: http://www.prism.oregonstate.edu/products/matrix.phtml?vartype=tmax&view=maps [testing was done using 1981-2010 monthly and annual normals; can use any .asc layer within the bounds of the PRISM data, with projection as lat/long and GRS80]
#Set Working Directory where data to be cropped are stored
setwd("D:/GIS/PRISM/1981-2010/TMin")
#Import Reference Layer
reference<-raster("D:/GIS/California/Hab Suitability Files/10m_DEM/10m DEM asc/DEM_10m.asc")
#Set Projection for Reference Layer
projection(reference) <- CRS("+proj=longlat +ellps=GRS80")
#Run Function [desired projection is UTM, zone 11, WGS84; desired output resolution is 800m]
BatchCrop(Reference=reference,OutName=TMinCrop,OutPrj="+proj=utm +zone=11 +datum=WGS84",OutRes=800)
于 2013-07-13T22:06:39.540 回答