Browse files

added RStoolbox dependency for ggRGB due to CRAN revisison comments

  • Loading branch information...
16EAGLE committed Nov 3, 2017
1 parent e105def commit 0733057c32c553b7dda30eddf98625975bbf75d7
Showing with 5 additions and 277 deletions.
  1. +2 −1 DESCRIPTION
  2. +1 −0 NAMESPACE
  3. +2 −149 R/animate_move.R
  4. +0 −127 R/moveVis-internal.R
@@ -31,4 +31,5 @@ Imports:
@@ -8,6 +8,7 @@ export(get_imconvert)
@@ -178,6 +178,7 @@
#' @importFrom lubridate seconds_to_period hour minute second
#' @importFrom parallel detectCores makeCluster stopCluster
#' @importFrom pbapply timerProgressBar getTimerProgressBar setTimerProgressBar closepb
#' @importFrom RStoolbox ggRGB
#' @export
@@ -269,155 +270,7 @@ animate_move <- function(m, out_dir, conv_dir = "",
#RGB plotting originally forked from RStoolbox::ggRGB based on raster:plotRGB
#author: Benjamin Leutner, Robert J. Hijmans
#license: GPL-3
#code taken from the ggRGB function due to dependency issues
#partly based on functions in the pixmap package by Friedrich Leisch
ggRGB <- function(img, r = 3, g = 2, b = 1, scale, maxpixels = 500000, stretch = "none", ext = NULL, limits = NULL,
clipValues = "limits", quantiles = c(0.02,0.98), ggObj = TRUE, ggLayer = FALSE,
alpha = 1, coord_equal = TRUE, geom_raster = FALSE, nullValue = 0) {
verbose <- getOption("RStoolbox.verbose")
annotation <- !geom_raster
## Subsample raster
rgb <- unlist(.numBand(raster=img,r,g,b))
nComps <- length(rgb)
if(inherits(img, "RasterLayer")) img <- brick(img)
rr <- sampleRegular(img[[rgb]], maxpixels, ext=ext, asRaster=TRUE)
RGB <- getValues(rr)
if(!is.matrix(RGB)) RGB <- as.matrix(RGB)
## Clip to limits
if (!is.null(limits)) {
## Tidy limits
if (!is.matrix(limits)) {
limits <- matrix(limits, ncol = 2, nrow = nComps, byrow = TRUE)
## Tidy clip values
if(!anyNA(clipValues) && clipValues[1] == "limits") {
clipValues <- limits
} else {
clipValues <- matrix(clipValues, ncol = 2, nrow = nComps, byrow = TRUE)
## Do clipping
for (i in 1:nComps) {
message("Number of pixels clipped in ", c("red", "green", "blue")[i], " band:\n",
"below limit: ", sum(RGB[,i] < limits[i,1], na.rm = TRUE), " | above limit: ", sum(RGB[,i] > limits[i,2], na.rm = TRUE))
RGB[RGB[,i] < limits[i,1], i] <- clipValues[i,1]
RGB[RGB[,i] > limits[i,2], i] <- clipValues[i,2]
rangeRGB <- range(RGB, na.rm = TRUE)
if(missing('scale')){ scale <- rangeRGB[2] }
if(rangeRGB[1] < 0){
RGB <- RGB - rangeRGB[1]
scale <- scale - rangeRGB[1]
rangeRGB <- rangeRGB - rangeRGB[1]
if(scale < rangeRGB[2]) {
warning("Scale < max value. Resetting scale to max.", call.=FALSE)
scale <- rangeRGB[2]
RGB <- na.omit(RGB)
## Perform data stretch
if (stretch != "none") {
stretch <- tolower(stretch)
for(i in seq_along(rgb)){
RGB[,i] <- .stretch(RGB[,i], method = stretch, quantiles=quantiles)
scale <- 1
## Assemble colors
naind <- as.vector( attr(RGB, "na.action") )
nullbands <- sapply(list(r,g,b), is.null)
if(any(nullbands)) {
RGBm <- matrix(nullValue, ncol = 3, nrow = NROW(RGB))
RGBm[,!nullbands] <- RGB
if (!is.null(naind)) {
z <- rep( NA, times=ncell(rr))
z[-naind] <- rgb(RGB[,1], RGB[,2], RGB[,3], max = scale, alpha = alpha*scale)
} else {
z <- rgb(RGB[,1], RGB[,2], RGB[,3], max = scale, alpha = alpha*scale)
df_raster <- data.frame(coordinates(rr), fill = z, stringsAsFactors = FALSE)
x <- y <- fill <- NULL ## workaround for a R CMD check 'note' about non-visible global variable in call to ggplot (variables are column names created earlier within 'data' and hence not visible to check). This does not in any way affect ggRGB,
## We need to set up ggplot with at least the minimum aestetics x and y
exe <- as.vector(extent(rr))
df <- data.frame(x=exe[1:2],y=exe[3:4])
## Set-up plot
## I prefer annotate_raster instead of geom_raster or tile to keep the fill scale free for additional rasters
if(annotation) {
dz <- matrix(z, nrow=nrow(rr), ncol=ncol(rr), byrow = TRUE)
p <- annotation_raster(raster = dz, xmin = exe[1], xmax = exe[2], ymin = exe[3], ymax = exe[4], interpolate = FALSE)
if(!ggLayer) {
p <- ggplot() + p + geom_blank(data = df, aes(x = x,y = y))
} else {
p <- geom_raster(data = df_raster, aes(x = x, y = y, fill = fill), alpha = alpha)
if(!ggLayer) {
p <- ggplot() + p + scale_fill_identity()
if(coord_equal & !ggLayer) p <- p + coord_equal()
} else {
## Perform histogram, sqrt log and 98% linear stretching
.stretch <- function (x, method = "lin", quantiles = c(0.02,0.98)) {
if(!method %in% c("lin", "hist", "log", "sqrt")) stop("Stretch method must be 'lin', 'hist', 'sqrt' or 'log'", call. = FALSE)
if(method == "lin"){
if(length(quantiles) == 1) quantiles <- c(0,1) + c(quantiles, -quantiles)/100
v <- quantile(x, quantiles, na.rm = TRUE)
temp <- (x - v[1])/(v[2] - v[1])
temp[temp < 0] <- 0
temp[temp > 1] <- 1
if(method == "hist"){
ecdfun <- ecdf(x)
if(method == "log"){
x <- log(x + 1)
x <- x - min(x)
return(x / max(x))
if(method == "sqrt"){
x <- sqrt(x)
x <- x - min(x)
return(x /max(x))
.numBand <- function(raster, ...){
bands <- list(...)
lapply(bands, function(band) if(is.character(band)) which(names(raster) == band) else band )
#+++++++++++++++++++++++++++++++++++++++ MAIN ++++++++++++++++++++++++++++++++++++++++++++++
@@ -1,130 +1,3 @@
.Random.seed <-
c(403L, 33L, 1018610931L, 1726942050L, -792246828L, -1961930537L,
1800782541L, -1546263964L, -2001553334L, 1796591029L, 1092555023L,
-2043646442L, 1184786624L, 1125342851L, -1275422047L, 1243040912L,
-1667332242L, -1053818599L, -1579335253L, -1840414822L, 1990479708L,
-1553269153L, 866938421L, 514674124L, -1722266334L, -1168115843L,
686525559L, -238415458L, -656758696L, 2082067899L, -2109908135L,
-1654503192L, -1009661674L, -444327279L, 1684821827L, -929045934L,
2058691812L, -874803417L, 228551549L, 1327582836L, -1419947942L,
-1982297947L, 1721653951L, 426058054L, 376068048L, 374309363L,
-1803309807L, -1780922048L, -55437026L, -1120346647L, 1845776155L,
1231165738L, 279540300L, -108753393L, -1283022523L, -592639236L,
1512137042L, 1400391373L, -295960953L, -1730229906L, 111264584L,
-1616152757L, -1887650135L, 1029628920L, -1400767706L, -1651184575L,
-1535975981L, 500895618L, -1424063564L, 1288026935L, 943938093L,
-790160572L, -1241157526L, 1830931157L, -1949035409L, 387222902L,
1131353760L, 408195939L, 2099509249L, 613548912L, -1270486258L,
-525924999L, -703504117L, -1709759942L, 1029732668L, 418338431L,
-1089897003L, 1059206124L, 87598402L, -1395197283L, 1190598423L,
-886032770L, -1761233352L, -2069933797L, -478480775L, -99552568L,
-1148157194L, 326208305L, 224115555L, 1010110770L, 1088500356L,
-1513136441L, 2002789789L, 119157012L, -299737158L, 1622710789L,
1395911647L, 1148699238L, -2018001424L, 2126285587L, 408360113L,
-1384290464L, -852712194L, 136176777L, -301349957L, -1874924150L,
-1378980756L, 793134575L, 1598266917L, 1786542812L, 1347465394L,
-94796883L, -465832089L, -2032581490L, 1404977512L, 47726955L,
-2105609463L, 1436640664L, 1072714950L, -1477726687L, 1126108851L,
914748450L, 1690332692L, 924590743L, 1162574989L, -1853214300L,
-1048831094L, -2031257611L, 1067365967L, 1508790614L, -879117312L,
-1627523773L, -1356072735L, 601100880L, -31428946L, -685524391L,
1780367979L, -2115459110L, -154908516L, -158053729L, -1182701323L,
-1769131636L, 1119861858L, 1964690493L, 1000734391L, 291183966L,
-264600424L, 915271675L, -1862191719L, 88819240L, 1590230742L,
587570001L, -1134838269L, 1300046226L, -1236762844L, 1825862119L,
732388925L, 1144686772L, -1513627878L, 2003637349L, 1357012223L,
-986067578L, 254149392L, 1852889011L, 843430993L, -688037888L,
-1025830690L, 600550185L, 1515820763L, -124877334L, -494023284L,
-1662344497L, -236446075L, 380140860L, -1727021038L, -98308339L,
-1008694201L, -1006551506L, 630998920L, -827469173L, -272817047L,
-341309384L, -1573191450L, 1017464961L, 1282060307L, 542603202L,
650514548L, 757065335L, 1854863213L, -1036507132L, -2052062422L,
-401344363L, 759703343L, 484019766L, -792421792L, -313694045L,
600361921L, -2089121872L, -74009906L, 1166151737L, -2013542837L,
-781693958L, -1517816068L, -902915265L, -333212651L, -1435168980L,
-827677694L, 668042973L, 2073540055L, -1788313410L, 1051603704L,
573577435L, -222352583L, -479256952L, 1983580726L, 808043121L,
-1742440285L, 1984664050L, -669482684L, 1278918770L, -1086549008L,
-1474301884L, 777409624L, 1286617146L, 553832560L, -1495717316L,
-954778860L, 1146244802L, 1402535648L, 412824700L, -156819248L,
-1950248030L, -593386968L, -1204101876L, 1387129788L, -2005832398L,
1123660992L, 1864269364L, 2053215944L, 1056181946L, -1969330032L,
-1383486740L, 1073566292L, 1168905874L, 685443872L, -1806742580L,
-1321687840L, -940217662L, 2030395432L, -152820820L, 1063174844L,
1135119858L, 1677217328L, 1587021732L, 1962689976L, 2061879130L,
-1039587184L, -197835908L, 1386642196L, 454115650L, -328215808L,
1268840380L, -896220336L, -1274696414L, 1431455304L, -475789300L,
1890318428L, 311673298L, -703847936L, 1904638292L, 1641555176L,
-1671365382L, 778010320L, 1659041068L, -36259596L, -1085366894L,
652939616L, 904974348L, 1334388448L, -1455338910L, -578812248L,
-468994484L, 1854145404L, -164162254L, -1322912144L, -368841916L,
-1642318888L, 920104826L, 989547056L, -287532100L, -679951788L,
-318774206L, -946691936L, 22653628L, -937284144L, 1916434274L,
-1900516632L, -444730164L, 989855228L, -717198862L, 223233664L,
1322632692L, -1644055928L, -287928518L, -995086512L, 2016951404L,
1337046420L, 2104710226L, 832690656L, -34820916L, 1349195936L,
-422004350L, 1914472680L, -1850373396L, -1554778820L, 1069642866L,
-726594448L, 16036900L, 1400176696L, 31519514L, -662362928L,
-1311816900L, -1905750124L, 219953026L, -341942848L, -696118724L,
-1778181744L, 1721147938L, 2038594824L, -526769524L, -142989540L,
-848471918L, 1202456960L, -244045292L, -1955040472L, 1355988538L,
-2038201904L, 1105479020L, 1997675444L, -1348744174L, 840464864L,
1744288716L, -1068697376L, -742412254L, -2013319896L, 138563084L,
1093053244L, 307291122L, -1228730640L, 591467076L, -62648744L,
465646394L, 34054640L, -1900953924L, 564317460L, 993547970L,
-747431840L, -852834436L, 1905159120L, -1084125918L, -1283287384L,
-2095240308L, 1704860860L, 574621490L, 448535104L, -1418927948L,
391924296L, -1832612422L, 1466507792L, 531830508L, -1102465708L,
1573958930L, -1002808928L, -1234880820L, -2038346144L, -1889010750L,
-2090128600L, 974486444L, -1331151044L, -1062560398L, -81621200L,
-2054867676L, -2013625544L, 1884962522L, -175412336L, -1134535940L,
-1128447084L, 310288450L, 293175552L, 1538923580L, -88189616L,
943354018L, 1028105544L, -1667020020L, -467784996L, 2039714130L,
1395968128L, -1889457068L, 1977795560L, -1130210054L, -1544126640L,
-2109966548L, 2094972276L, -185598062L, 153033824L, 730465676L,
1016361824L, -1964780062L, 41337768L, -647477044L, -1591143428L,
1180746162L, -90378768L, 1906507716L, -4683432L, 1151925626L,
1646307248L, -2129216068L, 517043284L, 727062466L, -632879072L,
-1306591044L, 533853776L, 2045081570L, 1596086376L, -1115326388L,
-181836548L, -1429723150L, 1054596352L, -545979020L, 1834270728L,
-1345739846L, 208455120L, -788742164L, -124500588L, 1441595858L,
1795905888L, 1358404428L, -1373134560L, -1261795454L, 685539048L,
1820616940L, -285194180L, -888950926L, -605204880L, -491983836L,
-1940191923L, -714286921L, 1886560832L, 60889886L, -118521365L,
573877613L, 1808282586L, -934619752L, -591668223L, 1102626227L,
-1425562412L, -2011385374L, 1091431543L, -24743487L, 312666950L,
2055943812L, 753606709L, 25718815L, -68274312L, 889081430L, -1956461853L,
-1990368091L, 1132817730L, 1072216880L, -1955766439L, 1873671371L,
141763900L, -1980403350L, 1489755039L, 30422569L, 30963902L,
1668874668L, -1989198083L, 1076914759L, -1308194512L, 2084139598L,
-1571407237L, -235353635L, 1867685898L, 1017733416L, -2102329455L,
-2025016957L, -1418604924L, -1550881614L, 804342791L, 1399035985L,
1726195670L, 1046439604L, -1891207483L, -266287089L, -345507704L,
199543206L, 1565307219L, 497746613L, -90703854L, -270419488L,
1147915337L, 851835515L, -1704606900L, 782468826L, 691452623L,
-1138824551L, 393324206L, 1588523964L, 509815853L, -218340393L,
1148586528L, -2145367170L, -1360259765L, 1655117645L, 669457786L,
-1616650312L, 1611703649L, -2087601901L, -1607618252L, -1279128766L,
-1875604649L, -1402574175L, 241675110L, 1144505700L, 1703268501L,
556392895L, -1369221800L, -1688671370L, 1539492227L, -961860667L,
-1304186654L, 639190352L, -2059590535L, 264812459L, 666254620L,
1709890122L, 224984639L, 135008905L, 960411294L, -1032191156L,
-2142780643L, -916352345L, -1895097904L, 1593577838L, -2062386149L,
-1375414915L, 432448362L, -1849330168L, -1181922639L, -904579805L,
1448289828L, -709366574L, -503272153L, 2117748593L, 2053465526L,
-1971876524L, 733670373L, 1002567151L, 1580162344L, -1051103866L,
-1609012813L, -1312742123L, -546568334L, 910533568L, 1988355113L,
267055259L, -1957601940L, -315374214L, 1747461807L, -1287041607L,
-926330930L, 1320582940L, -293130611L, -600158473L, -342490624L,
-141357986L, -360354005L, 409549869L, 835497114L, 1238458456L,
-1819654207L, -1107605389L, 1802396052L, 315125794L, 1290311479L,
-1177675263L, 1963959046L, 548335428L, -1547287947L, 943191007L,
1558345528L, 1872523542L, 2073914659L, 1964932837L, -2131147006L,
-1268322960L, -1554027239L, -190028533L, 1768146428L, -1873912278L,
1099073119L, -1988684311L, -1517107458L, -513558804L, 107923005L,
-1312950777L, 144660976L, -1549219442L, -1489657925L, -1549465187L,
625264458L, -1281000600L, -2127528623L, -1775610970L)
#Suppress messages and warnings
quiet <- function(expr){

0 comments on commit 0733057

Please sign in to comment.