Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中创建非矩形光栅_R_Spatial_R Raster - Fatal编程技术网

在R中创建非矩形光栅

在R中创建非矩形光栅,r,spatial,r-raster,R,Spatial,R Raster,在我的分析中,我必须在光栅的单元id(即单元编号)上循环,这是非可选的,并且是在不同的软件设置(BUGScode)中完成的,我不能熟练使用。我的光栅必须精确地适合一个形状文件(我的研究区域),否则我将在我没有值或兴趣的单元格编号上循环,然后。。。你知道会发生什么;) 可复制示例 ### get a shapefile GER<-getData("GADM", country="DEU",level=1,download=T,path=getwd())

在我的分析中,我必须在光栅的单元id(即单元编号)上循环,这是非可选的,并且是在不同的软件设置(BUGScode)中完成的,我不能熟练使用。我的光栅必须精确地适合一个形状文件(我的研究区域),否则我将在我没有值或兴趣的单元格编号上循环,然后。。。你知道会发生什么;)

可复制示例

### get a shapefile
GER<-getData("GADM", country="DEU",level=1,download=T,path=getwd())

### make a raster
raster<-raster(ext=extent(GER), vals=0, crs=crs(GER))

plot(raster);plot(GER, add=T)

### now make the raster fit exactly to the German borders
mask.raster<-mask(raster, GER)

plot(mask.raster) ### looks right, but WAIT
plot(mask.raster, colNA="black") ### the border cells were only set to NA, which is problematic if your raster contains NA in the first place, i.e. if you used rasterize to count points in the raster cells

trim.raster<-trim(raster, values=NA)

plot(trim.raster, colNA="black"); plot(GER, add=T) ### trim only trims complete rows/columns from the raster

crop.raster<-crop(raster, GER)

plot(crop.raster, colNA="black");plot(GER, add=T) ### crop only crops to extent

###获取一个shapefile

GER根据大多数定义以及支持它们的文件格式,光栅是矩形的。你声称这不是所有GIS软件的情况,但我不认为这是真的,你也没有提供任何证据。也许您正在考虑(类似光栅的)矩形多边形


而且,我也不知道接下来会发生什么。大多数有能力的R软件只需跳过
NA

的光栅单元,正如我最近发现的那样,R中的
光栅
意味着某种投影矩阵,因此只能是矩形(或在特殊情况下是对角的),但实际上并不适合形状

当你只是简单地屏蔽光栅文件时会发生什么。 如果在1:ncell(光栅)
中循环say
单元格,可能会遇到以下问题:

  • 更长的处理时间,因为您必须在可能更多的单元格上循环
  • 如果您正在合并总和等,则显示错误消息
  • 在我的例子中,一个问题是,如果您使用
    光栅化
    对光栅中的一些点进行计数,并且计数为0(这被解释为NA…无需计数),则将跳过研究区域内的同一类单元。通过将“计数”为空=NA的光栅值设置为0,然后屏蔽光栅,可以很容易地避免这种情况,但这对于许多工作边界来说都是一种情况

  • 尝试在QGIs中光栅化相同的形状文件,您将仅在多边形上获得光栅单元;)