我正在处理空间数据以便为分析做好准备 - 我在研究区域的所需范围内有一个DEM,尽管我在全国范围内有大约39个其他层(美国)。有没有办法同时将所有这39个层裁剪到与DEM相同的程度?
另外,我将在不同的投影中将输出与其他图层重叠。是否可以调整输出图层的投影和像素大小?
我正在尝试尽可能多地使用免费软件进行数据操作......
答案 0 :(得分: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)