-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
119 lines (85 loc) · 3.99 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
---
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%"
)
library(terra)
```
# fetchr <img src="man/figures/fetchr_logo_transparent.png" align="right" width="25%" />
<!-- badges: start -->
[](#)
[](https://choosealicense.com/licenses/mit/)
<!-- badges: end -->
<div align="left">
<p align="left">
<a href="https://en.wikipedia.org/wiki/Wind_fetch"><strong>« Fetch »</strong></a>
<br />
</p>
</div>
<hr>
The goal of `fetchr` is to provide a fast and efficient raster based method for calculating fetch lengths across thousands of water grid cells.
Calculating fetch lengths, the distance that wind can blow in a constant direction over a body of water without interruption, can be a slow and memory intensive process when done on thousands of points of interest in multiple directions. `fetchr` attempts to fix this problem and allows for thousands of fetch calculations to be performed in a fraction of the time that other methods take.
<hr>
## Installation
You can install the development version of **`fetchr`** from [GitHub](https://github.com/anguswg-ucsb/fetchr) with:
``` r
# install.packages("devtools")
devtools::install_github("anguswg-ucsb/fetchr")
```
## Coastline raster
If we start with a raster representing the Southern California coast near Santa Barbara, CA.
```{r example}
library(fetchr)
# land raster
land_rast <- terra::rast(fetchr::land)
terra::plot(land_rast, col = "#2e8b57")
```
<br>
## Make a binary land water rasters
We can take this land raster, indicate which cells are water cells, and create a binary land water raster (land = 0, water = 1).
```{r binary_lw}
# binary land water raster
landwater <- fetchr::get_landwater(
r = land_rast, # land raster
water_value = NA, # cells with a value of NA are classified as water, all other cells are land
res = terra::res(land_rast)[1] # return raster with the same cell resolution as input raster
)
terra::plot(landwater, col = c("#2e8b57", "#add8e6"))
```
This raster now meets all the specification for using `get_fetch()`:
- Binary cell values (land cells = 0 and water cells = 1)
- Projected Coordinate Reference System
- Regular grid cell size (same x and y cell resolution)
<br>
## Calculate fetch length
Internally, `get_fetch()` will coerce polygon/multipolygon geometries and rasters into the required binary landwater raster. It is recommended to provide either an `sf`/`terra` polygon or a `raster`/`terra` raster with a single value for land cells and NA values for water cells. To calculate fetch distances, we can simply provide an `sf`/`terra` polygon or `raster`/`terra` raster to `get_fetch()`
```{r fetch_calc}
system.time(
fetch <- fetchr::get_fetch(
r = land_rast, # binary land water raster
max_dist = 200000, # maximum distance to calculate fetch in meters (200km)
in_parallel = TRUE, # run calculations in parallel
verbose = TRUE
)
)
plot(fetch)
```
In this example here, my machine calculated fetch distances for > 24,000 water cells in about ~3 seconds, or ~0.00014 seconds per point. That is a ~99.99% reduction in computation time compared to various other polygon based methods out there!
<br>
## Multidirectional fetch calculations
The `get_fetch_directions()` function allows you to get get individual fetch distances in 8 directions:
```{r multi_fetch_calc}
multi_fetch <- fetchr::get_fetch_directions(
r = land_rast, # binary land water raster
max_dist = 200000, # maximum distance to calculate fetch in meters (200km)
in_parallel = TRUE, # run calculations in parallel
verbose = TRUE
)
plot(multi_fetch)
```