Skip to content

Commit 2d9e840

Browse files
committed
update readme
1 parent f9c693b commit 2d9e840

File tree

2 files changed

+123
-16
lines changed

2 files changed

+123
-16
lines changed

README.Rmd

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,80 @@ The goal of vaster is to provide grid logic without complication added by data a
2323

2424
## Installation
2525

26+
Install vaster from CRAN with
27+
28+
```r
29+
install.packages("vaster")
30+
```
31+
2632
You can install the development version of vaster from [GitHub](https://github.com/) with:
2733

2834
``` r
2935
# install.packages("devtools")
3036
devtools::install_github("hypertidy/vaster")
3137
```
3238

39+
## Grid logic
3340

41+
Here are four numbers, that represent two positions in the plane.
42+
43+
On their own they are very unlikely to align on a raster grid, but they provide a starting point for a "region of interest". Notice in this picture that the underlying discretization and the position of these points is unrelated, and can be arbitrary - but they are related in terms of describing *approximately* the same region. If we make our grid window too small we will miss part of the world that the points are in, you can see that we want to "snap out" at least one grid cell to the left and down, to the right and up.
3444

35-
See?
3645

3746
```{r vaster}
3847
library(vaster)
3948
set.seed(1)
4049
x <- c(sort(runif(2, -20, 160)), sort(runif(2, -80, 10)))
4150
names(x) <- c("xmin", "xmax", "ymin", "ymax")
4251
print(x)
52+
plot(matrix(x, ncol = 2))
53+
grid()
54+
55+
```
56+
57+
This is where vaster comes in, tools to relate arbitrary point locations to an underlying grid.
58+
59+
60+
vaster consists of a set of low-level functions for this relationship between point locations and grid elements.
61+
62+
The `vcrop()` function is a high-level function that uses these underlying tools. Imagine a grid that is 120x60 and includes the whole world in longlat, this is described by dimension (120x60) and by extent (-180, 180, -90, 90). With `vcrop()` we get exactly the right snapping for our points, identifying the exact extent we need to "snap out" that little bit as mentioned above. The discretization can be completely arbitrary, as it can be in geospatial packages.
63+
64+
```{r vaster1}
4365
## all we need is a extent and dimension, we want to align to that grid
44-
v <- vcrop(x, c(360, 180) /3, extent = c(-180, 180, -90, 90))
66+
v <- vcrop(x, c(120, 60), extent = c(-180, 180, -90, 90))
4567
plot(NA, xlim = v$extent[1:2], ylim = v$extent[3:4], asp = "")
46-
g_along <- function(x, n) seq(x[1], x[2], length.out = n)
68+
g_along <- function(x, n) seq(x[1], x[2], length.out = n + 1)
4769
abline(v = v$extent[1:2], h = v$extent[3:4], lwd = 2)
4870
abline(v = g_along(v$extent[1:2], v$dimension[1]), h = g_along(v$extent[3:4], v$dimension[2]), col = "grey")
4971
72+
73+
5074
## these points were used to crop the existing grid, they don't define its alignment
5175
points(x[1:2], x[3:4], pch = "+")
5276
```
5377

54-
All of the helper functions from the raster package are included, and every function is one of dimension, extent (when needed) in that order.
78+
That is the core of vaster - this is *raster logic* that exists in geospatial libraries and packages, but it has use on its own without any imagery, or grid data, or files or anything else.
79+
vaster was originally based on the abstract cell functions of the old [raster](https://CRAN.R-project.org/package=raster), which relied on its internal format - these are now superseded by the [terra](https://CRAN.R-project.org/package=terra) package - but we don't want to rely on its new format, or be subject to future changes so vaster has the logic separated from other tooling.
80+
81+
Most functions in vaster include arguments that 'dimension', and 'extent' (when needed) in that order.
82+
83+
We use the concept of "extent" which is xmin,xmax,ymin,ymax which is exactly the same as the concept of "bounding box (bbox)" which is xmin,ymin,xmax,ymax: the same thing in a different order.
84+
85+
## Another example
5586

5687
A grid of the world in 9x5 degree squares, we only need the dimension and extent to get the corners in x, y.
5788

89+
With `plot_extent()` we have a very simple way to plot a rectangular region with one argument.
90+
5891
```{r world}
5992
ex <- c(-180, 180, -90, 90)
6093
dm <- c(40, 36)
6194
62-
plot_extent(ex)
95+
plot_extent(ex, lwd = 5, border = "firebrick")
6396
abline(v = x_corner(dm, ex), h = y_corner(dm, ex))
6497
```
6598

99+
With `x_corner()`, `y_corner()`, `x_centre()`, and `y_centre()` we need only those six numbers, extent and dimension to get the corner and cell centre positions.
66100

67101
Now add the centre points.
68102

@@ -71,7 +105,7 @@ plot_extent(ex)
71105
points(x_centre(dm, ex), rep(y_centre(dm, ex)[1], length.out = dm[1]))
72106
```
73107

74-
We only get the margins from x_corner/x_centre so we go to a cell based function.
108+
We only get the margins from x_corner/x_centre so we go to a cell based function, `xy_from_cell()` returns any centre position indexed by its cell number (cell numbers are 1-based and traverse from topleft to the right, then down by row - this is the same as in raster/terra and is the orientation used by most geospatial tools).
75109

76110
```{r cells}
77111
## how many cells?
@@ -81,7 +115,7 @@ points(xy_from_cell(dm, ex, cells))
81115
```
82116

83117

84-
Other functions return cells.
118+
Other functions return cells, here we have another kind of "snap" operation, which cell does the point fall it (or which cell centre is it closest to).
85119

86120

87121
```{r query}

README.md

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ by data and format details.
1313

1414
## Installation
1515

16+
Install vaster from CRAN with
17+
18+
``` r
19+
install.packages("vaster")
20+
```
21+
1622
You can install the development version of vaster from
1723
[GitHub](https://github.com/) with:
1824

@@ -21,45 +27,107 @@ You can install the development version of vaster from
2127
devtools::install_github("hypertidy/vaster")
2228
```
2329

24-
See?
30+
## Grid logic
31+
32+
Here are four numbers, that represent two positions in the plane.
33+
34+
On their own they are very unlikely to align on a raster grid, but they
35+
provide a starting point for a “region of interest”. Notice in this
36+
picture that the underlying discretization and the position of these
37+
points is unrelated, and can be arbitrary - but they are related in
38+
terms of describing *approximately* the same region. If we make our grid
39+
window too small we will miss part of the world that the points are in,
40+
you can see that we want to “snap out” at least one grid cell to the
41+
left and down, to the right and up.
2542

2643
``` r
2744
library(vaster)
45+
#>
46+
#> Attaching package: 'vaster'
47+
#> The following object is masked from 'package:stats':
48+
#>
49+
#> ts
2850
set.seed(1)
2951
x <- c(sort(runif(2, -20, 160)), sort(runif(2, -80, 10)))
3052
names(x) <- c("xmin", "xmax", "ymin", "ymax")
3153
print(x)
3254
#> xmin xmax ymin ymax
3355
#> 27.791559 46.982302 -28.443197 1.738701
56+
plot(matrix(x, ncol = 2))
57+
grid()
58+
```
59+
60+
<img src="man/figures/README-vaster-1.png" width="100%" />
61+
62+
This is where vaster comes in, tools to relate arbitrary point locations
63+
to an underlying grid.
64+
65+
vaster consists of a set of low-level functions for this relationship
66+
between point locations and grid elements.
67+
68+
The `vcrop()` function is a high-level function that uses these
69+
underlying tools. Imagine a grid that is 120x60 and includes the whole
70+
world in longlat, this is described by dimension (120x60) and by extent
71+
(-180, 180, -90, 90). With `vcrop()` we get exactly the right snapping
72+
for our points, identifying the exact extent we need to “snap out” that
73+
little bit as mentioned above. The discretization can be completely
74+
arbitrary, as it can be in geospatial packages.
75+
76+
``` r
3477
## all we need is a extent and dimension, we want to align to that grid
35-
v <- vcrop(x, c(360, 180) /3, extent = c(-180, 180, -90, 90))
78+
v <- vcrop(x, c(120, 60), extent = c(-180, 180, -90, 90))
3679
plot(NA, xlim = v$extent[1:2], ylim = v$extent[3:4], asp = "")
37-
g_along <- function(x, n) seq(x[1], x[2], length.out = n)
80+
g_along <- function(x, n) seq(x[1], x[2], length.out = n + 1)
3881
abline(v = v$extent[1:2], h = v$extent[3:4], lwd = 2)
3982
abline(v = g_along(v$extent[1:2], v$dimension[1]), h = g_along(v$extent[3:4], v$dimension[2]), col = "grey")
4083

84+
85+
4186
## these points were used to crop the existing grid, they don't define its alignment
4287
points(x[1:2], x[3:4], pch = "+")
4388
```
4489

45-
<img src="man/figures/README-vaster-1.png" width="100%" />
90+
<img src="man/figures/README-vaster1-1.png" width="100%" />
91+
92+
That is the core of vaster - this is *raster logic* that exists in
93+
geospatial libraries and packages, but it has use on its own without any
94+
imagery, or grid data, or files or anything else. vaster was originally
95+
based on the abstract cell functions of the old
96+
[raster](https://CRAN.R-project.org/package=raster), which relied on its
97+
internal format - these are now superseded by the
98+
[terra](https://CRAN.R-project.org/package=terra) package - but we don’t
99+
want to rely on its new format, or be subject to future changes so
100+
vaster has the logic separated from other tooling.
46101

47-
All of the helper functions from the raster package are included, and
48-
every function is one of dimension, extent (when needed) in that order.
102+
Most functions in vaster include arguments that ‘dimension’, and
103+
‘extent’ (when needed) in that order.
104+
105+
We use the concept of “extent” which is xmin,xmax,ymin,ymax which is
106+
exactly the same as the concept of “bounding box (bbox)” which is
107+
xmin,ymin,xmax,ymax: the same thing in a different order.
108+
109+
## Another example
49110

50111
A grid of the world in 9x5 degree squares, we only need the dimension
51112
and extent to get the corners in x, y.
52113

114+
With `plot_extent()` we have a very simple way to plot a rectangular
115+
region with one argument.
116+
53117
``` r
54118
ex <- c(-180, 180, -90, 90)
55119
dm <- c(40, 36)
56120

57-
plot_extent(ex)
121+
plot_extent(ex, lwd = 5, border = "firebrick")
58122
abline(v = x_corner(dm, ex), h = y_corner(dm, ex))
59123
```
60124

61125
<img src="man/figures/README-world-1.png" width="100%" />
62126

127+
With `x_corner()`, `y_corner()`, `x_centre()`, and `y_centre()` we need
128+
only those six numbers, extent and dimension to get the corner and cell
129+
centre positions.
130+
63131
Now add the centre points.
64132

65133
``` r
@@ -70,7 +138,10 @@ points(x_centre(dm, ex), rep(y_centre(dm, ex)[1], length.out = dm[1]))
70138
<img src="man/figures/README-points-1.png" width="100%" />
71139

72140
We only get the margins from x_corner/x_centre so we go to a cell based
73-
function.
141+
function, `xy_from_cell()` returns any centre position indexed by its
142+
cell number (cell numbers are 1-based and traverse from topleft to the
143+
right, then down by row - this is the same as in raster/terra and is the
144+
orientation used by most geospatial tools).
74145

75146
``` r
76147
## how many cells?
@@ -81,7 +152,9 @@ points(xy_from_cell(dm, ex, cells))
81152

82153
<img src="man/figures/README-cells-1.png" width="100%" />
83154

84-
Other functions return cells.
155+
Other functions return cells, here we have another kind of “snap”
156+
operation, which cell does the point fall it (or which cell centre is it
157+
closest to).
85158

86159
``` r
87160
xy <- cbind(runif(50, -180, 180), runif(50, -90, 90))

0 commit comments

Comments
 (0)