-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo_fesm_overlay.r
145 lines (116 loc) · 4.69 KB
/
demo_fesm_overlay.r
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
#### DEMO Development - FESM Integration
library(tidyverse)
library(sf)
library(raster)
fesm_root <- "G:/ft_work/fesm/"
template <- raster("G:/ft_work/veg_bmnts_study_30m/r_vegadv.tif")
# List FESM files
fesm_list <- list.files(fesm_root,pattern="img")
sp <- str_split(fesm_list,"_",simplify = TRUE)
fesm_date <- as.Date(sp[,3],"%Y%m%d")
fesm_frame <- tibble(File = paste0(fesm_root,fesm_list),Date=fesm_date)
fesm_frame <- arrange(fesm_frame,rev(Date))
for(i in 1:nrow(fesm_frame)){
print(i)
r <- raster(fesm_frame$File[i])
r <- projectRaster(r,template,method="ngb")
r <- resample(r,template,method="ngb")
values(r)[values(r)==0]=NA
if(i ==1){
rx=r
}else{
rx = cover(rx,r)
}
}
# load region boundary
boundary <- raster("G:/ft_work/veg_bmnts_study_30m/roi_mask.tif")
rx <- rx * boundary
writeRaster(rx,"G:/ft_work/fesm_bmnts_study_30m/merged_fesm.tif",overwrite=TRUE)
values(rx)[values(rx) %in% c(2,3)] = 1
values(rx)[values(rx) %in% c(4,5)] = 2
writeRaster(rx,"G:/ft_work/fesm_bmnts_study_30m/merged_fesm_2class.tif",overwrite=TRUE)
# We now have 2-class fesm, time to combine with heritage threshold!
heritage <- raster("G:/ft_work/full_analysis_bmnts_study_30m/r_heritage_threshold_status.tif")
values(rx)[values(rx)==2]=10
heritage <- heritage * rx
# Define colours
# remove missing areas
values(heritage)[values(heritage) %in% c(1,9,10,90)] = NA
rtable = data.frame(ID = c(2,3,4,5),
Status = c("TooFrequentlyBurnt",
"Vulnerable",
"LongUnburnt",
"WithinThreshold"
),
Colour = c("#ff0000","#ff9900","#00ffff","#999999"))
stable = data.frame(ID = c(20,30,40,50),
Status = c("TooFrequentlyBurnt - Canopy Loss",
"Vulnerable - Canopy Loss",
"LongUnburnt - Canopy Loss",
"WithinThreshold - Canopy Loss"),
Colour = c("#aa0000","#aa6600","#00aaaa","#333333"))
ctable <- bind_rows(rtable,stable)
#library(tmap)
#m <- tm_shape(heritage,raster.downsample = FALSE) + tm_raster(breaks=ctable$ID,palette = ctable$Colour,style="cat",labels = ctable$Status)
#m
# Ratify and colour table the output raster
heritage <- ratify(heritage)
rat <- levels(heritage)[[1]]
rat <- left_join(rat,ctable)
ids = as.numeric(rownames(rat))
codes = rat$ID
heritage = reclassify(heritage,cbind(codes,ids))
rat$ID = ids
levels(heritage) <- rat
require(foreign)
atable = levels(heritage)[[1]]
names(atable)=c("VALUE","CATEGORY","COLOUR")
x = as.data.frame(table(raster::values(heritage)))
names(x)=c("VALUE","COUNT")
x$VALUE = as.numeric(as.character(x$VALUE))
a2 = left_join(atable,x)
a2$COUNT[is.na(a2$COUNT)]=0
a2 = dplyr::select(a2,VALUE,COUNT,CATEGORY)
write.dbf(a2,"G:/ft_work/fesm_bmnts_study_30m/heritage_fesm.tif.vat.dbf")
colortable(heritage) <- c("#ffffff",ctable$Colour)
writeRaster(heritage,"G:/ft_work/fesm_bmnts_study_30m/heritage_fesm.tif",overwrite=TRUE)
# Now veg overlay and tabulation, as for fire/heritage
# Load veg
source("../config/config_summary_heritage.r")
veg <- read_sf(paste0(veg_folder,"/","v_vegBase.gpkg"))
veg_r <- raster(paste0(veg_folder,"/","r_vegcode.tif"))
# Join ad create lookup table for vegetation names
# Based on input vegetation file
vc <- values(veg_r)
vc <- tibble(VEG = vc)
veg_ns <- veg
st_geometry(veg_ns)<-NULL
veg_ns <- dplyr::select(veg_ns,VEG,(!!rlang::sym(veg_field)))
veg_ns[[veg_field]] <- factor(veg_ns[[veg_field]])
veg_ns <- distinct(veg_ns)
veg_ns <- left_join(vc,veg_ns)
veg_ns[[veg_field]]<-toupper(veg_ns[[veg_field]])
rm(veg)
rm(veg_r)
gc()
# Overlay heritage/veg
ii <- values(heritage)
rtable <- levels(heritage)[[1]]
ii <- rtable$Status[ii]
ii[is.na(ii)]="No Status"
ii <- factor(ii)
#ii <- addNA(ii)
ii <- tibble(Status = ii, Veg = veg_ns[[veg_field]])
ii <- ii %>% group_by(Status,Veg,.drop=FALSE) %>% summarise(count = n())
current_table <- filter(ii, !is.na(Veg))
current_table$Veg[current_table$Veg=="<NULL>"] = "UNKNOWN"
current_table$area = current_table$count * (res(heritage)[1] * res(heritage)[2])
current_table$area = current_table$area / 10000
current_wide <- pivot_wider(current_table,id_cols = Veg,names_from=Status,values_from=area)
current_wide <- mutate(current_wide, across(everything(), ~replace_na(.x, 0)))
current_wide <- current_wide %>%
bind_rows(summarise(.,
across(where(is.numeric), sum),
across(where(is.character), ~"Total")))
current_wide <- current_wide %>% rowwise() %>% mutate(Total = sum(across(!contains("Veg"))))
write_csv(current_wide,"G:/ft_work/fesm_bmnts_study_30m/FESM_current_year_veg_summary.csv")