根据R中的年周期测试两个MULTIPOLYGONS的交集 [英] Test intersection of two MULTIPOLYGONS based on year cycles in R
问题描述
我有两个多面体,我想根据年份来测试其几何之间的交集.基本上,我有一个包含洪水事件及其几何形状的洪水多面体,以及一个选举数据集,该选举数据集的每次选举都以病房*年为单位,其中包含该病房的几何形状.我想看看每次选举前每个周期的选区有没有交叉路口.因此,如果选举在2009年进行,周期是2007-2009年,我想看看其病房在2007年,08年还是09年被淹.
I have two multipolygons and I want to test intersections between their geometries based on groups of years. Basically I have a flood multipolygon that contains flood events and their geometry and an election dataset which has each election as ward*year units, containing the geometry of that ward. I want to see if there are any intersections in the electoral ward each cycle prior to each election. So if the election was in 2009 and the cycle was 2007-2009 I want to see if its ward was flooded in 2007, 08 or 09.
election.df
election.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...
洪水数据:
df.floods
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...
整个选举数据帧的周期唯一值是:
The cycles' unique values for the entire election dataframe are these:
df.election$cycle%>% unique()
[1] "NA-2007" "NA-2008" "2007-2008" "NA-2009" "2008-2009" "2007-2009" "NA-2010" "2009-2010" "2008-2010" "2007-2010" "2010-2011" "2007-2011"
[13] "2008-2011" "2009-2011" "NA-2011" "2010-2012" "2011-2012" "NA-2012" "2008-2012" "2009-2012" "2007-2012" "2010-2013" "2012-2013" "2011-2013"
[25] "2007-2013" "NA-2013" "2009-2013" "2010-2014" "2012-2014" "2011-2014" "NA-2014" "2013-2014" "2014-2015" "2012-2015" "2011-2015" "NA-2015"
[37] "2013-2015" "2007-2015" "2009-2015" "2014-2016" "2015-2016" "2012-2016" "NA-2016" "2011-2016" "2013-2016" "2016-2017" "2015-2017" "2013-2017"
[49] "2009-2017" "NA-2017" "2012-2017" "2008-2017" "2014-2018" "2016-2018" "2017-2018" "2012-2018" "2010-2018" "2015-2018" "NA-2018" "2007-2018"
循环中的NA值表示之前没有选举.在那些情况下,我希望它仅对当年进行评估.因此,如果周期为NA-2015,我希望它测试该病房在2015年是否被洪水淹没.我希望每个选举*年份的 flood
值都为 1
,如果在其 cycle
值的年份中存在交集,并且 0
(如果不是).
The NA values in cycle mean that there is no election prior to it. In those cases I want it to evaluate just for that year. So if the cycle is NA-2015 I want it to test if that ward was flooded in 2015.
I want each election*year to have a value for flood
that is 1
if there was an intersection during the years of its cycle
value and a 0
if not.
因此理想的结果应如下所示:
So the ideal outcome would be something like the following:
ideal.df
Simple feature collection with 8 features and 4 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: 368816.4 ymin: 181032 xmax: 528891.1 ymax: 416703.1
projected CRS: OSGB 1936 / British National Grid
year ward cycle flood geometry
1 2007 E1 NA-2007 1 POLYGON ((527370.8 183470.7...
2 2008 E1 2007-2008 0 POLYGON ((528891.1 182192.6...
3 2009 E2 NA-2009 1 POLYGON ((370294.2 414678.7...
4 2010 E3 NA-2010 0 POLYGON ((375025.4 414992.1...
5 2011 E3 2010-2011 1 POLYGON ((375150.8 410809.8...
6 2018 E3 2011-2018 0 POLYGON ((373286.3 414364.5...
7 2007 E4 NA-2007 0 POLYGON ((373168.6 411597.8...
8 2010 E4 2007-2010 0 POLYGON ((374783.2 406209.4...
为此,我尝试使用 st_intersects
进行了几次循环,该循环基本上测试了两个几何图形是否相交.
I tried several loops for this, using st_intersects
which basically tests whether two geometries intersect.
for(i in 1:nrow(votes.sp) {
if(cycle =="NA-2007") int = st_intersects(recorded.full[recorded.full$year == 2007, ], i, sparse = FALSE) else
if(cycle =="2007-2008") int = st_intersects(recorded.full[recorded.full$year%in% c(2007, 2008), ], i, sparse = FALSE) else
int = FALSE}
并对每个 cycles
的值重复此操作.
And repeated this for every value of cycles
.
我遇到了不同的错误,例如:周期错误=="NA-2007";:比较(1)仅适用于原子类型和列表类型
.
I'm getting different errors, like: Error in cycle == "NA-2007" : comparison (1) is possible only for atomic and list types
.
我还尝试创建一个名为 lag.year2
的循环中具有最低值的新变量,并执行以下循环:
I also tried creating a new variable with the lowest value in the cycle called lag.year2
and this loop:
for(row in nrow(df.election)) {
rec_sub = st_union(subset(df.floods, year<= row$year & year>=row$lag.year2))
int = st_intersects(
n,
rec_sub,
sparse = FALSE
)
if(any(int)) df.election$flood.cycle[n]= int[ ,1] else df.election$flood.cycle[n] = FALSE
}
但是它也不起作用,我得到: row $ year中的错误:$运算符对于原子向量无效
But it isn't working either, I get: Error in row$year : $ operator is invalid for atomic vectors
我尝试了各种各样的事情.真的非常感谢您的帮助!
I've tried all sorts of things. Would really, really appreciate any help!
推荐答案
首先,让我们创建一个"lag.year"最后一个值为 year
的列,即 cycle
的最小值:
First, let's create a "lag.year" column that takes the last value of year
i.e. the lowest value of cycle
:
library(tidyverse)
library(sf)
df.elections <- df.elections%>%
group_by(code)%>%
arrange(year)%>%
mutate(lag.year = dplyr::lag(year))%>%
mutate(lag.year = ifelse(is.na(lag.year), year, lag.year))
现在,基于此,让我们循环进行我们打算做的事情:
Now, based on this, let's for loop what we intent to do:
for (i in 1:nrow(el9018_geo2)){
#for every row
(el9018_geo2$flood.cycle[i] = st_intersects(
el9018_geo2[i, ],
df.floods[which(df.floods$year %in% df.elections$lag.year[i]:df.elections2$year[i]), ],
sparse = FALSE
) %>% any(na.rm = T))
#test the intersection of any row in df.flood that is between values of lag.year and year.
if((i %% 25) == 0) cat(".")
# if(any(int)) el9018_simp$flood.cycle[i] = int[ ,1] else el9018_simp$flood.cycle[i] = FALSE
}
为我做到了.
这篇关于根据R中的年周期测试两个MULTIPOLYGONS的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!