创建矩阵,显示许多多边形之间重叠的比例

发布于 2025-01-24 22:31:26 字数 1020 浏览 2 评论 0原文

我有一个多边形列表,我需要知道多边形组合的重叠的比例。换句话此计算。我该怎么做?另外,我该提供什么使这篇文章有帮助?

这是我用来计算单个多边形列表重叠的代码的一个示例(处理)。是否可以调整它来创建每个poly计算的矩阵?

###################################################
#read in and create list of polygons
home.names <- list.files("", 
                         pattern="*.shp", recursive=T, full.names=T)
home.names


#####################################################
#create list to hold results and loop home-range overlap calculations
all.home.ranges <- list()

#loop
for (i in 1:length(home.names)){
  home.range <- st_read(home.names[i])
  home.range.t <- st_transform(home.range,'+proj=utm +zone=14 
        +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
  home.range$Area.ha <- st_area(home.range.t)*0.0001
  pi <- st_intersection(treatment, home.range.t) # treatment is a polygon
  if(nrow(pi) != 0){
    home.range$Treat.Overlap.per <- as.numeric(st_area(pi) / st_area(home.range.t) *100)
  } else {
    home.range$Treat.Overlap.per <- 0
  }

I have a list of polygons and I need to know the proportion of overlap of each combination of polygons. In other words proportion of over lap of poly1 to poly2, poly3,... and then poly2 to poly3, poly4,... I have found ways to do this for 2 polygons, but I would like to avoid doing a bunch of pairwise calculations. How can I accomplish this? Also, what can I provide to make this post helpful?

This is an example of code that I used to calculate proportion of overlap of a list of polygons on a single polygon (treatment). Can it be adapted to create a matrix of each poly to poly calculation?

###################################################
#read in and create list of polygons
home.names <- list.files("", 
                         pattern="*.shp", recursive=T, full.names=T)
home.names


#####################################################
#create list to hold results and loop home-range overlap calculations
all.home.ranges <- list()

#loop
for (i in 1:length(home.names)){
  home.range <- st_read(home.names[i])
  home.range.t <- st_transform(home.range,'+proj=utm +zone=14 
        +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
  home.range$Area.ha <- st_area(home.range.t)*0.0001
  pi <- st_intersection(treatment, home.range.t) # treatment is a polygon
  if(nrow(pi) != 0){
    home.range$Treat.Overlap.per <- as.numeric(st_area(pi) / st_area(home.range.t) *100)
  } else {
    home.range$Treat.Overlap.per <- 0
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2025-01-31 22:31:26

一种方法是构建具有2列的矩阵。例如,如果您有5个polys:

i <- rep(1:5, each=5)
j <- rep(1:5, 5)
comp <- cbind(i, j)[i < j, ]
comp
#       i j
#  [1,] 1 2
#  [2,] 1 3
#  [3,] 1 4
#  [4,] 1 5
#  [5,] 2 3
#  [6,] 2 4
#  [7,] 2 5
#  [8,] 3 4
#  [9,] 3 5
# [10,] 4 5

loop thun comp,并使用ij选择两个多粒子进行比较。没有可重现的数据,我将无法进行调试,但是类似:

for (k in 1:nrow(comp){
  home.range <- st_read(home.names[comp[k, 1]])
  home.range.t <- st_transform(home.range,'+proj=utm +zone=14 
        +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
  home.range$Area.ha <- st_area(home.range.t)*0.0001

  home.range2 <- st_read(home.names[comp[k, 2]])
  home.range.t2 <- st_transform(home.range2,'+proj=utm +zone=14 
        +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
  home.range2$Area.ha <- st_area(home.range.t2)*0.0001

  pi <- st_intersection(home.range.t, home.range.t2)
  if(nrow(pi) != 0){
    home.range$Treat.Overlap.per <- as.numeric(st_area(pi) / st_area(home.range.t) *100)
  } else {
    home.range$Treat.Overlap.per <- 0
  }

主要区别是重叠的百分比可能基于home.range和home.range2。您可以根据计划对结果的计划进行计算。

One approach is to construct a matrix with 2 columns. For example, if you have 5 polys:

i <- rep(1:5, each=5)
j <- rep(1:5, 5)
comp <- cbind(i, j)[i < j, ]
comp
#       i j
#  [1,] 1 2
#  [2,] 1 3
#  [3,] 1 4
#  [4,] 1 5
#  [5,] 2 3
#  [6,] 2 4
#  [7,] 2 5
#  [8,] 3 4
#  [9,] 3 5
# [10,] 4 5

Loop though comp and use i and j to select the two polys for that comparison. Without reproducible data I can't debug this but something like:

for (k in 1:nrow(comp){
  home.range <- st_read(home.names[comp[k, 1]])
  home.range.t <- st_transform(home.range,'+proj=utm +zone=14 
        +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
  home.range$Area.ha <- st_area(home.range.t)*0.0001

  home.range2 <- st_read(home.names[comp[k, 2]])
  home.range.t2 <- st_transform(home.range2,'+proj=utm +zone=14 
        +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
  home.range2$Area.ha <- st_area(home.range.t2)*0.0001

  pi <- st_intersection(home.range.t, home.range.t2)
  if(nrow(pi) != 0){
    home.range$Treat.Overlap.per <- as.numeric(st_area(pi) / st_area(home.range.t) *100)
  } else {
    home.range$Treat.Overlap.per <- 0
  }

The main difference is that the percent overlap could be based on both home.range and home.range2. You could compute both depending on what you are planning to do with the results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文