---
title: "Introduction to geoidep"
author: "Antony Barja"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to geoidep}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  dpi = 300,
  out.width = "100%" 
)
```

## 1. Introduction

This package aims to provide R users with a new way of accessing official Peruvian cartographic data on various topics that are managed by the country's Spatial Data Infrastructure.

By offering a new approach to accessing this official data, both from technical-scientific entities and from regional and local governments, it facilitates the automation of processes, thereby optimizing the analysis and use of geospatial information across various fields.

**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)
```

```{r include=FALSE}
providers <- get_data_sources()
layers_available <- get_providers()

loreto_prov <- get_provinces(show_progress = FALSE) |>
    subset(nombdep == "LORETO")
  
loreto_prov[["ubigeo"]] <- paste0(loreto_prov[["ccdd"]], loreto_prov[["ccpp"]])

```



## 3. Basic usage

```{r}
providers
```

```{r}
layers_available
```

## 4. Download Official Administrative Boundaries by INEI
```{r}
# Region boundaries download (done once in the setup chunk above)
head(loreto_prov, 3)
```

```{r, out.width='100%', out.height=250}
library(mapgl)
library(sf)
maplibre_view(data = loreto_prov)
```

## 5. Working with Geobosque data

```{r,include=FALSE}
probe <- get_forest_loss_data(
  layer = "stock_bosque_perdida_provincia",
  ubigeo = loreto_prov[["ubigeo"]][1],
  show_progress = FALSE
  )

```


```{r}
my_fun <- function(x){
  data <- get_forest_loss_data(
    layer = 'stock_bosque_perdida_provincia',
    ubigeo = loreto_prov[["ubigeo"]][x],
    show_progress = FALSE )
  return(data)
}
historico_list <- lapply(X = 1:nrow(loreto_prov),FUN = my_fun)
historico_df <- do.call(rbind.data.frame,historico_list)
```

```{r}
# The first five rows
head(historico_df)
```

## 6. Simple visualization with ggplot

```{r , fig.align='center'}
library(ggplot2)
library(dplyr)

historico_prov <- historico_df |>
  inner_join(y = loreto_prov, by = "ubigeo")

promedio_loreto <- historico_prov |>
  group_by(anio) |>
  summarise(perdida = mean(perdida), .groups = "drop")
```


```{r , fig.align='center', fig.height=7, fig.width=12}
ggplot(historico_prov, aes(x = anio, y = perdida)) +
  geom_line(aes(group = nombprov, color = "Provincia"), linewidth = 0.6) +
  geom_line(
    data = promedio_loreto,
    aes(color = "Promedio Loreto"),
    linewidth = 0.6,
    linetype = "dashed",
    ) +
  scale_color_manual(name = NULL, values = c("Provincia" = "red", "Promedio Loreto" = "black")) +
  facet_wrap(nombprov ~ .) +
  theme_minimal(base_size = 12) +
  theme(legend.position = "bottom") +
  labs(
    title = "Pérdida de bosque 2001-2025: provincias de Loreto vs. promedio departamental",
    caption = "Fuente: Geobosque",
    x = "",
    y = "")
```
