[英]Create polygons encompassing specific raster values
I have 4 raster layers contained in a raster stack and want to generate polygons that encompass all cells of a specified value. 我在栅格堆栈中包含4个栅格图层,并希望生成包含指定值的所有像元的多边形。
The raster
package can be use to generate an example data set. raster
包可用于生成示例数据集。
library(raster)
filename <- system.file("external/test.grd", package="raster")
r <- raster(filename)
Like the raster below, my real data are akin to maps of animal habitat and have a patchy distribution of 'good' and 'bad' areas. 像下面的栅格一样,我的真实数据类似于动物栖息地的地图,并且有“好”和“坏”区域的零星分布。
To more closely mirror my real data, we can then add a bit of variation to three other rasters and make a stack. 为了更紧密地镜像我的真实数据,我们可以向其他三个栅格添加一些变化并形成一个堆栈。
s <- stack(r, r+250, r-250, r+100)
Working with the stack s
is it possible to create polygons that surround all cells less than 300 in all stack layers? 使用堆栈
s
是否可以创建包围所有堆栈层中所有小于300的单元的多边形?
As an extension, my end goal is to then calculate the area (or percent) overlap between the resulting polygons. 作为扩展,我的最终目标是然后计算所得多边形之间的面积(或百分比)重叠。
Any suggestions (specific or general) would be greatly appreciated. 任何建议(具体或一般)将不胜感激。
Since you're working with a raster stack, all your cells should have the same area. 由于您使用的是栅格堆栈,因此所有像元都应具有相同的面积。 In that case, I don't think you need to use polygons at all.
在那种情况下,我认为您根本不需要使用多边形。 (Note that I adjusted your example data a bit.)
(请注意,我对示例数据进行了一些调整。)
library(raster)
filename <- system.file("external/test.grd", package="raster")
r <- raster(filename)
s <- stack(r, r + 50, r - 50, r + 100)
# Create a new raster stack with results of a logical test
s2 <- s < 300
# Create a raster indicating which cells of the new stack
# have values that are all TRUE
r2 <- sum(s2) == length(unstack(s2))
# Multiply by the area of a single cell
r3 <- r2 * area(r2)[1]
# Sum the area for all raster values
sum(values(r3), na.rm = TRUE)
## 124800
If you'd like to use polygons and your rasters aren't too large, converting the stack to a SpatialPolygonsDataFrame
should be pretty quick. 如果您想使用多边形并且栅格不是太大,那么将堆栈转换为
SpatialPolygonsDataFrame
应该很快。 Here's an analogous method that yields the same result: 这是一个产生相同结果的类似方法:
# Create a new raster stack with results of a logical test
s2 <- s < 300
# Convert to sp object
spdf <- as(s2, "SpatialPolygonsDataFrame")
# Index to the rows/features where the values in s2 were all TRUE
spdf2 <- spdf[which(rowSums(spdf@data) == length(unstack(s))), ]
rgeos::gArea(spdf2)
## 124800
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.