【问题标题】:Get percentage of overlap between two multipolygons in R获取R中两个多面体之间的重叠百分比
【发布时间】:2023-03-03 12:25:02
【问题描述】:

我有两个 MULTYPOLYGONS,其中一行是选举区*年份对,另一个是洪水事件的集合。

 district.df
      year ward_ons     cycle                       geometry
    1 2007       E1   NA-2007 POLYGON ((527370.8 183470.7...
    2 2008       E1 2007-2008 POLYGON ((528891.1 182192.6...
    3 2009       E2   NA-2009 POLYGON ((370294.2 414678.7...
    4 2010       E3   NA-2010 POLYGON ((375025.4 414992.1...
    5 2011       E3 2010-2011 POLYGON ((375150.8 410809.8...
    6 2018       E3 2011-2018 POLYGON ((373286.3 414364.5...
    7 2007       E4   NA-2007 POLYGON ((373168.6 411597.8...
    8 2010       E4 2007-2010 POLYGON ((374783.2 406209.4...

洪水数据:

    flood.df
    Simple feature collection with 8 features and 2 fields
geometry type:  GEOMETRY
dimension:      XY
bbox:           xmin: 317656.2 ymin: 90783.2 xmax: 546460.6 ymax: 631125.7
projected CRS:  OSGB 1936 / British National Grid
  year            name                       geometry
1 2007      River 2007 POLYGON ((359637.7 268239.7...
2 2007       Tank 2007 POLYGON ((325444.1 92717.57...
3 2008  Yorkshire 2008 POLYGON ((318550.7 103058.8...
4 2009 Flood East 2009 POLYGON ((541472.6 112593, ...
5 2010  Occurence 2010 MULTIPOLYGON (((545863.4 11...
6 2012      Storm 2012 POLYGON ((473637.4 103927, ...
7 2011      Flood 2011 MULTIPOLYGON (((524617.6 42...
8 2017      River 2017 POLYGON ((393387.6 631125.7...

我想要做的是在选举区多面体中获得一列,该列与洪水多面体中的 any 多边形重叠,@ 的值相同987654324@.

这是我尝试过的(受https://gis.stackexchange.com/questions/140504/extracting-intersection-areas-in-r影响)

# Create function
overlap.fraction <- function(election, flood, yeari){
  flood.year <- flood[flood$year == yeari,]
  election.year <- election[election$year == yeari, ]
for (i in 1:nrow(election.year)){
  int.i <- as_tibble(st_intersection(i, flood.year))
  int.i$affected <- st_area(int.i$geoms)
  affected.by.county(paste0(yeari)) <- int.i%>%
  dplyr::group_by(code) %>%
  dplyr::summarise(affected.area = sum(affected))
}
}

然后试了一下

i.1990 <- overlap.fraction(district.df, flood.df, yeari = 1990)

但不断得到:

Error in UseMethod("st_intersection") : no applicable method for 'st_intersection' applied to an object of class "c('integer', 'numeric')"

我尝试对函数和其中的循环进行不同的更改。理想情况下,我什至会得到一个可以立即执行的函数或循环,而不必像我在这里尝试的那样每年都执行。我这样做是因为我越来越接近以这种方式完成它,但我知道这不是最有效的方法。

任何帮助都会很棒。 谢谢!

【问题讨论】:

    标签: r geometry gis raster sf


    【解决方案1】:

    让我们创建一个函数来按年份

    library(sf)
    
    ## storing years in a variable
    years <- unique(district.df$year)
    
    ## creating an "auxiliary" geometry
    flood.df$geom_2 <- st_geometry(flood.df)
    
    ## function to calculate this "proportion of intersection"
    ## inputs: y = year; dt1 = dataset1 (district); dt2 = dataset2(flood)
    prop_intersect <- function(y, dt1, dt2) {
      ## filtering year
      out1 <- dt1[dt1$year == y,]
      out2 <- dt2[dt2$year == y,]
    
      ## calculating the areas for the first dataset
      out1 <- transform(out1, dist_area = as.numeric(st_area(geometry)))
    
      ## joining the datasets (polygons that intersect will be joined).
      ## It would be nice to have id variables for both datasets
      output <- st_join(
        x    = out1,
        y    = out2,
        join = st_intersects
      )
    
      ## calculating area of intersection
      output <- transform(output, 
                          inter_area = mapply(function(x, y) {
                                          as.numeric(sf::st_area(
                                               sf::st_intersection(x, y)
                                           ))}, x = geometry, y = geom_2))
    
      ## calculating proportion of intersected area
      output <- transform(output, prop_inter = inter_area/dist_area)
    
      return(output)
    }
    

    所以,现在我们有一个功能可以满足您的要求。很可能存在更好(更有效和“干净”)的方法来做到这一点。然而,这是我能想到的。另外,由于这不是reprex,我很难知道这段代码是否有效。

    话虽如此,现在我们可以按如下方式迭代“年份”

    final_df <- lapply(years, prop_intersect, dt1 = district.df, dt2 = flood.df)
    
    final_df <- do.call("rbind", final_df)
    

    【讨论】:

    • 非常感谢,不用担心它不是最有效和最干净的。只要它成功了,我就很高兴!在我的数据上运行该函数时,我不断收到以下信息:年份 == y 中的错误:比较 (1) 仅适用于原子和列表类型
    • 另外,很抱歉这不是一个代表。我对 shapefile 和 sf 很陌生,所以我发现在这里很难复制几何图形,欢迎任何提示
    • 不用担心不是reprex,有时它会发生。嗯,这很奇怪。我不确定发生了什么事。您是否有任何可以共享的数据子集?也许在您的全局环境中还有另一个名为 year 的变量。
    • @AntVal unique(district.df$year) 返回什么?
    • 谢谢!非常感谢所有的帮助。救命!万事如意
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多