---
title: "Get started"
description: "Introduction to CatastroNav"
vignette: >
  %\VignetteIndexEntry{Get started}
  %\VignetteEngine{quarto::html}
  %\VignetteEncoding{UTF-8}
bibliography: REFERENCES.bib
link-citations: true
---



<!-- CatastRoNav.qmd is generated from CatastRoNav.qmd.orig. Please edit that file -->

**CatastRoNav** is a package that provides access to different INSPIRE API
services of the [Cadastre of Navarre](https://geoportal.navarra.es/es/idena).
With **CatastRoNav**, you can download spatial objects such as buildings or
cadastral parcels.

## INSPIRE Services

> The INSPIRE Directive aims to create a European Union spatial data
> infrastructure for the purposes of EU environmental policies and policies or
> activities which may have an impact on the environment. This European Spatial
> Data Infrastructure will enable the sharing of environmental spatial
> information among public sector organisations, facilitate public access to
> spatial information across Europe and assist in policy-making across
> boundaries.
>
> *From <https://knowledge-base.inspire.ec.europa.eu/index_en>*

The implementation of the INSPIRE directive on the Cadastre of Navarre allows
retrieving spatial objects from the cadastre database:

- **Vector objects:** Parcels, addresses, buildings, cadastral zones and more.
  These objects are provided by **CatastRoNav** as `sf` objects (see
  `?sf::st_sf`).

## Examples

In this example, we retrieve the cadastral parcels of
[Olite](https://en.wikipedia.org/wiki/Olite):


``` r
library(CatastRoNav)
# For obtaining coordinates
library(sf)
library(mapSpain)
# Data wrangling and visualization
library(dplyr)
library(ggplot2)

olite <- esp_get_capimun(munic = "Olite") |>
  st_transform(25830) |>
  # Small buffer of 100 m
  st_buffer(100)


cp <- catrnav_wfs_get_parcels_bbox(olite)

ggplot(cp) +
  geom_sf()
```

<div class="figure">
<img src="./olite-1.png" alt="Figure 1: Example - Olite" width="100%" />
<p class="caption">Figure 1: Example - Olite</p>
</div>

### Thematic maps

We can also create thematic maps using information available in the spatial
objects. Here we produce a visualization of the urban growth of Pamplona using
**CatastRoNav**, replicating the map produced by [Dominic
Royé](https://dominicroye.github.io) [@roye19].

First, we extract the coordinates of the city center of Pamplona using
**mapSpain**:


``` r
# Use mapSpain to obtain coordinates
pamp <- esp_get_capimun(munic = "^Pamplona")

# Transform to ETRS89 / UTM 30 N and add a buffer of 750m

pamp_buff <- pamp |>
  st_transform(25830) |>
  st_buffer(1250)
```

The next step is to extract buildings using the WFS service:


``` r
pamp_bu <- catrnav_wfs_get_buildings_bbox(pamp_buff, count = 10000)
```

Then crop the buildings to the buffer created earlier:


``` r
# Cut buildings

dataviz <- st_intersection(pamp_bu, pamp_buff)

ggplot(dataviz) +
  geom_sf()
```

<div class="figure">
<img src="./minimal-1.png" alt="Figure 2: Minimal map of Pamplona" width="100%" />
<p class="caption">Figure 2: Minimal map of Pamplona</p>
</div>

Next, extract the construction year from the `beginning` column.


``` r
# Extract 4 initial positions
year <- substr(dataviz$beginning, 1, 4)

# Replace values that do not look like numbers with "0000".
year[!(year %in% 0:2500)] <- "0000"

# Convert to numeric
year <- as.integer(year)

# Create new column
dataviz <- dataviz |>
  mutate(year = year)
```

The last step is to group the data by year and build the visualization. Here we
use the function `ggplot2::cut_width()` to create classes:


``` r
dataviz <- dataviz |>
  mutate(year_cat = ggplot2::cut_width(year, width = 10, dig.lab = 12))

# Adjust the color palette

dataviz_pal <- hcl.colors(length(levels(dataviz$year_cat)), "Spectral")

ggplot(dataviz) +
  geom_sf(aes(fill = year_cat), color = NA) +
  scale_fill_manual(values = dataviz_pal) +
  theme_void() +
  labs(title = "PAMPLONA", fill = "") +
  theme(
    panel.background = element_rect(fill = "black"),
    plot.background = element_rect(fill = "black"),
    legend.justification = .5,
    legend.text = element_text(
      colour = "white",
      size = 12
    ),
    plot.title = element_text(
      colour = "white", hjust = .5,
      margin = margin(t = 30),
      size = 30
    ),
    plot.caption = element_text(
      colour = "white",
      margin = margin(b = 20), hjust = .5
    ),
    plot.margin = margin(r = 40, l = 40)
  )
```

<div class="figure">
<img src="./dataviz-1.png" alt="Figure 3: Pamplona - Urban Growth" width="100%" />
<p class="caption">Figure 3: Pamplona - Urban Growth</p>
</div>

## References
