3

Sample data

x <- raster(x=matrix(data=1:36, nrow=6), xmn=-1000, xmx=1000, ymn=-100, ymx=900)
x[c(8, 15, 16, 17, 22, 25, 26, 30, 31)] <- NA
plot(x)

enter image description here

The problem

How do I distinguish (algorithmically) the holes in the raster i.e., the area bounded by cells c(15:17, 22) from the other gaps that are not holes (i.e., the rest of the empty cells)?

This would make it possible to do operations only on the hole / island regions of the raster, fill holes with a custom value etc etc.

The actual rasters have around 30000 holes and therefore speed is important. I am interested in both R and Grass GIS solutions. Many thanks for your help, much appreciated !

4

3 回答 3

3

这是一个没有多边形化的解决方案:(它不优雅,但它有效)。但是,您必须将孔/岛重新分类为值(即 999)和所有其他非岛到 NA。像这样:

x <- raster(x=matrix(rep(NA,36), nrow=6), xmn=-1000, xmx=1000, ymn=-100, ymx=900)
x[c(8, 15, 16, 17, 22, 25, 26, 30, 31)] <- 999

plot(x)

初始虚拟栅格

现在我使用这个clump()函数来检查是否有岛,这个函数很酷的是,它还返回这些岛的 ID:

#Get Islands with IDs
cl <- clump(x,directions=8)
plot(cl)

带有 ID 的丛/岛

然后我根据岛屿的频率创建一个数据框(这只是为了获取每个岛屿的 ID)

freqCl <- as.data.frame(freq(cl))

#remove the (row) which corresponds to the NA values (this is important for the last step)
freqCl <- freqCl[-which(is.na(freqCl$value)),]

检查是否有任何岛屿接触边界:

#Check if the island touches any border and therefore isn't a "real island" (first and last column or row)

noIslandID <- c()
#First row
if(any(rownames(freqCl) %in% cl[1,])){
  eliminate   <- rownames(freqCl)[rownames(freqCl) %in% cl[1,]]
  noIslandID  <- append(noIslandID, eliminate)
}
#Last row
if(any(rownames(freqCl) %in% cl[nrow(cl),])){
  eliminate  <- rownames(freqCl)[rownames(freqCl) %in% cl[nrow(cl),]]
  noIslandID <- append(noIslandID, eliminate)
}
#First col
if(any(rownames(freqCl) %in% cl[,1])){
  eliminate   <- rownames(freqCl)[rownames(freqCl) %in% cl[,1]]
  noIslandID  <- append(noIslandID, eliminate)
}
#Last col
if(any(rownames(freqCl) %in% cl[,ncol(cl)])){
  eliminate   <- rownames(freqCl)[rownames(freqCl) %in% cl[,ncol(cl)]]
  noIslandID  <- append(noIslandID, eliminate)
}

消除那些接触边界的岛屿:

noIslandID <- unique(noIslandID)
IslandID   <- setdiff(rownames(freqCl), noIslandID)

在最后一步,为初始栅格中的每个“真实岛屿”分配 1:

for(i in 1:length(IslandID)) {
  x[cl[]==as.numeric(IslandID[i])] <- 1
}

plot(x)

在此处输入图像描述

于 2016-12-23T23:40:05.627 回答
2

多边形化呢?对于速度,我不知道它的价值,但你可以:

x[!is.na(values(x))]<-1
plot(x)
x[is.na(values(x))]<-0

hole <- rasterToPolygons(x, fun=NULL, n=4, na.rm=TRUE, digits=12, dissolve=T)

现在你必须将你的单部分多边形变成多部分:

hole2 <- SpatialPolygons(lapply(hole@polygons[[1]]@Polygons, function(xx) Polygons(list(xx),ID=round(runif(1,1,100000000)))))

plot(hole2, add=T)

现在你找到了“真正的”洞,这些洞没有接触到边界

around <- as(extent(x), "SpatialLines")
touch_border <- gIntersects(around, hole2, byid=T) 
extract(x, hole2[!touch_border,],cellnumbers=T)

这会给你一个孔的细胞数。它找到了单元格“8”,你不是说它是一个洞,所以我不确定它是否正确,但它一定非常接近!

如果在 R 中速度很慢,请在 GRASS 中执行相同的算法(主要是rasterToPolygons调用)

于 2016-12-22T19:57:48.187 回答
0

在这种情况下,如果一个单元格在四个基本方向的每一个上都由至少一个非缺失值界定,则该单元格位于一个洞中。您可以使用下面的代码对此进行测试,该代码还将缺失值替换为 99。

x2 <- x                        ## Create a copy of the raster
notNA <- !is.na(as.matrix(x))  ## Matrix identifying cells that are not NA

nr <- nrow(x) ## Number of rows
nc <- ncol(x) ## Number of columns

for(i in 2:(nr-1)) {       ## Loop over rows, ignoring first and last
    for(j in 2:(nc-1)) {   ## Loop over columns, ignoring first and last
        if(notNA[i,j]) ## Skip cell if not NA
            next
        ## For NA cells, determine if non-NA cell exists in each cardinal direction
        any.north <- any(notNA[1:(i-1),j])
        any.south <- any(notNA[(i+1):nr,j])
        any.west <- any(notNA[i,1:(j-1)])
        any.east <- any(notNA[i,(j+1):nc])
        if(any.north & any.south & any.west & any.east)
            x2[i,j] <- 99 ## If cell is in hole, repalce with new value
    }
}

这是新的栅格:

用值 99 填充孔的新栅格

于 2019-12-28T19:15:56.420 回答