-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
99 lines (69 loc) · 2.61 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# vaster
<!-- badges: start -->
[![R-CMD-check](https://github.com/hypertidy/vaster/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/hypertidy/vaster/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
The goal of vaster is to provide grid logic without complication added by data and format details.
## Installation
You can install the development version of vaster from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("hypertidy/vaster")
```
See?
```{r vaster}
library(vaster)
set.seed(1)
x <- c(sort(runif(2, -20, 160)), sort(runif(2, -80, 10)))
names(x) <- c("xmin", "xmax", "ymin", "ymax")
print(x)
## all we need is a extent and dimension, we want to align to that grid
v <- vcrop(x, c(360, 180) /3, extent = c(-180, 180, -90, 90))
plot(NA, xlim = v$extent[1:2], ylim = v$extent[3:4], asp = "")
g_along <- function(x, n) seq(x[1], x[2], length.out = n)
abline(v = v$extent[1:2], h = v$extent[3:4], lwd = 2)
abline(v = g_along(v$extent[1:2], v$dimension[1]), h = g_along(v$extent[3:4], v$dimension[2]), col = "grey")
## these points were used to crop the existing grid, they don't define its alignment
points(x[1:2], x[3:4], pch = "+")
```
All of the helper functions from the raster package are included, and every function is one of dimension, extent (when needed) in that order.
A grid of the world in 9x5 degree squares, we only need the dimension and extent to get the corners in x, y.
```{r world}
ex <- c(-180, 180, -90, 90)
dm <- c(40, 36)
plot_extent(ex)
abline(v = x_corner(dm, ex), h = y_corner(dm, ex))
```
Now add the centre points.
```{r points}
plot_extent(ex)
points(x_centre(dm, ex), rep(y_centre(dm, ex)[1], length.out = dm[1]))
```
We only get the margins from x_corner/x_centre so we go to a cell based function.
```{r cells}
## how many cells?
cells <- seq_len(prod(dm))
plot_extent(ex)
points(xy_from_cell(dm, ex, cells))
```
Other functions return cells.
```{r query}
xy <- cbind(runif(50, -180, 180), runif(50, -90, 90))
cells <- cell_from_xy(dm, ex, xy)
plot_extent(ex)
points(xy_from_cell(dm, ex, cells), col = "red")
points(xy, pch = "+")
```
## Code of Conduct
Please note that the vaster project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.