https://github.com/mdsumner/grainy
Raster logic, in an object
https://github.com/mdsumner/grainy
Last synced: 7 months ago
JSON representation
Raster logic, in an object
- Host: GitHub
- URL: https://github.com/mdsumner/grainy
- Owner: mdsumner
- License: other
- Created: 2022-11-04T02:21:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-06T09:50:53.000Z (over 3 years ago)
- Last Synced: 2024-12-28T12:17:04.532Z (over 1 year ago)
- Language: R
- Homepage:
- Size: 141 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
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%"
)
```
# grainy
[](https://github.com/mdsumner/grainy/actions/workflows/R-CMD-check.yaml)
The goal of grainy is to encapsulate the logic of a raster grid in an object.
## Installation
You can install the development version of grainy like so:
``` r
remotes::install_github("mdsumner/grainy")
```
## Example
This is a basic example which shows you how to solve a common problem:
```{r example}
library(grainy)
g <- grain$new(c(360, 180), c(-180, 180, -90, 90))
g$cell_from_xy(cbind(0, 0))
## compare to raster/terra/stars/etc
#library(raster)
#cellFromXY(raster(extent(-180, 180, -90, 90), ncols = 360, nrows = 180), cbind(0, 0))
```
This means we can encapsulate this basic information and use it conveniently.
```{r raster}
u <- "/vsicurl/https://opentopography.s3.sdsc.edu/raster/COP30/COP30_hh.vrt"
## get information about a raster
info <- vapour::vapour_raster_info(u)
COP30 <- grain$new(info$dimension, info$extent)
```
What is the extent of COP30?
```{r extent}
COP30$plot_extent()
```
What exact region would we get from a vague query?
```{r extent1}
ex <- COP30$vcrop(c(144.3578, 148.374, -43.7, -41))
region <- grain$new(ex$dimension, ex$extent)
COP30$plot_extent()
region$plot_extent(add = TRUE)
```
Let's get that data but at reduced resolution.
```{r reduced}
reduced <- grain$new(region$dimension %/% 30, region$extent)
v <- vapour::vapour_warp_raster_int(u, extent = reduced$extent, dimension = reduced$dimension)
str(v)
xyz <- reduced$listxyz(data = matrix(v, reduced$dimension[2], byrow = TRUE))
image(xyz, asp = 1/cos(mean(xyz$y) * pi/180), useRaster = TRUE)
maps::map(add = TRUE)
```
Treat the VRT like a list of files, and zoom in on our region for the COG urls of interest.
```{r zoom }
u <- "/vsicurl/https://opentopography.s3.sdsc.edu/raster/COP30/COP30_hh.vrt"
## ok so what have we got
info <- vapour::vapour_raster_info(u)
exts <- vaster::extent_vrt(gsub("/vsicurl/", "", u))
g <- grain$new(info$dimension, info$extent)
g$plot_extent()
region0 <- g$vcrop(c(144.3578, 148.374, -43.7, -41))
region <- grain$new(region0$dimension, region0$extent)
rect(exts[,1] ,exts[,3], exts[,2], exts[,4])
## each row in ext corresponds to an element in info$filelist[-1] so
files <- tibble::tibble(dsn = info$filelist[-1], xmin = exts[,1], xmax = exts[,2], ymin = exts[,3], ymax = exts[,4])
localfiles <- files |> dplyr::filter(xmin >= region$x_min(), xmax <= region$x_max(),
ymin >= region$y_min(), ymax <= region$y_max())
## it's a bit quicker to use the only files involved, but not much
v <- vapour::vapour_warp_raster_int(localfiles$dsn, extent = region$extent, dimension = region$dimension)
#v <- vapour::vapour_warp_raster_int(u, extent = region$extent, dimension = region$dimension)
```
## Code of Conduct
Please note that the grainy 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.