Skip to content

Commit

Permalink
Generate watercourse_100mseg & watercourse_100msegpoints: initial code
Browse files Browse the repository at this point in the history
florisvdh committed Nov 27, 2020
1 parent 808155e commit d841380
Showing 6 changed files with 190 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -19,3 +19,5 @@ src/private_code.R
*.qgs
*.bak
*.qgz
grass_*/
watercourse_segments/
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Making the data source

We will create a master dataset of watercourse segments (`watercourse_100mseg`), in order to derive sampling frames of lotic types (3260 in particular).

It is a processed data source, derived from the raw data source `watercourse_segments`.
The segments have a 100 m length, except at the end of the linestrings in `watercourse_segments`.

Further we derive a second data source `watercourse_100msegpoints` which represent the endpoints of the segments.

```{r}
local_root <- find_root(has_file("generate_watercourse_100mseg.Rproj"))
datapath <- file.path(local_root, "data")
if (!dir.exists(datapath)) dir.create(datapath)
finalpath_segments <- find_root_file("n2khab_data/20_processed/watercourse_100mseg",
criterion = has_dir("n2khab_data"))
if (!dir.exists(finalpath_segments)) dir.create(finalpath_segments)
finalpath_segmentpoints <- find_root_file("n2khab_data/20_processed/watercourse_100msegpoints",
criterion = has_dir("n2khab_data"))
if (!dir.exists(finalpath_segmentpoints)) dir.create(finalpath_segmentpoints)
```

```{r}
grass_location <- file.path(datapath, "grass_watercourse_segments")
if (!dir.exists(grass_location)) dir.create(file.path(grass_location, "PERMANENT"),
recursive = TRUE)
grass_exists <- file.exists(file.path(grass_location, "PERMANENT/PROJ_INFO"))
if (.Platform$OS.type == "unix") {
Sys.setenv(GRASS_PYTHON = system("which python3", intern = TRUE))
}
```


## Geoprocessing steps in GRASS

<!-- Read the `watercourse_segments` data source (this needs improvement). -->

<!-- ```{r paged.print=FALSE} -->
<!-- watercourse_segments <- read_sf("data/watercourse_segments") -->
<!-- ``` -->

Initialize GRASS project if non-existant:

```{r eval=!grass_exists}
initGRASS(gisBase = link2GI::findGRASS()[1, 1],
home = tempdir(),
gisDbase = datapath, location = grass_location,
mapset = "PERMANENT", override = TRUE)
execGRASS("g.proj", flags = c("c", "quiet"),
georef = file.path(datapath, "watercourse_segments"))
b_box <- st_bbox(watercourse_segments)
execGRASS("g.region", flags = c("quiet"),
n = as.character(b_box["ymax"]), s = as.character(b_box["ymin"]),
e = as.character(b_box["xmax"]), w = as.character(b_box["xmin"]),
res = "1")
use_sf()
```

Add `watercourse_segments` data source to GRASS:

```{r eval=!grass_exists}
execGRASS("v.in.ogr",
input = file.path(datapath, "watercourse_segments"),
output = "watercourse_segments")
```

### Generate `watercourse_100mseg` in GRASS

```{r eval=!grass_exists}
execGRASS("v.split",
flags = c("f", "overwrite"),
input = "watercourse_segments",
output = "watercourse_100mseg",
length = 100)
```

```{r eval=!grass_exists}
system("v.category input=watercourse_100mseg option=report")
```



### Generate `watercourse_100msegpoints` in GRASS

```{r eval=!grass_exists}
execGRASS("v.to.points",
flags = c("overwrite"),
input = "watercourse_100mseg",
output = "watercourse_100msegpoints",
use = "end")
```

# Checks on the data source

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

```{r session-info, results = "asis", echo=FALSE}
si <- devtools::session_info()
p <- si$platform %>%
do.call(what = "c")
if ("sf" %in% si$packages$package) p <- c(p, sf_extSoftVersion())
if ("rgrass7" %in% si$packages$package) {
p <- c(p, GRASS = link2GI::findGRASS()[1, "version"])
}
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_watercourse_100mseg/_bookdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
book_filename: "generating_watercourse_segments.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
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: 2
Encoding: UTF-8

RnwWeave: knitr
LaTeX: XeLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Website
51 changes: 51 additions & 0 deletions src/generate_watercourse_100mseg/index.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: "Generate watercourse segments"
# 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:
code_folding: show
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
---

```{r setup, include=FALSE}
library(sf)
library(dplyr)
library(knitr)
library(rprojroot)
library(ggplot2)
library(rgrass7)
opts_chunk$set(
echo = TRUE,
dpi = 300
)
library(mapview)
mapviewOptions(fgb = FALSE)
```

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





0 comments on commit d841380

Please sign in to comment.