Skip to content

Commit 5f329b9

Browse files
committed
basic animation mikejohnson51#71
1 parent 2d9415b commit 5f329b9

File tree

7 files changed

+76
-1
lines changed

7 files changed

+76
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Imports:
1717
dplyr,
1818
future.apply,
1919
glue,
20+
gifski,
2021
ncmeta,
2122
RNetCDF,
2223
terra,

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(animation)
34
export(checkDodsrc)
45
export(checkNetrc)
56
export(climater_filter)
@@ -69,6 +70,7 @@ importFrom(dplyr,select)
6970
importFrom(dplyr,slice)
7071
importFrom(dplyr,ungroup)
7172
importFrom(future.apply,future_lapply)
73+
importFrom(gifski,save_gif)
7274
importFrom(glue,glue)
7375
importFrom(methods,formalArgs)
7476
importFrom(ncmeta,nc_coord_var)
@@ -91,6 +93,7 @@ importFrom(terra,extract)
9193
importFrom(terra,flip)
9294
importFrom(terra,intersect)
9395
importFrom(terra,merge)
96+
importFrom(terra,minmax)
9497
importFrom(terra,nlyr)
9598
importFrom(terra,plot)
9699
importFrom(terra,project)

R/catalog.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
#' @importFrom dplyr filter mutate select distinct `%>%` everything mutate_all bind_rows left_join rename group_by ungroup slice
77
#' @importFrom glue glue
8-
#' @importFrom terra intersect project vect crs ext relate rast crop flip `ext<-` `crs<-` `units<-` `time<-` union sprc merge units nlyr as.polygons plot extract time align ymax ymin xmax xmin
8+
#' @importFrom terra intersect project vect crs ext relate rast crop flip `ext<-` `crs<-` `units<-` `time<-` union sprc merge units nlyr as.polygons plot extract time align ymax ymin xmax xmin plot minmax
99
#' @importFrom RNetCDF open.nc close.nc var.get.nc dim.inq.nc var.inq.nc utcal.nc att.get.nc
1010
#' @importFrom future.apply future_lapply
1111
#' @importFrom ncmeta nc_coord_var nc_grid_mapping_atts nc_gm_to_prj nc_vars nc_var nc_dims
1212
#' @importFrom utils head tail
13+
#' @importFrom gifski save_gif
1314
#' @importFrom methods formalArgs
1415
#' @importFrom stats complete.cases
1516

R/viz.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#' @title Animate SpatRast as GIF
2+
#' @description
3+
#' Animate a SpatRaster object as a gif.
4+
#' @param data a single SpatRast object
5+
#' @param AOI optional AOI sf or SpatVect object to overlay on gif
6+
#' @param outfile path to write gif file, must have .gif extenstion
7+
#' @param colors colors to plot w
8+
#' @return file.path
9+
#' @family viz
10+
#' @export
11+
12+
animation = function(data, AOI = NULL, outfile, colors = blues9){
13+
14+
y = terra::minmax(data, compute = TRUE)
15+
16+
y = y[!is.na(y)]
17+
18+
brk <- seq(min(y), max(y), (max(y) - min(y)) / length(colors))
19+
n = names(data)
20+
21+
gifski::save_gif({
22+
for (t in 1:nlyr(data)) {
23+
try({
24+
terra::plot(
25+
data[[t]],
26+
breaks = brk,
27+
col = colors ,
28+
legend = T,
29+
axes = F,
30+
box = F,
31+
main = n[t]
32+
)
33+
if(!is.null(AOI)){
34+
plot(spatAOI(AOI), col = NULL, add = TRUE)
35+
}
36+
37+
}, silent = F)
38+
}
39+
}, gif_file = outfile, width = 800, height = 600, delay = .5, loop = TRUE)
40+
41+
return(outfile)
42+
}

_pkgdown.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ reference:
4646
desc: Parquet file exported to package dataset
4747
contents:
4848
- has_concept("catalog")
49+
- subtitle: Visualization
50+
desc: Parquet file exported to package dataset
51+
contents:
52+
- has_concept("viz")

man/animation.Rd

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tester.gif

229 KB
Loading

0 commit comments

Comments
 (0)