基于R中的年周期测试两个MULTIPOLYGONS的交集
Test intersection of two MULTIPOLYGONS based on year cycles in R
我有两个多面体,我想根据年份组测试它们的几何图形之间的交集。
基本上,我有一个洪水多面体,其中包含洪水事件及其几何形状,以及一个选举数据集,其中每次选举都以病房 * 年为单位,包含该病房的几何形状。我想看看每次选举前每个周期的选举区是否有任何交叉路口。所以如果选举是在2009年,周期是2007-2009我想看看它的病房是在2007年,08年还是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...
整个选举数据帧的循环唯一值是这些:
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
的值,如果在其 cycle
值的年份有交集,则为 1
,如果没有,则为 0
。
所以理想的结果应该是这样的:
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
基本上测试两个几何图形是否相交。
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
的每个值重复此操作。
我遇到了不同的错误,例如:Error in cycle == "NA-2007" : comparison (1) is possible only for atomic and list types
。
我还尝试在名为 lag.year2
的循环和这个循环中创建一个具有最低值的新变量:
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
}
但它也不起作用,我得到:Error in row$year : $ operator is invalid for atomic vectors
我试过各种各样的东西。真的非常感谢任何帮助!
首先,让我们创建一个“lag.year”列,它采用 year
的最后一个值,即 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))
现在,基于此,让我们 for 循环我们打算做的事情:
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
}
这对我有用。
我有两个多面体,我想根据年份组测试它们的几何图形之间的交集。 基本上,我有一个洪水多面体,其中包含洪水事件及其几何形状,以及一个选举数据集,其中每次选举都以病房 * 年为单位,包含该病房的几何形状。我想看看每次选举前每个周期的选举区是否有任何交叉路口。所以如果选举是在2009年,周期是2007-2009我想看看它的病房是在2007年,08年还是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...
整个选举数据帧的循环唯一值是这些:
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
的值,如果在其 cycle
值的年份有交集,则为 1
,如果没有,则为 0
。
所以理想的结果应该是这样的:
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
基本上测试两个几何图形是否相交。
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
的每个值重复此操作。
我遇到了不同的错误,例如:Error in cycle == "NA-2007" : comparison (1) is possible only for atomic and list types
。
我还尝试在名为 lag.year2
的循环和这个循环中创建一个具有最低值的新变量:
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
}
但它也不起作用,我得到:Error in row$year : $ operator is invalid for atomic vectors
我试过各种各样的东西。真的非常感谢任何帮助!
首先,让我们创建一个“lag.year”列,它采用 year
的最后一个值,即 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))
现在,基于此,让我们 for 循环我们打算做的事情:
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
}
这对我有用。