问题
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...
Flood data:
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"
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...
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}
And repeated this for every value of cycles
.
I'm getting different errors, like: Error in cycle == "NA-2007" : comparison (1) is possible only for atomic and list types
.
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
}
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!
来源:https://stackoverflow.com/questions/66098097/test-intersection-of-two-multipolygons-based-on-year-cycles-in-r