Skip to contents

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.

2. Package installation

install.packages("geoidep")

Also, you can install the development version as follows:

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

3. Basic usage

providers <- get_data_sources()
providers
#> # A tibble: 82 × 7
#>    provider  category   layer layer_can_be_actived admin_en year  link_geoportal
#>    <chr>     <chr>      <chr> <lgl>                <chr>    <chr> <chr>         
#>  1 INEI      General    depa… TRUE                 Nationa… 2019  https://ide.i…
#>  2 INEI      General    prov… TRUE                 Nationa… 2019  https://ide.i…
#>  3 INEI      General    dist… TRUE                 Nationa… 2019  https://ide.i…
#>  4 Midagri   Agricultu… agri… TRUE                 Ministr… 2024  https://siea.…
#>  5 Midagri   Agricultu… oil_… TRUE                 Ministr… 2016… https://siea.…
#>  6 Geobosque Forest     stoc… FALSE                Ministr… 2001… https://geobo…
#>  7 Geobosque Forest     stoc… TRUE                 Ministr… 2001… https://geobo…
#>  8 Geobosque Forest     stoc… TRUE                 Ministr… 2001… https://geobo…
#>  9 Geobosque Forest     stoc… TRUE                 Ministr… 2001… https://geobo…
#> 10 Geobosque Forest     warn… TRUE                 Ministr… last… https://geobo…
#> # ℹ 72 more rows
layers_available <- get_providers()
layers_available
#> # A tibble: 9 × 2
#>   provider  layer_count
#>   <fct>           <int>
#> 1 Geobosque           5
#> 2 INAIGEM             5
#> 3 INEI                7
#> 4 Midagri             2
#> 5 MTC                26
#> 6 Senamhi             1
#> 7 Serfor              1
#> 8 Sernanp            31
#> 9 SIGRID              4

4. Download Official Administrative Boundaries by INEI

# Region boundaries download 
loreto_prov <- get_provinces(show_progress = FALSE) |> 
  subset(nombdep == 'LORETO')
library(leaflet)
#> 
#> Attaching package: 'leaflet'
#> The following object is masked _by_ '.GlobalEnv':
#> 
#>     providers
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
loreto_prov |> 
  leaflet() |> 
  addTiles() |> 
  addPolygons()
# Defined Ubigeo 
loreto_prov[["ubigeo"]] <- paste0(loreto_prov[["ccdd"]],loreto_prov[["ccpp"]])
# The first five rows
head(loreto_prov)
#> Simple feature collection with 6 features and 9 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -77.82596 ymin: -6.14773 xmax: -69.94904 ymax: -0.03860597
#> Geodetic CRS:  WGS 84
#>      id objectid ccdd ccpp nombdep                nombprov shape_length
#> 136 136      136   16   02  LORETO           ALTO AMAZONAS     9.955533
#> 137 137      137   16   07  LORETO       DATEM DEL MARAÑON    11.493679
#> 138 138      138   16   03  LORETO                  LORETO    13.602250
#> 139 139      139   16   04  LORETO MARISCAL RAMON CASTILLA    15.113580
#> 140 140      140   16   01  LORETO                  MAYNAS    14.501777
#> 141 141      141   16   08  LORETO                PUTUMAYO    20.443604
#>     shape_area                           geom ubigeo
#> 136   1.569481 MULTIPOLYGON (((-76.30752 -...   1602
#> 137   3.881998 MULTIPOLYGON (((-76.49832 -...   1607
#> 138   5.574816 MULTIPOLYGON (((-75.74592 -...   1603
#> 139   3.054652 MULTIPOLYGON (((-72.08996 -...   1604
#> 140   6.197942 MULTIPOLYGON (((-75.24086 -...   1601
#> 141   3.725588 MULTIPOLYGON (((-75.10004 -...   1608

5. Working with Geobosque data

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)
# The first five rows
head(historico_df)
#>   anio perdida rango1 rango2 rango3  rango4  rango5 tipobosque ubigeo
#> 1 2001 2081.97      0   0.00  40.05  519.21 1522.71          1   1602
#> 2 2002 3923.46      0   0.00 136.71 1254.51 2532.24          1   1602
#> 3 2003 1843.92      0   0.00  23.94  584.73 1235.25          1   1602
#> 4 2004 4269.33      0   0.00 351.36 1818.00 2099.97          1   1602
#> 5 2005 4948.20      0   0.00 351.09 2020.50 2576.61          1   1602
#> 6 2006 2274.48      0 209.07 152.64  689.22 1223.55          1   1602

6. Simple visualization with ggplot

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
historico_df |> 
  inner_join(y = loreto_prov,by = "ubigeo") |> 
  ggplot(aes(x = anio,y = perdida)) + 
  geom_point(size = 1) + 
  geom_line() + 
  facet_wrap(nombprov~.,ncol = 3) + 
  theme_minimal(base_size = 5) + 
  labs(
    title = "Pérdida de bosque histórico del 2001-2023 \npara las provincias de Loreto",
    caption = "Fuente: Geobosque",
    x = "",
    y = "")