2017-08-01 71 views
3

我的以下问题建立在这个帖子上提出@jbaums解决方案:Global Raster of geographic distances如何基于网格细胞子集栅格值

为了再现例子的目的,我有距离到的栅格数据集最近的海岸线:

library(rasterVis); library(raster); library(maptools) 
data(wrld_simpl) 

# Create a raster template for rasterizing the polys. 
r <- raster(xmn=-180, xmx=180, ymn=-90, ymx=90, res=1) 
# Rasterize and set land pixels to NA 
r2 <- rasterize(wrld_simpl, r, 1) 
r3 <- mask(is.na(r2), r2, maskvalue=1, updatevalue=NA) 
# Calculate distance to nearest non-NA pixel 
d <- distance(r3) # if claculating distances on land instead of ocean: d <- distance(r3) 
# Optionally set non-land pixels to NA (otherwise values are "distance to non-land") 
d <- d*r2 
levelplot(d/1000, margin=FALSE, at=seq(0, maxValue(d)/1000, length=100),colorkey=list(height=0.6), main='Distance to coast (km)') 

的数据是这样的: output plot

从这里,我需要子集距离栅格(d),或创建一个新的光栅,仅包含细胞距离海岸线不到200公里。我试图使用getValues()来确定值为< = 200(如下所示)的单元格,但目前为止没有成功。谁能帮忙?我在正确的轨道上吗?

#vector of desired cell numbers 
my.pts <- which(getValues(d) <= 200) 
# create raster the same size as d filled with NAs 
bar <- raster(ncols=ncol(d), nrows=nrow(d), res=res(d)) 
bar[] <- NA 
# replace the values with those in d 
bar[my.pts] <- d[my.pts] 

回答

3

我认为这是你在找什么,你可以把一个光栅喜欢这里矩阵右后你d <- d*r2行:

d[d>=200000]<-NA 
levelplot(d/1000, margin=FALSE, at=seq(0, maxValue(d)/1000, length=100),colorkey=list(height=0.6), main='Distance to coast (km)') 

(如果你忘了:在单位是米所以门槛应该是200000,而不是200)

+0

太棒了!有用。谢谢@Ouistiti! – fabfab