【发布时间】: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')"
我尝试对函数和其中的循环进行不同的更改。理想情况下,我什至会得到一个可以立即执行的函数或循环,而不必像我在这里尝试的那样每年都执行。我这样做是因为我越来越接近以这种方式完成它,但我知道这不是最有效的方法。
任何帮助都会很棒。 谢谢!
【问题讨论】: