---
title: "Analyzing forest fires with the geoideo package"
author: "Antony Barja"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Currently with geoidep you can work with forest fire data provided by the forest monitoring unit of serfor.}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  dpi = 300,
  out.width = "100%" 
)
```

## 1. Introduction

`geoidep` currently allows you to work with data from Serfor's Satellite Tracking Unit.

**However, this project is still under construction, for more information you can visit the GitHub official repository <https://github.com/ambarja/geoidep>.**

If you want to support this project, you can support me with a coffee for my programming moments.

<center>
<script type='text/javascript' src='https://storage.ko-fi.com/cdn/widget/Widget_2.js'></script><script type='text/javascript'>kofiwidget2.init('Support Me on Ko-fi', '#ff0022', 'B0B56J9J9');kofiwidget2.draw();</script> 
<center/>

## 2. Package installation 

```r
install.packages("geoidep")
```
Also, you can install the development version as follows: 

```r
install.packages('pak')
pak::pkg_install('ambarja/geoidep')
```

```{r}
library(geoidep)
```

## 3. Filter the available Serfor layer

```{r}
providers <- get_data_sources(query = "Serfor")
providers
```

## 4. Forest fire data collection download
```{r}
incendio.forestal <- get_forest_fire_data(show_progress = FALSE)
head(incendio.forestal)
```

## 5. National Forest Fire Count 
```{r message=FALSE, warning=FALSE}
library(sf)
library(dplyr)
library(ggplot2)
stats <- incendio.forestal |> 
  st_drop_geometry() |>
  filter(FECHA >= '2024-09-11') |> 
  group_by(NOMDEP) |> 
  summarise( total = n()) 
```

```{r out.height=500,out.width=500,fig.align='center'}
# Simple visualisation
stats |> 
  ggplot(aes(x = NOMDEP, y = total)) + 
  geom_bar(stat = "identity") + 
  coord_polar() + 
  theme_minimal(base_size = 10)  + 
  labs(x="", y = "", caption = "Unidad de Monitoreo Forestales - SERFOR")
```


## 6. Download the official INEI administrative limits of interest 
```{r}
# Region boundaries download 
ucayali_dep <- get_departaments(show_progress = FALSE) |> 
  subset(NOMBDEP == 'UCAYALI')
```

```{r}
# The first five rows
head(ucayali_dep)
```


## 7. Forest fire count in the area of interest only
```{r warning=FALSE,message=FALSE}
# Data collection only within the regions of interest.
ucayali.if <- st_filter(incendio.forestal, ucayali_dep)
head(ucayali.if)
```


## 8. Simple visualization with leaflet
```{r ,fig.align='center',out.height=500}
library(leaflet)
library(leaflet.extras)
ucayali.if |> 
  leaflet() |> 
  addProviderTiles(provider = "CartoDB.Positron") |> 
  addHeatmap(radius = 10,minOpacity = 1)
```
