Skip to content

Commit 4373a5e

Browse files
committed
Add tests
1 parent b5be983 commit 4373a5e

5 files changed

Lines changed: 146 additions & 37 deletions

File tree

.gitattributes

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
*.qmd linguist-language=R
44

5-
images/* linguist-documentation
6-
docs/* linguist-documentation
7-
ttf/* linguist-documentation
5+
images/** linguist-documentation
6+
docs/** linguist-documentation
7+
ttf/** linguist-documentation

R/loop-geobr.R

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,36 @@ year <- 2024
4343
# sort()
4444
# )
4545

46-
code <- c(
47-
double_quote("all"),
48-
c(
49-
2507507, # João Pessoa (PB)
50-
3106200, # Belo Horizonte (MG)
51-
3550308, # São Paulo (SP)
52-
4314902, # Porto Alegre (RS)
53-
1501402 # Belém (PA)
54-
)
55-
)
56-
57-
# code <-
58-
# read_state(
59-
# "all",
60-
# year = 2020
61-
# ) |>
62-
# arrange(1) |>
63-
# pull(1) %>%
64-
# c(double_quote("all"), .)
65-
66-
# code <-
67-
# here("pmtiles", "geobr", "read_municipality") |>
68-
# dir_ls(type = "file") |>
69-
# basename() |>
70-
# str_extract("\\d+") |>
71-
# as.integer() |>
72-
# unique() |>
73-
# sort() %>%
74-
# setdiff(code, .)
46+
# code <- c(
47+
# double_quote("all"),
48+
# c(
49+
# 2507507, # João Pessoa (PB)
50+
# 3106200, # Belo Horizonte (MG)
51+
# 3550308, # São Paulo (SP)
52+
# 4314902, # Porto Alegre (RS)
53+
# 1501402 # Belém (PA)
54+
# )
55+
# )
7556

76-
# length(code)
57+
code <-
58+
read_municipality(year = year) |>
59+
arrange(1) |>
60+
pull(1) # %>%
61+
# c(double_quote("all"), .)
62+
63+
code <-
64+
here("pmtiles", "geobr", "read_municipality") |>
65+
dir_ls(type = "file") |>
66+
basename() |>
67+
str_subset(paste0("year-", year)) |>
68+
str_extract("\\d+") |>
69+
as.integer() |>
70+
unique() |>
71+
sort() %>%
72+
setdiff(code, .) |>
73+
sort()
74+
75+
length(code)
7776

7877
# Replace Fixed Values -----
7978

qmd/geobr.qmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ for (i in c(data_dir)) {
8080
}
8181
```
8282

83-
### Set Initial Variables
83+
### Set Initial Parameters
8484

8585
::: {.callout-note}
8686
The `code` variable specifies the IBGE code of the Brazilian territorial unit to retrieve. Use `"all"` to retrieve all units. For the full list of IBGE codes and their corresponding territorial units, see <https://sidra.ibge.gov.br/territorio>.
@@ -92,7 +92,7 @@ The `code` variable specifies the IBGE code of the Brazilian territorial unit to
9292
# brazil_municipality_code("São Paulo")
9393
# "all" to select all units
9494
95-
code <- 1501402
95+
code <- 5106000
9696
```
9797

9898
::: {.callout-note}
@@ -128,7 +128,7 @@ The `h3jsr` variable specifies whether to process the spatial data with the [`h3
128128
:::
129129

130130
```{r}
131-
h3jsr <- TRUE
131+
h3jsr <- FALSE
132132
```
133133

134134
::: {.callout-note}

qmd/geodata.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ for (i in c(data_dir, raw_data_dir)) {
8080
}
8181
```
8282

83-
### Set Initial Variables
83+
### Set Initial Parameters
8484

8585
::: {.callout-note}
8686
The `country` variable specifies which country to retrieve. Use a three-letter country code that follows the [ISO 3166-1 alpha-3](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) standard (e.g., `BRA`, `ITA`, or `USA`).

qmd/tests.qmd

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
```{r}
2+
#| label: setup
3+
#| include: false
4+
5+
library(here)
6+
7+
here("R", ".setup.R") |> source()
8+
```
9+
10+
## Overview
11+
12+
This document describes the testing process used after the creation of the [PMTiles](https://docs.protomaps.com/pmtiles/) files.
13+
14+
## Methods
15+
16+
### Code Style
17+
18+
The Tidyverse [tidy tools manifesto](https://tidyverse.tidyverse.org/articles/manifesto.html) [@wickham2023c], [code style guide](https://style.tidyverse.org/) [@wickhama] and [design principles](https://design.tidyverse.org/) [@wickhamc] were followed to ensure consistency and enhance readability.
19+
20+
### Reproducibility
21+
22+
The pipeline is fully reproducible and can be run again at any time. To ensure consistent results, the [`renv`](https://rstudio.github.io/renv/) package [@ushey2025] is used to manage and restore the R environment. See the [README](https://github.com/cem-usp/pmtilesbr/blob/main/README.md) file in the code repository to learn how to run it.
23+
24+
## Set the Environment
25+
26+
### Load Packages
27+
28+
```{r}
29+
#| label: Set the Environment
30+
#| output: false
31+
32+
library(cli)
33+
library(fs)
34+
library(here)
35+
library(pmtiles) # github.com/walkerke/pmtiles
36+
library(stringr)
37+
```
38+
39+
### Set Data Directories
40+
41+
```{r}
42+
raw_data_dir <- here("data-raw")
43+
data_dir <- here("pmtiles")
44+
```
45+
46+
```{r}
47+
for (i in c(data_dir, raw_data_dir)) {
48+
if (!dir_exists(i)) {
49+
dir_create(i, recurse = TRUE)
50+
}
51+
}
52+
```
53+
54+
### Set Initial Parameters
55+
56+
::: {.callout-note}
57+
The `year` variable specifies the year of the data.
58+
:::
59+
60+
```{r}
61+
year <- 2024
62+
```
63+
64+
```{r}
65+
pmtiles_dir <- data_dir
66+
```
67+
68+
## Test and Delete Corrupted PMTiles
69+
70+
```{r}
71+
#| label: Test and Delete Corrupted PMTiles
72+
73+
files <-
74+
pmtiles_dir |>
75+
dir_ls(
76+
type = "file",
77+
recurse = TRUE,
78+
glob = "*.pmtiles"
79+
) |>
80+
str_subset(
81+
paste0("year-", year)
82+
)
83+
```
84+
85+
```{r}
86+
cli_progress_bar(
87+
name = "Testing PMTiles",
88+
total = length(files)
89+
)
90+
91+
for (i in files) {
92+
test <-
93+
i |>
94+
pm_show(tilejson = TRUE) |>
95+
try(silent = TRUE)
96+
97+
if (inherits(test, "try-error")) {
98+
file_delete(i)
99+
}
100+
101+
cli_progress_update()
102+
}
103+
104+
cli_progress_done()
105+
```
106+
107+
## References {.unnumbered}
108+
109+
::: {#refs}
110+
:::

0 commit comments

Comments
 (0)