如果光栅值NA搜索并提取最近的非NA像素

时间:2014-12-19 08:17:06

标签: r r-raster

在将栅格的值提取到点时,我发现我有几个NA,而不是使用buffer的{​​{1}}和fun个参数我希望将最近的非extract像素提取到与NA重叠的点。

我正在使用基本提取功能:

NA

3 个答案:

答案 0 :(得分:6)

这是一个不使用缓冲区的解决方案。但是,它会为数据集中的每个点单独计算距离图,因此如果数据集很大,它可能无效。

set.seed(2)

# create a 10x10 raster
r <- raster(ncol=10,nrow=10, xmn=0, xmx=10, ymn=0,ymx=10)
r[] <- 1:10
r[sample(1:ncell(r), size = 25)] <- NA

# plot the raster
plot(r, axes=F, box=F)
segments(x0 = 0, y0 = 0:10, x1 = 10, y1 = 0:10, lty=2)
segments(y0 = 0, x0 = 0:10, y1 = 10, x1 = 0:10, lty=2)

# create sample points and add them to the plot
xy = data.frame(x=runif(10,1,10), y=runif(10,1,10))
points(xy, pch=3)
text(x = xy$x, y = xy$y, labels = as.character(1:nrow(xy)), pos=4, cex=0.7, xpd=NA)

# use normal extract function to show that NAs are extracted for some points
extracted = extract(x = r, y = xy)

# then take the raster value with lowest distance to point AND non-NA value in the raster
sampled = apply(X = xy, MARGIN = 1, FUN = function(xy) r@data@values[which.min(replace(distanceFromPoints(r, xy), is.na(r), NA))])

# show output of both procedures
print(data.frame(xy, extracted, sampled))

#          x        y extracted sampled
#1  5.398959 6.644767         6       6
#2  2.343222 8.599861        NA       3
#3  4.213563 3.563835         5       5
#4  9.663796 7.005031        10      10
#5  2.191348 2.354228        NA       2
#6  1.093731 9.835551         2       2
#7  2.481780 3.673097         3       3
#8  8.291729 2.035757         9       9
#9  8.819749 2.468808         9       9
#10 5.628536 9.496376         6       6

答案 1 :(得分:1)

这是一种基于栅格的解决方案,首先使用最近的非NA像素值填充NA像素。 但请注意,这并未考虑像素内点的位置。相反,它会计算像素中心之间的距离,以确定最近的非NA像素。

首先,它为每个NA光栅像素计算距离最近的非NA像素的距离和方向。下一步是计算这个非NA单元格的坐标(假设投影CRS),提取其值并将该值存储在NA位置。

开始数据:投影栅格,其值与koekenbakker中的answer相同:

set.seed(2)
# set projected CRS
r <- raster(ncol=10,nrow=10, xmn=0, xmx=10, ymn=0,ymx=10, crs='+proj=utm +zone=1') 
r[] <- 1:10
r[sample(1:ncell(r), size = 25)] <- NA

# create sample points
xy = data.frame(x=runif(10,1,10), y=runif(10,1,10))

# use normal extract function to show that NAs are extracted for some points
extracted <- raster::extract(x = r, y = xy)

计算从所有NA像素到最近的非NA像素的距离和方向:

dist <- distance(r)  
# you can also set a maximum distance: dist[dist > maxdist] <- NA
direct <- direction(r, from=FALSE)

检索NA像素的坐标

# NA raster
rna <- is.na(r) # returns NA raster

# store coordinates in new raster: https://stackoverflow.com/a/35592230/3752258 
na.x <- init(rna, 'x')
na.y <- init(rna, 'y')

# calculate coordinates of the nearest Non-NA pixel
# assume that we have a orthogonal, projected CRS, so we can use (Pythagorean) calculations
co.x <- na.x + dist * sin(direct)
co.y <- na.y + dist * cos(direct)

# matrix with point coordinates of nearest non-NA pixel
co <- cbind(co.x[], co.y[]) 

用坐标'co'

提取最近的非NA细胞的值
# extract values of nearest non-NA cell with coordinates co
NAVals <- raster::extract(r, co, method='simple') 
r.NAVals <- rna # initiate new raster
r.NAVals[] <- NAVals # store values in raster

使用新值填充原始栅格

# cover nearest non-NA value at NA locations of original raster
r.filled <- cover(x=r, y= r.NAVals)

sampled <- raster::extract(x = r.filled, y = xy)

# compare old and new values
print(data.frame(xy, extracted, sampled))

#           x        y extracted sampled
# 1  5.398959 6.644767         6       6
# 2  2.343222 8.599861        NA       3
# 3  4.213563 3.563835         5       5
# 4  9.663796 7.005031        10      10  
# 5  2.191348 2.354228        NA       3
# 6  1.093731 9.835551         2       2
# 7  2.481780 3.673097         3       3
# 8  8.291729 2.035757         9       9
# 9  8.819749 2.468808         9       9 
# 10 5.628536 9.496376         6       6

请注意,第5点采用的另一个值是Koekenbakker的answer,因为此方法没有考虑像素内点的位置(如上所述)。如果这很重要,则此解决方案可能不合适。在其他情况下,例如如果栅格单元格与点精度相比较小,则这种基于栅格的方法应该会产生良好的结果。

答案 2 :(得分:1)

对于光栅堆栈,请使用上面的@ koekenbakker解决方案,并将其转换为函数。光栅堆栈的@layers广告位是一个栅格列表,因此,请将其拉​​平并从那里开始。

#new layer
r2 <- raster(ncol=10,nrow=10, xmn=0, xmx=10, ymn=0,ymx=10)
r2[] <- 1:10
r2[sample(1:ncell(r2), size = 25)] <- NA

#make the stack
r_stack <- stack(r, r2)

#a function for sampling
sample_raster_NA <- function(r, xy){
  apply(X = xy, MARGIN = 1, 
        FUN = function(xy) r@data@values[which.min(replace(distanceFromPoints(r, xy), is.na(r), NA))])

}

#lapply to get answers
lapply(r_stack@layers, function(a_layer) sample_raster_NA(a_layer, xy))

还是想要(速度提升?)

purrr::map(r_stack@layers, sample_raster_NA, xy=xy)

这让我想知道是否可以使用dplyr加速整个事情......