我有一组林分(SpatialPolygonDataFrame)随机分布在景观上,即分散和聚集。对于每个多边形,我想确定它是否有开放的边缘。 多边形具有开放边缘 如果:

  • 没有邻居
  • 至少在一侧没有邻居;
  • 有邻居,
    但是立柱和立柱之间的树高差
    邻居超过 5

  • 我徘徊如何将属性 open_edge = TRUE/FALSE 添加到单个多边形?在 raster 包中,使用 moving window 是一种很有前途的方法。但是,我的原始数据是要素类,不幸的是不像工作示例那样栅格化。

    我想( 伪代码 ):
  • 子集每一个都一一对应(在 for 循环中)
  • 创建周围缓冲区
  • 子集围绕备用 buffer 与备用
  • 的重叠
  • 如果有邻居 -> 比较高度。如果差 > 5,open_edge = TRUE

  • 但是,这种方法没有考虑展台有什么,比如说只有 3 边的邻居,即车邻居。 poly2nb 工具看起来很有前途,但如何为单个展台添加属性?

    R gis : add polygon attribute based on neighbors value?-LMLPHP

    这是我的虚拟方法,但我想知道您是否有更有效的解决方案?

    创建虚拟数据:
    library(ggplot2)  # for choropleth map plot
    library(broom) # to convert spatial data to dataframe
    library(mapproj)
    library(spdep)    # neighbours
    library(rgdal)
    library(rgeos)
    library(sf)
    library(raster)
    library(dplyr)
    library(spData)
    library(sf)
    
    r <- raster(nrow=6, ncol=6, crs = "+init=epsg:2957")
    values(r) <- matrix(data = c(9,  NA, NA, NA, NA, NA,
                                 NA, NA, NA, NA, NA, NA,
                                 NA, NA, NA, NA, NA, NA,
                                 NA, NA, NA, 1, 1, 1,
                                 NA, NA, NA, 1, 9, 1,
                                 NA, NA, NA, 1, 1, 1),
                        nrow = 6,
                        ncol = 6,
                        byrow = TRUE)
    
    # Convert raster to polygon
    polys <- rasterToPolygons(r)
    

    确定支架是否有开放边缘,以一个支架为例:
    # Subset first row in SpatialPolygonDataFrame
    i = 10
    one = polys[i, ]
    
    # Keep the remaining polygons
    left = polys[-i,]
    
    # Create buffer within distance
    buff = buffer(one, width = 100)
    
    # subset set of neighbours by spatial overlap
    nbrs <- left[which(gContains(sp::geometry(buff),
                                        sp::geometry(left), byid = T)),
    # Compare if the values are different
    height.one  = rep(one$layer[1], nrow(nbrs))
    height.nbrs = nbrs$layer
    
    # Get the differences between the neighbouring stands
    difference = height.one - height.nbrs
    
    # If the difference in at least one stand is
    # in more than 5, set open_edge = TRUE
    # or if no neighbours find
    one$open_edge <- any(difference > 5)
    

    最佳答案

    开始使用 spdep::poly2nb

    library(raster)
    
    r <- raster(nrow=6, ncol=6, crs = "+init=epsg:2957")
    values(r) <- matrix(data = c(9,  NA, NA, NA, NA, NA,
                                 NA, NA, NA, NA, NA, NA,
                                 NA, NA, NA, NA, NA, NA,
                                 NA, NA, NA, 1, 1, 1,
                                 NA, NA, NA, 1, 9, 1,
                                 NA, NA, NA, 1, 1, 1),
                        nrow = 6,
                        ncol = 6,
                        byrow = TRUE)
    
    # Convert raster to polygon
    polys <- rasterToPolygons(r)
    
    library(spdep)
    nb <- poly2nb(polys)
    
    plot(polys)
    text(polys, 1:length(polys))
    
    str(nb)
    #List of 10
    # $ : int 0
    # $ : int [1:3] 3 5 6
    # $ : int [1:5] 2 4 5 6 7
    # $ : int [1:3] 3 6 7
    # ...
    

    因此,poly 1 没有邻居,poly 2 有邻居 3、5、6 等。

    现在您可以使用 sapply 来计算事物。例如邻居的数量
    nbcnt <- sapply(nb, function(i) length(i[i>0]))
    nbcnt
    #[1] 0 3 5 3 5 8 5 3 5 3
    

    将此添加回多边形
    polys$nbcnt <- nbcnt
    

    关于R gis : add polygon attribute based on neighbors value?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59878623/

    10-12 21:22