---
title: "5. Fuzzy Hypotheses"
author: "Wolfgang Hamer, Daniel Knitter"
output: html_document
---


```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  warning = FALSE,
  message = FALSE,
  eval = TRUE,
  results = "show",
  #echo = FALSE,
  comment = "#>")
```

## Import data

```{r}
library(magrittr)
library(sf)
library(raster)
library(FuzzyLandscapes)
library(dplyr)
library(rpredmod)

geomorph <- stack(list.files(paste0("../data/derived_data/raster/geomorph/"),full.names=TRUE,pattern=".tif"),
                  "../data/derived_data/raster/moraine_distr.tif",
                  "../data/derived_data/raster/mire_head_distr.tif",
                  "../data/derived_data/raster/waters_dist.tif")
```

## Hypotheses - reindeer hunters {.tabset .tabset-fade .tabset-pills}

### Valleys

```{r}
enclosed <- fl_create_ras(method="trapezoid",
                          rast = geomorph$neg,
                          p1 = 1.566, 
                          p2=1.57, 
                          p3 = 1.6,
                          p4 = 1.61, 
                          setname = "enclosed")
plot_fs(enclosed)
#  the degree of "enclosure" of a lower location by elevated surroundings (negative openness) Yokoyama 2002


low_open <- fl_create_ras(method="trapezoid",
                          rast = geomorph$pos,
                          p1 = 1.3, 
                          p2=1.4, 
                          p3 = 1.5,
                          p4 = 1.55, 
                          setname = "low_open")
plot_fs(low_open)
#  the degree of "enclosure" of a lower location by elevated surroundings (negative openness) Yokoyama 2002

valley_g <- fl_create_ras(method="crisp",
                          rast = geomorph$geomorphons,
                          p1 = 9, 
                          setname = "valley_g")
plot(valley_g$FuzzyRaster)

valley <- fl_combine_fuzzy(enclosed,
                           low_open,
                           valley_g,
                           combinator = "or",
                           newname = "valley")

plot_fs(valley)

writeRaster(x = valley$FuzzyRaster,
            filename = "../data/derived_data/raster/FuzzyPot/valley.tif",
            format = "GTiff",
            overwrite = TRUE)
```


### Valleys with waters

```{r}
wet <- fl_create_ras(method="trapezoid",
                     rast = geomorph$twi,
                     p1 = 10, 
                     p2=20, 
                     p3 = 40, 
                     p4 = 50, 
                     setname = "wet")
d_waters <- fl_create_ras(method="trapezoid",
                          rast = geomorph$waters_dist,
                          p1 = 0, 
                          p2=20, 
                          p3 = 1000, 
                          p4 = 2500, 
                          setname = "Reindeer_hunters")

d_mire <- fl_create_ras(method="trapezoid",
                        rast = geomorph$mire_head_distr,
                        p1 = 0, 
                        p2=20, 
                        p3 = 1000, 
                        p4 = 2500, 
                        setname = "Reindeer_hunters")

wet_c <- fl_combine_fuzzy(wet,
                          d_waters,
                          d_mire,
                          combinator = "or",
                          newname = "wet_c")

plot_fs(wet_c)

writeRaster(x = wet_c$FuzzyRaster,
            filename = "../data/derived_data/raster/FuzzyPot/wet_c.tif",
            format = "GTiff",
            overwrite = TRUE)

wet_depression <- fl_combine_fuzzy(valley,
                                   wet_c,
                                   combinator = "and",
                                   newname = "wet_depression")

plot_fs(wet_depression)
writeRaster(x = wet_depression$FuzzyRaster,
            filename = "../data/derived_data/raster/FuzzyPot/wet_depression.tif",
            format = "GTiff",
            overwrite = TRUE)
```

### Flintstone accessibility

```{r}
flint <- fl_create_ras(method="triangle",
                       rast = geomorph$moraine_distr,
                       p1 = -1, 
                       p2=0, 
                       p3 = 3000, 
                       setname = "flint")
plot_fs(flint)

writeRaster(x = flint$FuzzyRaster,
            filename = "../data/derived_data/raster/FuzzyPot/flint.tif",
            format = "GTiff",
            overwrite = TRUE)

library(ggplot2)

flint_plot <- plot_fs_comb(flint) + 
  labs(x = "Distance (m)") +
  theme(legend.position="none") +
  scale_colour_grey(end = 0)+ 
  coord_cartesian(xlim =c(0,3500))

ggsave("../plots/flintfuzzy.png", 
       plot = flint_plot,
       width = 6, height = 4)

wet_depression_near_flint <- fl_combine_fuzzy(wet_depression,
                                              flint,
                                              combinator = "and",
                                              newname = "wet_depression_near_flint")

plot_fs(wet_depression_near_flint)

writeRaster(x = wet_depression_near_flint$FuzzyRaster,
            filename = "../data/derived_data/raster/FuzzyPot/wet_depression_near_flint.tif",
            format = "GTiff",
            overwrite = TRUE)
```


## Hypotheses - elk hunters {.tabset .tabset-fade .tabset-pills}

### Low dry ridges

```{r}
ridge <- fl_create_ras(method="trapezoid",
                       rast = geomorph$neg,
                       p1 = 1.50, 
                       p2=1.52, 
                       p3 = 1.54,
                       p4 = 1.55, 
                       setname = "ridge")
plot_fs(ridge)
#  the degree of "enclosure" of a lower location by elevated surroundings (negative openness) Yokoyama 2002

neg_plot <- plot_fs_comb(enclosed,ridge,cols=colorRampPalette(c("grey55","black")),ltys=c(1,2))+ 
  labs(x = "Negative Openess", y="") +
  theme(legend.position="none",
        axis.text.y=element_blank())+
  coord_cartesian(xlim =c(1.45, 1.65))


sandy_ridge <- fl_combine_fuzzy(ridge,
                                list(FuzzyRaster=raster("../data/derived_data/raster/pleistocene_fluvial_sand_rast.tif")),
                                combinator = "and",
                                newname = "sandy_ridge")
writeRaster(x = sandy_ridge$FuzzyRaster,
            filename = "../data/derived_data/raster/FuzzyPot/sandy_ridge.tif",
            format = "GTiff",
            overwrite = TRUE)
```

### Flintstone accessibility

```{r}

sandy_ridge_near_flint <- fl_combine_fuzzy(sandy_ridge,
                                           flint,
                                           combinator = "and",
                                           newname = "sandy_ridge_near_flint")

plot_fs(sandy_ridge_near_flint)

writeRaster(x = sandy_ridge_near_flint$FuzzyRaster,
            filename = "../data/derived_data/raster/FuzzyPot/sandy_ridge_near_flint.tif",
            format = "GTiff",
            overwrite = TRUE)
```

### Close to water bodys

```{r}
wet_elk <- fl_create_ras(method="trapezoid",
                         rast = geomorph$twi,
                         p1 = 10, 
                         p2=20, 
                         p3 = 40, 
                         p4 = 50, 
                         setname = "wet_elk")
d_waters_elk <- fl_create_ras(method="trapezoid",
                              rast = geomorph$waters_dist,
                              p1 = 0, 
                              p2=20, 
                              p3 = 500, 
                              p4 = 1500, 
                              setname = "Elk_hunters")

d_mire_elk <- fl_create_ras(method="trapezoid",
                            rast = geomorph$mire_head_distr,
                            p1 = 0, 
                            p2=20, 
                            p3 = 500, 
                            p4 = 1500, 
                            setname = "Elk_hunters")

wet_c_elk <- fl_combine_fuzzy(wet_elk,
                              d_waters_elk,
                              d_mire_elk,
                              combinator = "or",
                              newname = "wet_c_elk")
writeRaster(x = wet_c_elk$FuzzyRaster,
            filename = "../data/derived_data/raster/FuzzyPot/wet_c_elkt.tif",
            format = "GTiff",
            overwrite = TRUE)


waters_dist_plot <- plot_fs_comb(d_waters,d_waters_elk,cols=colorRampPalette(c("grey55","black")),ltys=c(1,2))+ 
  labs(x = "Distance (m)", y="") +
  theme(legend.position="none",
        axis.text.y=element_blank())+ 
  coord_cartesian(xlim =c(0,3500))

library(ggpubr)
ggsave("../plots/membership_plots_neg.png", 
       plot = ggarrange(flint_plot,
                        waters_dist_plot,
                        neg_plot,
                        labels = c("A", "B","C"),
                        ncol = 3, nrow = 1),
       width = 12, height = 4)


wet_sandy_ridge_near_flint <- fl_combine_fuzzy(sandy_ridge_near_flint,
                                               wet_c_elk,
                                               combinator = "and",
                                               newname = "wet_sandy_ridge_near_flint")

plot_fs(wet_sandy_ridge_near_flint)
writeRaster(x = wet_sandy_ridge_near_flint$FuzzyRaster,
            filename = "../data/derived_data/raster/FuzzyPot/wet_sandy_ridge_near_flint.tif",
            format = "GTiff",
            overwrite = TRUE)
```
