# # # # # # # # # # # # # #
# LOAD PACKAGES -----------
# # # # # # # # # # # # # #

if(!require("pacman")) install.packages("pacman") 
pacman::p_load(mgcv, dplyr, sf, here, ggplot2)


# # # # # # # # # # # # # #
# # # # # # # # # # # # # #
# LOAD DATA ---------------
# # # # # # # # # # # # # #
# # # # # # # # # # # # # #


path.files <- here::here() # perhaps, you have to adapt the folder directory
path.files


# ... INVENTORY -----------------

# ... WAIDHOFEN
Wh.Lslide_dis <- sf::st_read(dsn = file.path(path.files, "Waidhofen", "Landslides_dissolved.shp")) # Shapefile with dissolved landslides
Wh.Lslide_pts <- sf::st_read(dsn = file.path(path.files, "Waidhofen", "Landslides_Pts.shp")) # Shapefile with absence/presence landslide samples
Wh.Lslide_source <- sf::st_read(dsn = file.path(path.files, "Waidhofen", "Landslides_source.shp")) # Shapefile with landslide source areas

# ... Paldau
P.Lslide_dis <- sf::st_read(dsn = file.path(path.files, "Paldau", "Landslides_dissolved.shp")) # Shapefile with dissolved landslides
P.Lslide_pts <- sf::st_read(dsn = file.path(path.files, "Paldau", "Landslides_Pts.shp")) # Shapefile with absence/presence landslide samples
P.Lslide_source <- sf::st_read(dsn = file.path(path.files, "Paldau", "Landslides_source.shp")) # Shapefile with landslide source areas



# ... LANDSLIDE DATA AND SUSCEPTIBILITY MODELS -----------------

# ... WAIDHOFEN
Wh.Lslide_df <- readRDS(file = file.path(path.files, "Waidhofen", "Wh__df_Lslide.rds")) # dataset with absence/presence landslide observations, see README.txt for description of attributes

Wh.GAM_BASE <- readRDS(file = file.path(path.files, "Waidhofen", "Wh__GAM__BASE__model.rds")) # "GAM-Base"
Wh.GAM_LEG1820 <- readRDS(file = file.path(path.files, "Waidhofen", "Wh__GAM__LEG1820__model.rds")) # "GAM-1820"
Wh.GAM_LEG1960 <- readRDS(file = file.path(path.files, "Waidhofen", "Wh__GAM__LEG1960__model.rds")) # "GAM-1960"
Wh.GAM_LULC21 <- readRDS(file = file.path(path.files, "Waidhofen", "Wh__GAM__LULC21__model.rds")) # "GAM-2015"
Wh.GAM_LULC21Mask <- readRDS(file = file.path(path.files, "Waidhofen", "Wh__GAM__LULC21nF__model.rds")) # "GAM-2015-Masked"


# ... Paldau
P.Lslide_df <- readRDS(file = file.path(path.files, "Paldau", "P__df_Lslide.rds")) # dataset with absence/presence landslide observations, see README.txt for description of attributes

P.GAM_BASE <- readRDS(file = file.path(path.files, "Paldau", "P__GAM__BASE__model.rds")) # "GAM-Base"
P.GAM_LEG1820 <- readRDS(file = file.path(path.files, "Paldau", "P__GAM__LEG1820__model.rds")) # "GAM-1820"
P.GAM_LEG1960 <- readRDS(file = file.path(path.files, "Paldau", "P__GAM__LEG1960__model.rds")) # "GAM-1960"
P.GAM_LULC21 <- readRDS(file = file.path(path.files, "Paldau", "P__GAM__LULC21__model.rds")) # "GAM-2015"
P.GAM_LULC21Mask <- readRDS(file = file.path(path.files, "Paldau", "P__GAM__LULC21nF__model.rds")) # "GAM-2015-Masked"





# # # # # # # # # # # # # #
# # # # # # # # # # # # # #
# PLOT DATA AND SUMMARY ---------------
# # # # # # # # # # # # # #
# # # # # # # # # # # # # #

# ... QUICK LOOK ON INVENTORY -----------------

# ... WAIDHOFEN
ggplot2::ggplot() +
  geom_sf(data = Wh.Lslide_source, fill = "blue", color = NA) + 
  geom_sf(data = Wh.Lslide_dis, fill = NA, color = "red") + 
  # geom_sf(data = Wh.Lslide_pts %>% dplyr::filter(Lslide == 1), size = 0.2) + 
  ggtitle("Waidhofen") +
  theme_bw()

# ... Paldau
ggplot2::ggplot() +
  geom_sf(data = P.Lslide_source, fill = "blue", color = NA) + 
  geom_sf(data = P.Lslide_dis, fill = NA, color = "red") + 
  # geom_sf(data = P.Lslide_pts %>% dplyr::filter(Lslide == 1), size = 0.2) + 
  ggtitle("Paldau") +
  theme_bw()



# ... QUICK LOOK ON LANDSLIDE SUSCEPTIBILITY MODELS -----------------

# ... WAIDHOFEN
Wh.GAM.list <- list("GAM-Base" = Wh.GAM_BASE, 
                    "GAM-2015" = Wh.GAM_LULC21, 
                    "GAM-2015-Masked"= Wh.GAM_LULC21Mask, 
                    "GAM-1960" = Wh.GAM_LEG1960, 
                    "GAM-1820" = Wh.GAM_LEG1820)

Wh.GAM.list %>% lapply(X = ., FUN = summary)




# ... Paldau
P.GAM.list <- list("GAM-Base" = P.GAM_BASE, 
                    "GAM-2015" = P.GAM_LULC21, 
                    "GAM-2015-Masked"= P.GAM_LULC21Mask, 
                    "GAM-1960" = P.GAM_LEG1960, 
                    "GAM-1820" = P.GAM_LEG1820)

P.GAM.list %>% lapply(X = ., FUN = summary)



# ... QUICK LOOK ON PREDICTOR-RESPONSE RELATIONSHIPS -----------------

# ... SLOPE ANGLE OF GAM-BASE (select = 9)

# ... WAIDHOFEN
Wh.GAM.list$`GAM-Base` %>% mgcv::plot.gam(select = 9, main = "Waidhofen - slope angle")

# ... Paldau
P.GAM.list$`GAM-Base` %>% mgcv::plot.gam(select = 9, main = "Paldau - slope angle")



