---
title: "Site map examples"
author: "Duncan Golicher"
date: "`r Sys.Date()`"
output:rmarkdown::html_vignette:
        fig_caption: yes
vignette: >
  %\VignetteIndexEntry{"Site map examples"}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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


## Loading layers from the data base

The giscourse package has four composite high level functions that load in data for a site. 

You can either use a string of characters to geocode the site, or the coordinates as latitude and longitude. If a distance in meters is provided the functions download all features within this radius. If no distance is given then the default is to download all the features inside the OS 5km grid square for the site.

### Example 1: Geocode Arne and get 5km grid square

```{r}
library(giscourse)
conn<-connect()
lcover<-landcover("Arne, Dorset") 
sssi<-sssi("Arne, Dorset") 
phabitat<-phabitat("Arne, Dorset") 
osm<-osm("Arne, Dorset")
dem<-mkdem("Arne, Dorset") 
```

This code has loaded land-cover, sssi boundaries, priority habitat, open street map paths, tracks and roads and the 12m resolution digital elevation model for the 5km grid square that contains R. 

Higher resolution DTMs are available by changing the zoom value. The highest resolution provided by elevatr is 3.5 meters. The server contains some Lidar data at higher resolution for chosen sites such as R. The functions given here will work for any site in the South of England.

### Exploratory mapview

Use burst =TRUE to plot each land use separately. Useful for exploration.

```{r}
mapviewOptions(legend=FALSE)
mapview(lcover,zcol="bhab",burst=TRUE,  hide = TRUE) %>% extras()
```


```{r}
mapview(osm) +mapview(dem)
```

### Terrain analysis

```{r}
dem[dem<1]<-NA
sloper<-terrain(dem,"slope","radians")
aspectr<-terrain(dem,"aspect","radians")
hs<-hillShade(sloper,aspectr)
```



```{r}
library(tmap)
lcover %>% filter(bhab !="Saltwater") %>% 

qtm("bhab") + tmap_style("classic") + tmap_options(legend.outside=TRUE) + tm_scale_bar() + tm_compass(type="arrow") + tm_shape(hs) +
 tm_raster(alpha=0.5,palette = gray(0:100 / 100), n = 100, legend.show = FALSE)
```

## Example 2: Distance around a point

These coordinates are for a study point in the New forest

```{r}
lon<--1.5
lat<-50.86
landcover(lon,lat,dist=2000) ->lcover
sssi(lon,lat,dist=2000) ->sssi
phabitat(lon,lat,dist=2000) -> phabitat

```


```{r}
mapview(lcover,zcol="bhab",burst=TRUE,  hide = TRUE) %>% extras()
```


```{r}

lcover %>% 

qtm("bhab") + tmap_style("classic") + tmap_options(legend.outside=TRUE) + tm_scale_bar() + tm_compass(type="arrow")
```

It's easy to calculate total areas of each habitat.

```{r}

lcover$bhab<-as.factor(lcover$bhab)

lcover %>% data.frame() %>% group_by(bhab) %>% summarise(n=n(), area_ha=round(sum(area)/10000,1)) -> areas
```


```{r}
library(ggplot2)

areas %>% arrange(area_ha) %>%  mutate(bhab = factor(bhab, bhab)) %>%
ggplot(aes(x=bhab,y=area_ha,lab=area_ha)) +geom_bar(stat="identity",fill="grey") + geom_label(aes(x = bhab, y = area_ha, label = paste(area_ha,"ha"))) + coord_flip() + theme_bw()
```


### Saving the results to a geodata file

All the layers can be written to a single geodata package file. The geodata package can then be exported from the server and dropped into a local QGIS project for work offline.


```{r,eval=FALSE}
st_write(arne_lcover, dsn="geodata.gpkg", layer="arne_lcover")
st_write(arne_sssi, dsn="geodata.gpkg", layer="arne_sssi")
## Etc, etc ..
```

### Insolation and terrain wetness analysis

The package allows the SAGA functions to be run on a clipped digital terrain model. 

```{r}
corfe_dem<-mkdem(-2.057,50.64,dist=1000,z=13)
sol<-insol(corfe_dem)
twi<-twi(corfe_dem)
mapview(twi)
sloper<-terrain(corfe_dem,"slope","radians")
aspectr<-terrain(corfe_dem,"aspect","radians")
hs<-hillShade(sloper,aspectr)

```



```{r}
landcover(-2.057,50.64,dist=1000)->lc

tm_shape(hs) +
 tm_raster(palette = gray(0:100 / 100), n = 100, legend.show = FALSE) + tm_shape(lc) + tm_fill("bhab",alpha=0.8) +tmap_style("classic") +  tmap_options(legend.outside=TRUE) ->mp

tmap_mode("view")
mp + tm_minimap()

```

### Modis derived fire points


Data from ftp://fuoco.geog.umd.edu/modis/C6/mcd14ml/ have been downloaded and reformatted for Postgis. The current data cover the period between December 2000 and January 2019.

### Fires in the New forest area

```{r}
fires<-get_fires(-1.7,50.8,0.4)
library(RColorBrewer)
pal <- brewer.pal(n = 7, name = "OrRd")
fires %>% mapview(zcol="year",col.regions =pal,legend=FALSE) %>% hansen_wms()
```

```{r}
library(dplyr)
library(ggplot2)

fires %>% filter(year>2000) %>% group_by(year) %>% summarise(n=n()) %>% ggplot(aes(x=year,y=n)) + geom_line() +geom_point()
```


#### Fires in Mexico

```{r}
fires<-get_fires(-92,16.5,0.4)
lc<-get_lc(-92,16.5,50)
library(RColorBrewer)
pal <- brewer.pal(n = 7, name = "OrRd")
fires %>% mapview(zcol="year",col.regions =pal,legend=FALSE) + mapview(lc$lc,col.regions=lc$pal) -> mp
```


```{r}

mp %>% hansen_wms() 
```



```{r}
fires %>% filter(year>2000) %>% group_by(year) %>% summarise(n=n()) %>% ggplot(aes(x=year,y=n)) + geom_line() +geom_point()
```


```{r}

land_cover<-data.frame(id=raster::extract(lc$lc,as(fires,"Spatial")))
land_cover<-base::merge(land_cover,lc$legend)
data.frame(table(land_cover$land_cover)) %>% DT::datatable()
```


```{r}
library(dplyr)
library(ggplot2)

fires %>% filter(year>2000) %>% group_by(year) %>% summarise(n=n()) %>% ggplot(aes(x=year,y=n)) + geom_line() +geom_point()

```


### Deforestation analysis

We can use the get_hansen function to cut out raw data for any reasonably sized study area from Hansen's global mosaic. For example, to obtain a 4 km by 4km grid square use a distance of 2km around a point showing a high rate of deforestation.

```{r}

fr<-get_hansen(x=101,y=0.01, dist=2) 

```

The loss layer holds the year at which the loss was detected. The WMS loss layer simply shows all loss during the period from 2001 to 2018 in red.

```{r}
fr$loss[fr$loss<1]<-NA
mapview(fr$loss,legend=TRUE) %>% hansen_wms()

```

```{r}
d<-data.frame(table(round(getValues(fr$loss),0))) # Tabulate the results
pix_area<-28*28
cell_area<- 40*40
d %>% 
  mutate(area_ha=Freq*pix_area/10000) %>%
  mutate(percent=round(100*area_ha/cell_area,1)) %>%
  mutate(year=as.numeric(as.character(Var1))+2000) ->d
DT::datatable(d)
```

So we can see that the deforestation intensified in 2008.


```{r}
d %>% ggplot(aes(x=year,y=percent)) +geom_line() +ylab("Annual deforestation as percentage of total area")
```


The pattern of loss can be calculated easily by forming a raster stack containing the forest remaining for each year.


```{r}
forest<-fr$forest2000>50
ls<-function(yr=1){
 forest * (1 -round((fr$loss<yr)*1,0))
}

list<-7:18

loss<-raster::stack(lapply(list,ls))
names(loss)<-paste("Forest", 2007:2018)
plot(loss,legend=FALSE)
```

