Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soilmap: early exploration + creation of soilmap_simple #34

Merged
merged 11 commits into from
Apr 10, 2020
1 change: 1 addition & 0 deletions src/generate_soilmap_simple/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
85 changes: 85 additions & 0 deletions src/generate_soilmap_simple/10_soilmap_simple.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Making a simple version of the `soilmap` data source

As we incorporated two handles for processing the raw `soilmap` data source in the `read_soilmap()` function,
we can implement directly as follows:

```{r}
soilmap_simple <- read_soilmap(use_processed = FALSE,
standardize_coastalplain = TRUE,
simplify = TRUE,
explan = TRUE)
```

# Split off a lookup table with factor levels & their explanations

```{r}
soilmap_df <-
soilmap_simple %>%
st_drop_geometry %>%
select(contains("bsm_mo"), -bsm_mo_soilunitype)
subjects <- soilmap_df %>%
select(-contains("_explan")) %>%
colnames
explanations <- tibble()
for (i in 1:6) {
explanations <-
bind_rows(explanations,
tibble(subject = subjects[i],
code = soilmap_df %>%
pull((i * 2 - 1)) %>%
levels,
name = soilmap_df %>%
pull((i * 2)) %>%
levels
)
)
}
```

```{r}
soilmap_simple <-
soilmap_simple %>%
select(-contains("_explan"))
```

```{r paged.print=FALSE}
soilmap_simple
```


# Writing the result as a GeoPackage

```{r}
datapath <- fileman_up("n2khab_data")
```

```{r}
dir.create(file.path(datapath, "20_processed/soilmap_simple"), recursive = TRUE)
```


```{r}
st_write(soilmap_simple,
file.path(datapath,
"20_processed/soilmap_simple/soilmap_simple.gpkg"),
layer = "soilmap_simple",
driver = "GPKG",
delete_dsn = TRUE)

gpkg_contents_add <-
tribble(
~table_name, ~data_type, ~identifier,
"explanations", "aspatial", "explanations"
)

con <- dbConnect(RSQLite::SQLite(),
dbname = file.path(datapath,
"20_processed/soilmap_simple/soilmap_simple.gpkg"))

dbWriteTable(con, "explanations", explanations)
devnull <- dbAppendTable(con, "gpkg_contents", gpkg_contents_add)

dbDisconnect(con)
```


19 changes: 19 additions & 0 deletions src/generate_soilmap_simple/99_sessioninfo.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Used environment

```{r session-info, results = "asis", echo=FALSE}
si <- devtools::session_info()
p <- si$platform %>%
do.call(what = "c")
sprintf("- **%s**:\n %s\n", names(p), p) %>%
cat()
```

```{r results = "asis", echo=FALSE}
si$packages %>%
as_tibble %>%
select(package, loadedversion, date, source) %>%
pander::pandoc.table(caption = "(\\#tab:sessioninfo)Loaded R packages",
split.table = Inf)
```


4 changes: 4 additions & 0 deletions src/generate_soilmap_simple/_bookdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
book_filename: "generate_soilmap_simple.Rmd"
new_session: FALSE
rmd_files: # specifies the order of Rmd files; NOT needed when you use index.Rmd and an alphabetical order for other Rmd files
# - index.Rmd
18 changes: 18 additions & 0 deletions src/generate_soilmap_simple/generate_soilmap_simple.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 4
Encoding: UTF-8

RnwWeave: knitr
LaTeX: XeLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Website
61 changes: 61 additions & 0 deletions src/generate_soilmap_simple/index.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: "Generating a simplified form of the soilmap"
# subtitle: "x"
date: "`r lubridate::now()`"
link-citations: true
linkcolor: link.colour
citecolor: link.colour
urlcolor: link.colour
geometry: margin=1in
mainfont: "Calibri"
fontsize: 11pt
documentclass: "article"
# csl: ../inbo.csl
# bibliography: ../references.bib
site: bookdown::bookdown_site
output:
bookdown::html_document2:
keep_md: TRUE
number_sections: yes
fig_caption: yes
df_print: paged
toc: TRUE
toc_float:
collapsed: FALSE
smooth_scroll: FALSE
includes:
in_header: ../header.html
bookdown::pdf_document2:
fig_caption: yes
keep_tex: yes
toc: yes
toc_depth: 3
latex_engine: xelatex
number_sections: true
includes:
in_header: ../header.tex
---

```{r setup, include=FALSE}
options(stringsAsFactors = FALSE,
scipen = 999,
digits = 15)
library(tidyverse)
library(stringr)
library(knitr)
library(n2khab)
library(sf)
library(DBI)
opts_chunk$set(
echo = TRUE,
dpi = 300,
paged.print = FALSE
)
```

**Note: this is a bookdown project, supposed to be run from within the `src/generate_soilmap_simple` subfolder. You can use the `generate_soilmap_simple` RStudio project file in this subfolder to run it.**





Loading