https://github.com/inbo/grtsdb
Spatially balanced sampling based on the General Randomized Tessellation Stratified (GRTS) using R and SQLite
https://github.com/inbo/grtsdb
grts r r-package r-stats sampling sampling-methods sqlite
Last synced: 5 months ago
JSON representation
Spatially balanced sampling based on the General Randomized Tessellation Stratified (GRTS) using R and SQLite
- Host: GitHub
- URL: https://github.com/inbo/grtsdb
- Owner: inbo
- License: gpl-3.0
- Created: 2019-05-10T07:16:17.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2022-01-10T10:28:34.000Z (over 3 years ago)
- Last Synced: 2025-01-11T19:42:50.919Z (6 months ago)
- Topics: grts, r, r-package, r-stats, sampling, sampling-methods, sqlite
- Language: R
- Homepage: https://inbo.github.io/grtsdb/
- Size: 440 KB
- Stars: 1
- Watchers: 8
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Citation: CITATION.cff
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# grtsdb
[](https://www.repostatus.org/#active)
[](https://lifecycle.r-lib.org/articles/stages.html)

[](https://github.com/inbo/grtsdb/actions)
[](https://app.codecov.io/gh/inbo/grtsdb?branch=master)

The goal of `grtsdb` is to create a spatially balanced sample based on the 'Generalised Random Tesselation Stratified' strategy.
We store the base schema in an SQLite database to make the sampling reproducible.
Sampling the same database with the same parameters yields a stable sample.## Installation
To install the latest stable version use
``` r
# activate the INBO r-universe
options(
repos = c(
INBO = 'https://inbo.r-universe.dev', CRAN = 'https://cloud.r-project.org'
)
)
install.packages("grtsdb")
```You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("remotes")
remotes::install_github("inbo/grtsdb")
```## Example
This is a basic example.
Connect to a database.
```{r}
tmp_copy <- tempfile(pattern = "grts", fileext = ".sqlite")
if (system.file("grts.sqlite", package = "grtsdb") != "") {
file.copy(system.file("grts.sqlite", package = "grtsdb"), tmp_copy)
}
library(grtsdb)
db <- connect_db(tmp_copy)
```To extract a sample, you'll need to specify the bounding box in projected coordinates and the size of the grid cells.
```{r example}
bbox <- rbind(
c(0, 32),
c(0, 32)
)
extract_sample(grtsdb = db, samplesize = 10, bbox = bbox, cellsize = 1)
```Repeating the sample yields the same results.
```{r}
extract_sample(grtsdb = db, samplesize = 10, bbox = bbox, cellsize = 1)
``````{r echo = FALSE}
drop_legacy_sites(grtsdb = db, level = 5)
```You can add legacy sites to the sampling scheme.
```{r}
legacy <- rbind(
c(4, 4),
c(17, 6)
)
add_legacy_sites(legacy, bbox = bbox, cellsize = 1, grtsdb = db)
extract_legacy_sample(grtsdb = db, samplesize = 10, bbox = bbox, cellsize = 1)
```You can compact the database for storage.
```{r}
compact_db(db)
```Disconnect the database when done.
```{r}
dbDisconnect(db)
``````{r eval = system.file("grts.sqlite", package = "grtsdb") == "", echo = FALSE}
dir.create("inst", showWarnings = FALSE)
file.copy(tmp_copy, file.path("inst", "grts.sqlite"))
```