-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWG_Brecon_Broad.Rmd
598 lines (503 loc) · 28.5 KB
/
WG_Brecon_Broad.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
---
title: "CUUPeat: Brecon Beacons, Wales - Broad-scale bare peat mapping"
date: "15/09/2020"
author: 'JNCC'
licence: 'MIT Licence'
output:
html_document:
df_print: paged
css: style.css
includes:
before_body: header.html
pdf_document: default
always_allow_html: yes
---
```{r setup, include=FALSE,echo=F}
knitr::opts_chunk$set(echo = TRUE)
# Built with r 3.6.0
library(tmap)
library(raster)
library(dplyr)
library(sf)
library(stringr)
library(gdalUtils)
library(purrr)
library(JNCCsdms)
#package dependencies
# JNCCsdms_0.1.0 stringr_1.4.0
# purrr_0.3.4 gdalUtils_2.0.3.2
# tmap_3.0 sf_0.9-2
# dplyr_0.8.5 raster_3.0-7
# sp_1.4-2
```
<div class="mycontent">
CUU Peat Project - Welsh Government - Brecon Beacons, Wales
Broad-scale bare peat mapping processing
## Site location
```{r sitemap,message=F,warning=F}
#site boundaries
site <- sf::st_read('Breacon_Site_Boundaries.shp',quiet=T)
#S2 AOI
S2 <- sf::st_read('Proposed_S2_AOI.shp',quiet=T)
#APGB AOI
APGB <- sf::st_read('Proposed_APGB_AOI.shp',quiet=T)
#plot
tmap::tmap_mode("view")
tmap::tm_shape(site) + tmap::tm_borders(alpha=0.5) + tmap::tm_fill("green") +
tmap::tm_shape(S2) + tmap::tm_borders(alpha=1,col='blue') +
tmap::tm_shape(APGB) + tmap::tm_borders(alpha=1,col='pink')
```
## Preparing Variable layers
### DTM and slope
```{r dtmslopevars, eval = FALSE}
projfolder <- 'PeatlandCondition/'
#crop dtm to AOI
dtm <- raster::raster('UK_DEM_Composite_10m_WGS84.tif')
AOI= sf::st_read(paste0(projfolder,'Data/Site_AOIs/WG_Beacons/Proposed_S2_AOI.shp'),quiet=T)
# reproject AOI
AOI_WGS84 <- AOI %>% sf::st_transform(4326)
#crop to dtm
dtm_crop <-raster::crop(dtm,AOI_WGS84)
dtm_bng <- raster::projectRaster(dtm_crop, crs=sp::CRS('+init=epsg:27700'), method='bilinear')
raster::writeRaster(dtm_bng,paste0(projfolder,'WG_BreconBeacons/Variable_layers/WG_BB_dtm.tif'),overwrite=T)
# we can then create a slope layer using gdalutils
gdalUtils::gdal_setInstallation()
gdalUtils::gdaldem(mode="slope",
input_dem=paste0(projfolder,'WG_BreconBeacons/Variable_layers/WG_BB_dtm.tif'),
output=paste0(projfolder,'WG_BreconBeacons/Variable_layers/WG_BB_slope.tif'),
,verbose=T)
```
### Climate layers
```{r prepclimate,eval=F}
#list all data files from met office
met_files <- data.frame(files=list.files("MetOffice_HadUK_monthly_grid_1km/",pattern='.nc'))
#subset to seasonal records
met_season <- met_files %>% dplyr::filter(stringr::str_detect(files, "Seas"))
#iterate through taking just the summer season data
out<- purrr:::map(met_season$files,function(metlayer){
met_raw <- raster::brick(paste0("MetOffice_HadUK_monthly_grid_1km/",as.character(metlayer)))
summer <- met_raw[[3]]
summer_crop <-raster::crop(summer,AOI)
#get filename of measure and year
filename_split <- stringr::str_split(gsub(as.character(metlayer),pattern="Had",replacement="_Had"),pattern='_')
filename <- paste0(stringr::str_extract(as.character(filename_split[[1]][4]),'\\d{4}'),'_',filename_split[[1]][2])
#write out
raster::writeRaster(summer_crop, paste0(projfolder,'WG_BreconBeacons/Variable_layers/WG_BB_',filename,'.tif'),overwrite=T)
summer_crop
})
#create a mean layer for use with years without data released - 2019,2020
vars <- c("rainfall","tas","tasmin","tasmax")
# iterate through each variable
all <- purrr::map(vars, .f=function(var){
varfiles <- list.files(paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),full.name=T,pattern=var) # list all files in that folder for variable
varfiles_r <- purrr::map(varfiles, raster::raster) #load
varstack <- raster::stack(varfiles_r) #stack
varmean <- raster::calc(varstack,mean) #mean
raster::writeRaster(varmean,paste0(projfolder,'WG_BreconBeacons/Variable_layers/WG_BB_mean_',var,'.tif')) #write out
})
```
### Sentinel imagery and calculating indices
extraction time series:
* S2A_20170525_lat52lon366_T30UVC_ORB037_utm30n_osgb 2017-05-25
* S2B_20180505_lat52lon366_T30UVC_ORB037_utm30n_osgb 2018-05-05
Modelling time series:
* 2016 S2A_20160719_lat52lon366_T30UVC_ORB037_utm30n_osgb 2016-07-19
* 2017 S2A_20170525_lat52lon366_T30UVC_ORB037_utm30n_osgb 2017-05-25
* 2018 S2A_20180629_lat52lon366_T30UVC_ORB037_utm30n_osgb 2018-06-29
* 2019 S2B_20190420_lat52lon366_T30UVC_ORB037_utm30n_osgb 2019-04-20
* 2020 S2B_20200623_lat52lon366_T30UVC_ORB037_utm30n_osgb 2020-06-23
```{r, eval=F}
projfolder <- 'PeatlandCondition/'
#prep and crop sentinel imagery to new AOI
s2_processing(s2path=paste0(projfolder,"Data/Sentinel-2/WG_BB/"),
out_folder = paste0(projfolder,"WG_BreconBeacons/Variable_layers/"),
nirband=8,rband=3,bband=1,gband=2,swirband=9,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
)
```
## Compiling Variable Stacks
```{r,eval=F}
# create varstacks and mask
# do this for the highly probable peat crop as well as the probable peat
### highly probable peat ###
# extraction varstack: 2017-05-25 (same as 2017 time series)
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20170525_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2017_rainfall.tif',
'WG_BB_2017_tas.tif',
'WG_BB_2017_tasmin.tif',
'WG_BB_2017_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp)',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2017')
# extraction varstack: 2018-05-052018-05-05
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20180505_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2018_rainfall.tif',
'WG_BB_2018_tas.tif',
'WG_BB_2018_tasmin.tif',
'WG_BB_2018_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2018_extract')
# 2016 varstack: 2016-07-19
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20160719_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2016_rainfall.tif',
'WG_BB_2016_tas.tif',
'WG_BB_2016_tasmin.tif',
'WG_BB_2016_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2016')
# 2018 varstack: 2018-06-29
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20180629_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2018_rainfall.tif',
'WG_BB_2018_tas.tif',
'WG_BB_2018_tasmin.tif',
'WG_BB_2018_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2018')
# 2019 varstack: 2019-04-20
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20190420_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_mean_rainfall.tif',
'WG_BB_mean_tas.tif',
'WG_BB_mean_tasmin.tif',
'WG_BB_mean_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2019')
# 2020 varstack: 2020-06-23
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20200623_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_mean_rainfall.tif',
'WG_BB_mean_tas.tif',
'WG_BB_mean_tasmin.tif',
'WG_BB_mean_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2020')
### probable peat ###
# extraction varstack: 2017-05-25 (same as 2017 time series)
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20170525_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2017_rainfall.tif',
'WG_BB_2017_tas.tif',
'WG_BB_2017_tasmin.tif',
'WG_BB_2017_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2017')
# extraction varstack: 2018-05-052018-05-05
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20180505_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2018_rainfall.tif',
'WG_BB_2018_tas.tif',
'WG_BB_2018_tasmin.tif',
'WG_BB_2018_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2018_extract')
# 2016 varstack: 2016-07-19
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20160719_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2016_rainfall.tif',
'WG_BB_2016_tas.tif',
'WG_BB_2016_tasmin.tif',
'WG_BB_2016_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2016')
# 2018 varstack: 2018-06-29
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20180629_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2018_rainfall.tif',
'WG_BB_2018_tas.tif',
'WG_BB_2018_tasmin.tif',
'WG_BB_2018_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2018')
# 2019 varstack: 2019-04-20
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20190420_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_mean_rainfall.tif',
'WG_BB_mean_tas.tif',
'WG_BB_mean_tasmin.tif',
'WG_BB_mean_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2019')
# 2020 varstack: 2020-06-23
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20200623_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_mean_rainfall.tif',
'WG_BB_mean_tas.tif',
'WG_BB_mean_tasmin.tif',
'WG_BB_mean_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'fine_peat_soils.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/'),
stackname='WG_BreconBeacons_2020')
#### probable peat###
# extraction varstack: 2017-05-25 (same as 2017 time series)
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20170525_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2017_rainfall.tif',
'WG_BB_2017_tas.tif',
'WG_BB_2017_tasmin.tif',
'WG_BB_2017_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'CUU_WG_Peat_Area_of_Interest_Data.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/probablepeat/'),
stackname='WG_BreconBeacons_2017')
# extraction varstack: 2018-05-052018-05-05
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20180505_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2018_rainfall.tif',
'WG_BB_2018_tas.tif',
'WG_BB_2018_tasmin.tif',
'WG_BB_2018_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'CUU_WG_Peat_Area_of_Interest_Data.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/probablepeat/'),
stackname='WG_BreconBeacons_2018_extract')
# 2016 varstack: 2016-07-19
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20160719_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2016_rainfall.tif',
'WG_BB_2016_tas.tif',
'WG_BB_2016_tasmin.tif',
'WG_BB_2016_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'CUU_WG_Peat_Area_of_Interest_Data.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/probablepeat/'),
stackname='WG_BreconBeacons_2016')
# 2018 varstack: 2018-06-29
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20180629_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_2018_rainfall.tif',
'WG_BB_2018_tas.tif',
'WG_BB_2018_tasmin.tif',
'WG_BB_2018_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'CUU_WG_Peat_Area_of_Interest_Data.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/probablepeat/'),
stackname='WG_BreconBeacons_2018')
# 2019 varstack: 2019-04-20
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20190420_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_mean_rainfall.tif',
'WG_BB_mean_tas.tif',
'WG_BB_mean_tasmin.tif',
'WG_BB_mean_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'CUU_WG_Peat_Area_of_Interest_Data.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/probablepeat/'),
stackname='WG_BreconBeacons_2019')
# 2020 varstack: 2020-06-23
#varstacks:"Brightness","NDVI","RG","RB","NBR","NDWI","GLI", nir band, average seasonal temp, max seasonal temp, precip, slope
sat_varstack(varpath = paste0(projfolder,'WG_BreconBeacons/Variable_layers/'),
satimg='SEN2_20200623_lat52lon366_T30UVC_ORB037_utm30n_osgb_vmsk_sharp_rad_srefdem_stdsref_BreconBeacons_msk.tif',
satbands=8,
indices=c("Brightness","NDVI","RG","RB","NBR","NDWI","GLI"),
vars=c('WG_BB_mean_rainfall.tif',
'WG_BB_mean_tas.tif',
'WG_BB_mean_tasmin.tif',
'WG_BB_mean_tasmax.tif',
'WG_BB_slope.tif'),
mask= 'CUU_WG_Peat_Area_of_Interest_Data.shp',
outpath = paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/probablepeat/'),
stackname='WG_BreconBeacons_2020')
```
## Extracting training data
```{r,eval=F}
# Two dates needed: 2017 and 2018
# 2017-05-25
train_extract(varstack=paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/WG_BreconBeacons_2017.tif'),
varnames=c("NIRBand","Brightness","NDVI","RG","RB","NBR","NDWI","GLI","rainfall","av_temp","min_temp","max_temp",'slope'),
trainr=paste0(projfolder,'APGB/WG_BB/Combined/bare_pcov_mosaic_2017.tif'),
outfolder=paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Training_data/'),
stratified = T)
# 2018-05-05
train_extract(varstack=paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/highlyprobablepeat/WG_BreconBeacons_2018_extract.tif'),
varnames=c("NIRBand","Brightness","NDVI","RG","RB","NBR","NDWI","GLI","rainfall","av_temp","min_temp","max_temp",'slope'),
trainr=paste0(projfolder,'Data/APGB/WG_BB/Combined/bare_pcov_mosaic_2018.tif'),
outfolder=paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Training_data/'),
stratified = T)
```
## Running the time series regression models
```{r,eval=F}
projfolder <- 'PeatlandCondition/'
## combine data years
training1 <- read.csv(paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/Training_data/WG_BreconBeacons_2017.csv")) %>% dplyr::select(-X)
training2 <- read.csv(paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/Training_data/WG_BreconBeacons_2018_extract.csv")) %>% dplyr::select(-X)
alltrain <- rbind(training1,training2)
write.csv(alltrain,paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/Training_data/WG_BreconBeacons_2017_2018.csv"))
## look at how many per category
alltrain %>% group_by(barecat) %>% summarise(n=n())
## covariate analysis
varstack <- raster::stack(paste0(projfolder,'WG_BreconBeacons/Time_series_modelling/Varstacks/probablepeat/WG_BreconBeacons_2017.tif'))
names(varstack) <-c("NIRBand","Brightness","NDVI","RG","RB","NBR","NDWI","GLI","rainfall","av_temp","min_temp","max_temp",'slope')
# run function to see how the variables correlate
JNCCsdms::corrVars(varstack)
## train models and run predictions
RFReg(training=paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/Training_data/WG_BreconBeacons_2017_2018.csv"),
varpredict=paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/Varstacks/probablepeat/"),
varnames=c("NIRBand","Brightness","NDVI","RG","RB","NBR","NDWI","GLI","rainfall","av_temp","min_temp","max_temp",
'slope'),
out.folder=paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017 & 2018/"),
max_tries=10,
prop.test=0.25,
nsamp=5000,
resamp=5,
stratified=T,fillsamp=F)
```
## Assessing change over time
```{r,eval=F}
dir.create(paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017 & 2018/ChangeMaps"))
pred_maps <- list.files(paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017 & 2018/Outputs/"),
pattern='.tif',full.names = T)
pred_tifs <- purrr::map(pred_maps,raster::raster)
names(pred_tifs) <- gsub('.tif',basename(pred_maps),replacement="")
year_combo <- data.frame(startyear=c(names(pred_tifs)[1:4],names(pred_tifs)[1]),endyear=c(names(pred_tifs)[2:5],names(pred_tifs)[5]))
purrr::map2(as.character(year_combo$startyear),as.character(year_combo$endyear),.f=function(x,y){
start_bare <- pred_tifs[[x]]
end_bare <- pred_tifs[[y]]
change_bare <-raster::overlay(start_bare,end_bare,fun=function(x,y){y-x})
endyear <- gsub('_barepeat',gsub('WG_BreconBeacons',y,replacement=''),replacement='')
raster::writeRaster(change_bare,paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017 & 2018/ChangeMaps/",x,endyear,'change.tif'),overwrite=T)
})
```
# rerunning on just 2017 data with 5000 points
```{r,eval=F}
## Running the time series regression models
projfolder <- 'PeatlandCondition/'
## combine data years
training <- read.csv(paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/Training_data/WG_BreconBeacons_2017.csv"))
## look at how many per category
training %>% group_by(barecat) %>% summarise(n=n())
## train models and run predictions
RFReg(training=paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/Training_data/WG_BreconBeacons_2017.csv"),
varpredict=paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/Varstacks/probablepeat/"),
varnames=c("NIRBand","Brightness","NDVI","RG","RB","NBR","NDWI","GLI","rainfall","av_temp","min_temp","max_temp",
'slope'),
out.folder=paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017/"),
max_tries=10,
prop.test=0.25,
nsamp=5000,
resamp=5,
stratified=T,fillsamp=F)
# running the change analysis
dir.create(paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017/ChangeMaps"))
pred_maps <- list.files(paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017/Outputs/"),
pattern='.tif',full.names = T)
pred_tifs <- purrr::map(pred_maps,raster::raster)
names(pred_tifs) <- gsub('.tif',basename(pred_maps),replacement="")
year_combo <- data.frame(startyear=c(names(pred_tifs)[1:4],names(pred_tifs)[1]),endyear=c(names(pred_tifs)[2:5],names(pred_tifs)[5]))
purrr::map2(as.character(year_combo$startyear),as.character(year_combo$endyear),.f=function(x,y){
start_bare <- pred_tifs[[x]]
end_bare <- pred_tifs[[y]]
change_bare <-raster::overlay(start_bare,end_bare,fun=function(x,y){y-x})
endyear <- gsub('_barepeat',gsub('WG_BreconBeacons',y,replacement=''),replacement='')
raster::writeRaster(change_bare,paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017/ChangeMaps/",x,endyear,'change.tif'),overwrite=T)
})
```
# rerunning on just 2017 data with 2000 points
```{r,eval=F}
## Running the time series regression models
## combine data years
training <- read.csv(paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/Training_data/WG_BreconBeacons_2017.csv"))
## look at how many per category
training %>% group_by(barecat) %>% summarise(n=n())
## train models and run predictions
RFReg(training=paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/Training_data/WG_BreconBeacons_2017.csv"),
varpredict=paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/Varstacks/probablepeat/"),
varnames=c("NIRBand","Brightness","NDVI","RG","RB","NBR","NDWI","GLI","rainfall","av_temp","min_temp","max_temp",
'slope'),
out.folder=paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017_2000points/"),
max_tries=10,
prop.test=0.25,
nsamp=2000,
resamp=5,
stratified=T,fillsamp=F)
# running the change analysis
dir.create(paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017_2000points/ChangeMaps"))
pred_maps <- list.files(paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017_2000points/Outputs/"),
pattern='.tif',full.names = T)
pred_tifs <- purrr::map(pred_maps,raster::raster)
names(pred_tifs) <- gsub('.tif',basename(pred_maps),replacement="")
year_combo <- data.frame(startyear=c(names(pred_tifs)[1:4],names(pred_tifs)[1]),endyear=c(names(pred_tifs)[2:5],names(pred_tifs)[5]))
purrr::map2(as.character(year_combo$startyear),as.character(year_combo$endyear),.f=function(x,y){
start_bare <- pred_tifs[[x]]
end_bare <- pred_tifs[[y]]
change_bare <-raster::overlay(start_bare,end_bare,fun=function(x,y){y-x})
endyear <- gsub('_barepeat',gsub('WG_BreconBeacons',y,replacement=''),replacement='')
raster::writeRaster(change_bare,paste0(projfolder,"WG_BreconBeacons/Time_series_modelling/RegressionModels/2017_2000points/ChangeMaps/",x,endyear,'change.tif'),overwrite=T)
})
```