2015-02-08 50 views
4

我想概述给定单元格编号的栅格单元格。我做了一个简单的例子,我可以在功率点上做出来,但是可能有一个解决方案,对于720 x 360 vs 3 x 5这样做会更困难。 要生成样本数据:根据单元格编写一个感兴趣的栅格单元格

library(raster) 
x = raster(matrix(seq(1,15), nrow = 3)) 
plot(x) 

而且我想的修改的绘图命令(优选地),使得如果我来选择第五细胞,结果看起来是这样的: enter image description here

回答

2

这里有一个通用的方法,在这里我们基于感兴趣的单元格的行和列绘制的extent

library(raster) 
r <- raster(matrix(1:15, nrow=3)) 
plot(r) 
rc <- rowColFromCell(r, 5) 
plot(extent(r, rc[1], rc[1], rc[2], rc[2]), add=TRUE, col='red', lwd=3) 

fig1

到第四参数的第二至extent确定行(参数2和3)和列(参数4和5)将要用于计算的程度的跨度。

如果我们想勾勒细胞3,4,8,9,我们可以这样做:

plot(r) 
rc <- rowColFromCell(r, c(3, 4, 8, 9)) 
plot(extent(r, min(rc[, 1]), max(rc[, 1]), 
      min(rc[, 2]), max(rc[, 2])), add=TRUE, col='red', lwd=3) 

fig2

这工作正常周边相邻的单元组的矩形范围。如果你想概述任意选择的单元格,你可以考虑rasterToPolygons。例如。为电池单元2,8,9,11,和14:

plot(r) 
r2 <- r 
r2[setdiff(seq_len(ncell(r2)), c(2, 8, 9, 11, 14))] <- NA 
r2[!is.na(r2)] <- 1 
plot(rasterToPolygons(r2, dissolve=TRUE), add=TRUE, border='red', lwd=2) 

在这里,我们创建然后焦点细胞光栅的一个副本,所有其他细胞设定为NA,以及共同的值(1 , 在这种情况下)。 rasterToPolygons然后将非NA细胞转换成多边形,如果需要,溶解接触多边形。

fig3

2

这段代码应该做你想做的。

plot(raster(matrix(seq(1,15), nrow = 3))) 

gridx = 5 
gridy = 3 
dx = 1/gridx #resolution of the grid 
dy = 1/gridy 

# if you want to specify the cell number (cell 1 is bottom left): 

cell = 15 
ny = floor(cell/gridx - dx)+1 
nx = cell-gridx*(ny-1) 

# if you want to give cell positions, just edit nx, ny 

x1 = c(nx-1,nx-1,nx-1,nx)*dx 
y1 = c(ny-1,ny,ny-1,ny-1)*dy 
x2 = c(nx,nx,nx-1,nx)*dx 
y2 = c(ny-1,ny,ny,ny)*dy 

segments(x1,y1,x2,y2,col=2,lwd=2) 
+0

谢谢 - 并不完全做到这一点通过手机号码 - 但很容易的找出该行和col(nx和ny)给出的单元格数值。 – Sarah 2015-02-08 22:51:29

+1

编辑要通过单元格编号 – xraynaud 2015-02-09 07:49:06

相关问题