From ac4aad35d00bde02913b5d62ea0c02e627861007 Mon Sep 17 00:00:00 2001 From: dutri001 Date: Thu, 21 Aug 2014 22:26:46 +0200 Subject: [PATCH 01/23] Added function to extract a zoo object from a raster brick --- NAMESPACE | 2 ++ R/zooExtract.R | 65 +++++++++++++++++++++++++++++++++++++++++++++++ man/zooExtract.Rd | 39 ++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 R/zooExtract.R create mode 100644 man/zooExtract.Rd diff --git a/NAMESPACE b/NAMESPACE index 56dc69e..409e30f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -20,9 +20,11 @@ export(sr2vi) export(subsetRasterTS) export(summaryBrick) export(timeStack) +export(zooExtract) import(bfast) import(gdalUtils) import(parallel) import(raster) import(rgdal) import(stringr) +import(zoo) diff --git a/R/zooExtract.R b/R/zooExtract.R new file mode 100644 index 0000000..b5bafbe --- /dev/null +++ b/R/zooExtract.R @@ -0,0 +1,65 @@ +#' @title Utility to extract a zoo object from a raster time-series +#' +#' @description This function extracts a zoo object from a multilayer raster object with time written to the z dimension or a date vector supplied externally. The aim of this utility is to facilitate the use of the bfmApp for bfastmonitor parameters investigation. The app allows the user to supply a zoo (time-series) object and visualize the effect of parameters change on the algorithm output. We outline in the vignette of the bfastSpatial package that this is an interesting step for parameter selection prior to more heavy processing when starting to work in a study area. +#' +#' @param x rasterBrick or rasterStack object, or file name to a multilayer raster object stored on disk. +#' @param sample The point(s) to extract. Default to 'click' where a point is interactively choosen by clicking. Or any of object accepted by \code{\link{extract}}. (points represented by a two-column matrix or data.frame, or \code{\link{SpatialPoints}*}; \code{\link{SpatialPolygons}*}; \code{\link[sp]{SpatialLines}}; \code{\link{Extent}}; or a numeric vector representing cell numbers). For 'click', a layer of \code{x} needs to be plotted first. +#' @param dates A date vector (optional, only if time is not yet contained in the z dimension of the raster object, or comprised in its layer names.) +#' @param file character rds filename where to write the output. +#' +#' @author Loic Dutrieux +#' @return A zoo object that may contain multiple time-series +#' @seealso \link{sr2vi} +#' +#' @import zoo +#' @import raster +#' +#' @examples +#' +#' # Single time-series selcted interactively +#' data(tura) +#' plot(tura, 2) +#' zooExtract(x = tura, sample = 'click', file = file.path(tempdir(), 'zooClick.rds')) +#' +#' # Then in order to explore the time series and the bfastmonitor parameters, run the following line +#' \dontrun{ +#' runGitHub( "bfmApp", "dutri001") +#' } +#' # And upload the file zooClick.rds +#' +#' @export + + +zooExtract <- function(x, sample = 'click', dates = NULL, file = NULL) { + + if(is.character(x)) { + x <- brick(x) + } + + # date collection snippet + if(is.null(dates)) { + if(is.null(getZ(x))) { + if(!.isLandsatSceneID(x)){ # Check if dates can be extracted from layernames + stop('A date vector must be supplied, either via the date argument, the z dimension of x or comprised in names(x)') + } else { + dates <- as.Date(getSceneinfo(names(x))$date) + } + } else { + dates <- getZ(x) + } + } + + + if(sample == 'click') { + v <- click(x, show=FALSE) + } else { + v <- extract(x, sample) + } + + out <- zoo(t(v), dates) + + if(!is.null(file)) { + saveRDS(out, file = file) + } + +} \ No newline at end of file diff --git a/man/zooExtract.Rd b/man/zooExtract.Rd new file mode 100644 index 0000000..e77ce38 --- /dev/null +++ b/man/zooExtract.Rd @@ -0,0 +1,39 @@ +% Generated by roxygen2 (4.0.1): do not edit by hand +\name{zooExtract} +\alias{zooExtract} +\title{Utility to extract a zoo object from a raster time-series} +\usage{ +zooExtract(x, sample = "click", dates = NULL, file = NULL) +} +\arguments{ +\item{x}{rasterBrick or rasterStack object, or file name to a multilayer raster object stored on disk.} + +\item{sample}{The point(s) to extract. Default to 'click' where a point is interactively choosen by clicking. Or any of object accepted by \code{\link{extract}}. (points represented by a two-column matrix or data.frame, or \code{\link{SpatialPoints}*}; \code{\link{SpatialPolygons}*}; \code{\link[sp]{SpatialLines}}; \code{\link{Extent}}; or a numeric vector representing cell numbers). For 'click', a layer of \code{x} needs to be plotted first.} + +\item{dates}{A date vector (optional, only if time is not yet contained in the z dimension of the raster object, or comprised in its layer names.)} + +\item{file}{character rds filename where to write the output.} +} +\value{ +A zoo object that may contain multiple time-series +} +\description{ +This function extracts a zoo object from a multilayer raster object with time written to the z dimension or a date vector supplied externally. The aim of this utility is to facilitate the use of the bfmApp for bfastmonitor parameters investigation. The app allows the user to supply a zoo (time-series) object and visualize the effect of parameters change on the algorithm output. We outline in the vignette of the bfastSpatial package that this is an interesting step for parameter selection prior to more heavy processing when starting to work in a study area. +} +\examples{ +# Single time-series selcted interactively +data(tura) +plot(tura, 2) +zooExtract(x = tura, sample = 'click', file = file.path(tempdir(), 'zooClick.rds')) + +# Then in order to explore the time series and the bfastmonitor parameters, run the following line +\dontrun{runGitHub( "bfmApp", "dutri001")} +# And upload the file zooClick.rds +} +\author{ +Loic Dutrieux +} +\seealso{ +\link{sr2vi} +} + From 70fe0b9fdee28c4ef4f640affe4d5e5ddae803ec Mon Sep 17 00:00:00 2001 From: Ben DeVries Date: Wed, 12 Nov 2014 18:44:28 +0100 Subject: [PATCH 02/23] updated bfmSpatial documentation according to Jan's suggestion (see "Value" field) --- R/bfmSpatial.R | 2 +- man/bfmSpatial.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/bfmSpatial.R b/R/bfmSpatial.R index e446971..3cb055f 100644 --- a/R/bfmSpatial.R +++ b/R/bfmSpatial.R @@ -21,7 +21,7 @@ #' @param sensor Character. Optional: Limit analysis to one or more particular sensors. Can be any combintation of \code{c("ETM+", "ETM+ SLC-on", "ETM+ SLC-off", "TM", or "OLI")} #' @param ... Arguments to be passed to \code{\link{mc.calc}} #' -#' @return A rasterBrick with layers depending on what has been supplied to \code{returnLayers}. See details for more information. +#' @return A rasterBrick with layers depending on what has been supplied to \code{returnLayers}. By default, 3 layers are returned: (1) breakpoint: timing of breakpoints detected for each pixel; (2) magnitude: the median of the residuals within the monitoring period; (3) error: a value of 1 for pixels where an error was encountered by the algorithm (see \code{\link{try}}), and NA where the method was successfully run. See \code{\link{bfastmonitor}} for more information on the other possible layers. #' #' @details #' \code{bfmSpatial} applies \code{\link{bfastmonitor}} over a raster time series. For large raster datasets, processing times can be long. Given the number of parameters that can be set, it is recommended to first run \code{\link{bfmPixel}} over some test pixels or \code{bfmSpatial} over a small test area to gain familiarity with the time series being analyzed and to test several parameters. diff --git a/man/bfmSpatial.Rd b/man/bfmSpatial.Rd index e8678d9..409cc66 100644 --- a/man/bfmSpatial.Rd +++ b/man/bfmSpatial.Rd @@ -45,7 +45,7 @@ bfmSpatial(x, dates = NULL, pptype = "irregular", start, monend = NULL, \item{n}{See \code{\link{bfastmonitor}}} } \value{ -A rasterBrick with layers depending on what has been supplied to \code{returnLayers}. See details for more information. +A rasterBrick with layers depending on what has been supplied to \code{returnLayers}. By default, 3 layers are returned: (1) breakpoint: timing of breakpoints detected for each pixel; (2) magnitude: the median of the residuals within the monitoring period; (3) error: a value of 1 for pixels where an error was encountered by the algorithm (see \code{\link{try}}), and NA where the method was successfully run. See \code{\link{bfastmonitor}} for more information on the other possible layers. } \description{ Implements bfastmonitor function, from the bfast package on any kind of rasterBrick object. Time information is provided as an extra object and the time series can be regular or irregular. From 5674369fb30d779e90291ba4944e6e24f3679b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc?= Date: Mon, 24 Nov 2014 14:29:15 +0100 Subject: [PATCH 03/23] Fixed bug. Comparing objects of different classes --- R/zooExtract.R | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/R/zooExtract.R b/R/zooExtract.R index b5bafbe..c4a630f 100644 --- a/R/zooExtract.R +++ b/R/zooExtract.R @@ -16,14 +16,14 @@ #' #' @examples #' -#' # Single time-series selcted interactively +#' # Single time-series selected interactively #' data(tura) #' plot(tura, 2) #' zooExtract(x = tura, sample = 'click', file = file.path(tempdir(), 'zooClick.rds')) #' #' # Then in order to explore the time series and the bfastmonitor parameters, run the following line #' \dontrun{ -#' runGitHub( "bfmApp", "dutri001") +#' runGitHub("bfmApp", "dutri001") #' } #' # And upload the file zooClick.rds #' @@ -49,9 +49,10 @@ zooExtract <- function(x, sample = 'click', dates = NULL, file = NULL) { } } - - if(sample == 'click') { - v <- click(x, show=FALSE) + if(is.character(sample)) { + if(sample == 'click') { + v <- click(x, show=FALSE) + } } else { v <- extract(x, sample) } From 58981209ac3e5589e9dabf68ed3095eae4d93abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc?= Date: Mon, 24 Nov 2014 14:56:35 +0100 Subject: [PATCH 04/23] Added zoo to list of dependencies --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index f25fdaf..02c1f22 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,5 +17,6 @@ Depends: gdalUtils, stringr, rgdal, - bitops + bitops, + zoo License: tbd From 34488a96d7e30584b8402386c65f3827f264b869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dutrieux?= Date: Tue, 25 Nov 2014 12:03:43 +0100 Subject: [PATCH 05/23] Started bfmZoo function, to run bfastmonitor on zoo objects and return a dataframe (vectorized) --- R/bfmZoo.R | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 R/bfmZoo.R diff --git a/R/bfmZoo.R b/R/bfmZoo.R new file mode 100644 index 0000000..03dec2e --- /dev/null +++ b/R/bfmZoo.R @@ -0,0 +1,5 @@ +bfmZoo <- function(x, start, mc.cores = 1) { + + + mclapply(X = ts, FUN = bfastmonitor, start = start, mc.cores = mc.cores) +} \ No newline at end of file From 628d096585112ae256a419870d4de7f214f966fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dutrieux?= Date: Tue, 25 Nov 2014 18:59:26 +0100 Subject: [PATCH 06/23] Function to run bfastmonitor on a (multiple) zoo time-series. And return a dataframe --- R/bfmZoo.R | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/R/bfmZoo.R b/R/bfmZoo.R index 03dec2e..3058737 100644 --- a/R/bfmZoo.R +++ b/R/bfmZoo.R @@ -1,5 +1,25 @@ -bfmZoo <- function(x, start, mc.cores = 1) { +#' Runs bfastmonitor on a zoo object +#' +#' @description The zoo object may contain several time-series +#' +#' @param x A zoo object that may contain several time-series +#' @param mc.cores Numeric For parallel processing. Number of workers. +#' @param ... Arguments to be passed to \link{bfastmonitor} +#' +#' @return A dataframe +#' +#' @author Loic Dutrieux +#' +#' @import bfast +#' +#' @export +#' + + +bfmZoo <- function(x, mc.cores = 1, ...) { - - mclapply(X = ts, FUN = bfastmonitor, start = start, mc.cores = mc.cores) + out <- mclapply(X = ts, FUN = bfastmonitor, mc.cores = mc.cores, ...) + # Unlist + data.frame(breakpoint = sapply(X = out, function(x) x$breakpoint), + magnitude = sapply(out, function(x) x$magnitude)) } \ No newline at end of file From f7c6baa07b27611c0c93a7e068de3a2afa6115e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dutrieux?= Date: Tue, 25 Nov 2014 19:04:26 +0100 Subject: [PATCH 07/23] Added elypsis on extract (zooExtract function) to allow e.g. functions to be passed when sample is a spatialPolygon object --- R/zooExtract.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/zooExtract.R b/R/zooExtract.R index c4a630f..e388972 100644 --- a/R/zooExtract.R +++ b/R/zooExtract.R @@ -6,6 +6,7 @@ #' @param sample The point(s) to extract. Default to 'click' where a point is interactively choosen by clicking. Or any of object accepted by \code{\link{extract}}. (points represented by a two-column matrix or data.frame, or \code{\link{SpatialPoints}*}; \code{\link{SpatialPolygons}*}; \code{\link[sp]{SpatialLines}}; \code{\link{Extent}}; or a numeric vector representing cell numbers). For 'click', a layer of \code{x} needs to be plotted first. #' @param dates A date vector (optional, only if time is not yet contained in the z dimension of the raster object, or comprised in its layer names.) #' @param file character rds filename where to write the output. +#' @param ... Arguments to be passed to \link{extract}. #' #' @author Loic Dutrieux #' @return A zoo object that may contain multiple time-series @@ -30,7 +31,7 @@ #' @export -zooExtract <- function(x, sample = 'click', dates = NULL, file = NULL) { +zooExtract <- function(x, sample = 'click', dates = NULL, file = NULL, ...) { if(is.character(x)) { x <- brick(x) @@ -54,7 +55,7 @@ zooExtract <- function(x, sample = 'click', dates = NULL, file = NULL) { v <- click(x, show=FALSE) } } else { - v <- extract(x, sample) + v <- extract(x, sample, ...) } out <- zoo(t(v), dates) From b6ad123ecac7c473e67c9310d1fcfdc0ee40b8fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dutrieux?= Date: Tue, 25 Nov 2014 19:29:48 +0100 Subject: [PATCH 08/23] Function to run bfastmonitor on an sp object (overlayed to a rasterBrick) in progress. --- R/bfmSpOver.R | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 R/bfmSpOver.R diff --git a/R/bfmSpOver.R b/R/bfmSpOver.R new file mode 100644 index 0000000..7e30237 --- /dev/null +++ b/R/bfmSpOver.R @@ -0,0 +1,13 @@ +bfmSpOver <- function(x, y, mc.cores = 1) { + + ts <- zooExtract(x, y) + bfm <- bfmZoo(ts, mc.cores = mc.cores) + + if(inherits(y, 'SpatialPoints')) { + y <- SpatialPoints(y) # Not sure if that is necessary + out <- SpatialPointsDataFrame() + } else if(inherits(y, 'SpatialPolygons')) { + + } + +} \ No newline at end of file From 0c442f82990256265a3412e0fcd50c7468917c2a Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 26 Nov 2014 14:58:13 +0100 Subject: [PATCH 09/23] Adapted TC components based on suggestion by Tom Bewernick --- R/LandsatVIs.R | 110 +++++++++++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/R/LandsatVIs.R b/R/LandsatVIs.R index e193df3..bd993a3 100644 --- a/R/LandsatVIs.R +++ b/R/LandsatVIs.R @@ -59,70 +59,72 @@ fun=fun)) } -# Tasseled Cap Components ------------------------------------------------- +# Tasseled Cap Components ------------------------------------------------- -.tcbright <- function(sensor) { - ind <- c('band1','band2','band3','band4','band5','band7') - # make compatible with getSceneinfo() output - if(sensor %in% c("ETM+", "ETM+ SLC-on", "ETM+ SLC-off")) - sensor <- 7 - if(sensor == "TM") - sensor <- 5 +.tcbright <- function(sensor) { + ind <- c('band1','band2','band3','band4','band5','band7') + # make compatible with getSceneinfo() output + if(sensor %in% c("ETM+", "ETM+ SLC-on", "ETM+ SLC-off")) + sensor <- 7 + if(sensor == "TM") + sensor <- 5 - if(sensor == 5) { - tc_coef <- c(0.3037, 0.2793, 0.4743, 0.5585, 0.5082, 0.1863) - } else if (sensor == 7) { + if(sensor == 5) { + tc_coef <- c(0.2043, 0.4158, 0.5524, 0.5741, 0.3124, 0.2303) + } else if (sensor == 7) { tc_coef <- c(0.3561, 0.3972, 0.3904, 0.6966, 0.2286, 0.1596) - } + } - fun <- function(x1, x2, x3, x4, x5, x7) { - tcbright <- sum(c(x1, x2, x3, x4, x5, x7) * tc_coef) - } + fun <- function(x1, x2, x3, x4, x5, x7) { + tcbright <- sum(c(x1, x2, x3, x4, x5, x7) * tc_coef) + } - return(list(ind=ind, - fun=fun)) -} + return(list(ind=ind, + fun=fun)) +} + -.tcgreen <- function(sensor) { - ind <- c('band1','band2','band3','band4','band5','band7') - # make compatible with getSceneinfo() output - if(sensor %in% c("ETM+", "ETM+ SLC-on", "ETM+ SLC-off")) - sensor <- 7 - if(sensor == "TM") - sensor <- 5 +.tcgreen <- function(sensor) { + ind <- c('band1','band2','band3','band4','band5','band7') + # make compatible with getSceneinfo() output + if(sensor %in% c("ETM+", "ETM+ SLC-on", "ETM+ SLC-off")) + sensor <- 7 + if(sensor == "TM") + sensor <- 5 - if(sensor == 5) { - tc_coef <- c(-0.2848, -0.2435, -0.5436, 0.7243, 0.0840, -0.1800) - } else if (sensor == 7) { - tc_coef <- c(-0.3344, -0.3544, -0.4556, 0.6966, -0.0242, -0.263) - } + if(sensor == 5) { + tc_coef <- c(-0.1603, -0.2819, -0.4934, 0.7940, 0.0002, -0.1446) + } else if (sensor == 7) { + tc_coef <- c(-0.3344, -0.3544, -0.4556, 0.6966, -0.0242,-0.2630) + } - fun <- function(x1, x2, x3, x4, x5, x7) { - tcbright <- sum(c(x1, x2, x3, x4, x5, x7) * tc_coef) - } + fun <- function(x1, x2, x3, x4, x5, x7) { + tcbright <- sum(c(x1, x2, x3, x4, x5, x7) * tc_coef) + } - return(list(ind=ind, - fun=fun)) -} + return(list(ind=ind, + fun=fun)) +} -.tcwet <- function(sensor) { - ind <- c('band1','band2','band3','band4','band5','band7') - # make compatible with getSceneinfo() output - if(sensor %in% c("ETM+", "ETM+ SLC-on", "ETM+ SLC-off")) - sensor <- 7 - if(sensor == "TM") - sensor <- 5 + +.tcwet <- function(sensor) { + ind <- c('band1','band2','band3','band4','band5','band7') + # make compatible with getSceneinfo() output + if(sensor %in% c("ETM+", "ETM+ SLC-on", "ETM+ SLC-off")) + sensor <- 7 + if(sensor == "TM") + sensor <- 5 - if(sensor == 5) { - tc_coef <- c(0.1509, 0.1973, 0.3279, 0.3406, -0.7112, -0.4572) - } else if (sensor == 7) { - tc_coef <- c(0.2626, 0.2141, 0.0926, 0.0656, -0.7629, -0.5388) - } + if(sensor == 5) { + tc_coef <- c(0.0315, 0.2021, 0.3102, 0.1594, 0.6806, -0.6109) + } else if (sensor == 7) { + tc_coef <- c(0.2626, 0.2141, 0.0926, 0.0656, -0.7629, -0.5388) + } - fun <- function(x1, x2, x3, x4, x5, x7) { - tcbright <- sum(c(x1, x2, x3, x4, x5, x7) * tc_coef) - } + fun <- function(x1, x2, x3, x4, x5, x7) { + tcbright <- sum(c(x1, x2, x3, x4, x5, x7) * tc_coef) + } - return(list(ind=ind, - fun=fun)) -} + return(list(ind=ind, + fun=fun)) +} \ No newline at end of file From 258e7f4cd655169b0bae82fcbf9695f9637971da Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 28 Nov 2014 15:40:26 +0100 Subject: [PATCH 10/23] Extented bfmZoo documentation --- R/bfmZoo.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/bfmZoo.R b/R/bfmZoo.R index 3058737..9df6b26 100644 --- a/R/bfmZoo.R +++ b/R/bfmZoo.R @@ -1,6 +1,6 @@ #' Runs bfastmonitor on a zoo object #' -#' @description The zoo object may contain several time-series +#' @description This function is analog to the \link{bfastmonitor} function, but differs in terms of inputs. In \link{bfastmonitor} an object of class ts needs to be provided, usually pre-processed using \link{bfastts}; the present function accepts directly zoo time-series. The return also differs from \link{bfastmonitor}; instead of returning an object of class 'bfastmonitor'the present function returns a dataframe. The zoo object may contain several time-series, which results in a return dataframe containing several rows. #' #' @param x A zoo object that may contain several time-series #' @param mc.cores Numeric For parallel processing. Number of workers. From 00f13169b4f08ead756c118b3d6f3a5547378b1e Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 28 Nov 2014 16:34:10 +0100 Subject: [PATCH 11/23] Finished and documented bfmSpOver function, still need to validate and write example section --- R/bfmSpOver.R | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/R/bfmSpOver.R b/R/bfmSpOver.R index 7e30237..a7a536d 100644 --- a/R/bfmSpOver.R +++ b/R/bfmSpOver.R @@ -1,13 +1,37 @@ -bfmSpOver <- function(x, y, mc.cores = 1) { +#' Runs bfastmonitor for a spatial subset with aggregation +#' +#' @description Runs \link{bfastmonitor} on a rasterBrick object for a set of locations, determined by an object of class \link{Spatial-class}. +#' +#' @param x A rasterBrick or rasterStack, ideally with time written to the z dimension. In case time is not written to the z dimension, the \code{dates=} argument has to be supplied (see \link{zooExtract}) +#' @param y A SpatialPoints, SpatialPointsDataFrame, SpatialPolygons, SpatialPolygonsDataFrame, SpatialLines, SpatialLinesDataFrame, or extent. \link{bfastmonitor} will be ran at these locations. In case each feature of the object covers several pixels (typically SpatialPolygons(DataFrames), SpatialLines(DataFrames) and extent), an aggregation function (\code{fun=}) has to be supplied (see \link{extract}). +#' @param mc.cores Numeric NUmber of cores to use (for parallel processing) +#' @param ... Arguments to be passed to \link{zooExtract} +#' +#' @author Loic Dutrieux +#' +#' @import raster +#' @import sp +#' +#' @export + +bfmSpOver <- function(x, y, mc.cores = 1, ...) { - ts <- zooExtract(x, y) + ts <- zooExtract(x, y, ...) bfm <- bfmZoo(ts, mc.cores = mc.cores) if(inherits(y, 'SpatialPoints')) { y <- SpatialPoints(y) # Not sure if that is necessary - out <- SpatialPointsDataFrame() + out <- SpatialPointsDataFrame(y, data = bfm) } else if(inherits(y, 'SpatialPolygons')) { - + y <- SpatialPolygons(y) + out <- SpatialPolygonsDataFrame(y, data = bfm) + } else if(inherits(y, 'SpatialLines')) { + y <- SpatialLines(y) + out <- SpatialLinesDataFrame(y, data = bfm) + } else if(class(y) == 'extent') { + y <- polygonFromExtent(y) + out <- SpatialPolygonsDataFrame(y, data = bfm) + proj4string(out) <- CRS(projection(x)) } - + return(out) } \ No newline at end of file From a261252d1cf0fdf69809f7260955075275c0e2cf Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 28 Nov 2014 19:23:15 +0100 Subject: [PATCH 12/23] Added new dataset (SpatialPolygons with 3 polygons located in tura) --- R/test_data.R | 15 +++++++++++++++ data/turaSp.rda | Bin 0 -> 732 bytes man/turaSp.Rd | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 data/turaSp.rda create mode 100644 man/turaSp.Rd diff --git a/R/test_data.R b/R/test_data.R index d0b04e5..fc60759 100644 --- a/R/test_data.R +++ b/R/test_data.R @@ -8,3 +8,18 @@ NULL +#' SpatialPolygons object +#' @aliases turaSp +#' @name turaSp +#' @description Three polygons located in the tura area (ethiopia) +#' @docType data +#' @usage data(turaSp) +#' @author Ben DeVries +#' @keywords data +#' @examples +#' data(turaSp) +#' data(tura) +#' plot(tura, 2) +#' plot(turaSp, add = TRUE) + +NULL diff --git a/data/turaSp.rda b/data/turaSp.rda new file mode 100644 index 0000000000000000000000000000000000000000..ba53be7d4dcbc40f1430249865cd2cc662157a09 GIT binary patch literal 732 zcmV<20wet&iwFP!000001`BeDFye~fVqjokVqj)uVqjthGFg~_1S10r11FGXD=95X z3@%_`WR^seVM$8LuK;S~1F;=7{=e%f-(c#f>2I`TlBcSprr;JJ*T_*bK3RV19A`AW z%qf|PGtCpW;S<0 zQGS+5aY<2TUOE<^ae$-~i_(js2E)V+wLucLr6suv+Ew{^skX*u3fieTIR(YG;qJi} zCJNf6d6^}}wm|8;{P>jAv|==aS(9@Ti-Cen$fhzo2L+?4V=qWd&Q45EMYEr&xB%iA z3=eS>Ufk03EVyX)(bkvL~@jrD= z0VWTzjrl)Pa%6?l&`@Uoj}~L>(2#^kbNnYdnm7w`@=N@SQc|(_m-|1F!OrCAf^K1f zAw&%i%nY>X!0-W5S|I2Jcq-xgkC}3iT`gd8;=rW2j+%iJq@S#H!En*Xwj26KJ^di! zy#Fy11s^O9kV1w3KO)}w9&a!;ya%)=S^^l+h!TeX@~6!m0SNgZ#gbTYS0Md)UH76F zK=;(REwjD>ESbLE0;dm%JRRa)0E>qR1(+a)sRc&(gOWax%kbM{-po9pTU9hB9k<4w z+|t#^j$C;4!I=6bR@@Cp|B-#e>kG8^{Hb$__6T`++PtMQHP0cGoQ_Sk-=o9P; Oa})so7Nt;f2mk;h8&_`t literal 0 HcmV?d00001 diff --git a/man/turaSp.Rd b/man/turaSp.Rd new file mode 100644 index 0000000..59cb2f6 --- /dev/null +++ b/man/turaSp.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\docType{data} +\name{turaSp} +\alias{turaSp} +\title{SpatialPolygons object} +\usage{ +data(turaSp) +} +\description{ +Three polygons located in the tura area (ethiopia) +} +\examples{ +data(turaSp) +data(tura) +plot(tura, 2) +plot(turaSp, add = TRUE) +} +\author{ +Ben DeVries +} +\keyword{data} + From 44fefbf7d5c3f17bb3ce915c73eaa8b066bb23f2 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 28 Nov 2014 19:24:39 +0100 Subject: [PATCH 13/23] Set of functions to run bfastmonitor object based and point based added --- R/bfmSpOver.R | 50 +++++++++++++++++++++++++++++++++++++++++++------- R/bfmZoo.R | 1 + R/zooExtract.R | 2 ++ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/R/bfmSpOver.R b/R/bfmSpOver.R index a7a536d..934db16 100644 --- a/R/bfmSpOver.R +++ b/R/bfmSpOver.R @@ -4,33 +4,69 @@ #' #' @param x A rasterBrick or rasterStack, ideally with time written to the z dimension. In case time is not written to the z dimension, the \code{dates=} argument has to be supplied (see \link{zooExtract}) #' @param y A SpatialPoints, SpatialPointsDataFrame, SpatialPolygons, SpatialPolygonsDataFrame, SpatialLines, SpatialLinesDataFrame, or extent. \link{bfastmonitor} will be ran at these locations. In case each feature of the object covers several pixels (typically SpatialPolygons(DataFrames), SpatialLines(DataFrames) and extent), an aggregation function (\code{fun=}) has to be supplied (see \link{extract}). +#' @param start See \code{\link{bfastmonitor}} +#' @param formula See \code{\link{bfastmonitor}} +#' @param order See \code{\link{bfastmonitor}} +#' @param lag See \code{\link{bfastmonitor}} +#' @param slag See \code{\link{bfastmonitor}} +#' @param history See \code{\link{bfastmonitor}} +#' @param type See \code{\link{bfastmonitor}} +#' @param n See \code{\link{bfastmonitor}} +#' @param level See \code{\link{bfastmonitor}} #' @param mc.cores Numeric NUmber of cores to use (for parallel processing) #' @param ... Arguments to be passed to \link{zooExtract} #' #' @author Loic Dutrieux #' +#' @examples +#' # Load data +#' data(tura) +#' +#' # 1- SpatialPoints case +#' # Generate SpatialPoints +#' sp <- sampleRegular(x = tura, size = 20, sp=TRUE) +#' +#' # Run bfmSpOver with monitoring period starting year 2005 and all other default parameters of bfastmonitor +#' out <- bfmSpOver(tura, y = sp, start=c(2005,1)) +#' +#' # Visualize the results +#' plot(tura, 166) +#' points(out, col= out$breakpoint, pch=16, cex = abs(out$magnitude/max(out$magnitude))) +#' +#' # 2 - SpatialPolygons case +#' data(turaSp) +#' # Run bfmSpOver with monitoring period starting year 2002 and mean spatial aggregation function +#' out2 <- bfmSpOver(tura, y = turaSp, fun = mean, start=c(2002,1)) +#' +#' # Visualize +#' plot(tura, 166) +#' plot(out2, col = out2$breakpoint, add = TRUE) +#' #' @import raster #' @import sp #' #' @export -bfmSpOver <- function(x, y, mc.cores = 1, ...) { +bfmSpOver <- function(x, y, start, formula = response ~ trend + harmon, order = 3, lag = NULL, slag = NULL, history = c("ROC", "BP", "all"), type = "OLS-MOSUM", h = 0.25, end = 10, level = 0.05, mc.cores = 1, ...) { ts <- zooExtract(x, y, ...) - bfm <- bfmZoo(ts, mc.cores = mc.cores) + bfm <- bfmZoo(ts, mc.cores = mc.cores, start = start, + formula=formula, + order=order, lag=lag, slag=slag, + history=history, + type=type, h=h, + end=end, level=level) if(inherits(y, 'SpatialPoints')) { y <- SpatialPoints(y) # Not sure if that is necessary out <- SpatialPointsDataFrame(y, data = bfm) } else if(inherits(y, 'SpatialPolygons')) { - y <- SpatialPolygons(y) - out <- SpatialPolygonsDataFrame(y, data = bfm) + out <- SpatialPolygonsDataFrame(y, data = bfm, match.ID = FALSE) } else if(inherits(y, 'SpatialLines')) { - y <- SpatialLines(y) - out <- SpatialLinesDataFrame(y, data = bfm) + out <- SpatialLinesDataFrame(y, data = bfm, match.ID = FALSE) } else if(class(y) == 'extent') { y <- polygonFromExtent(y) - out <- SpatialPolygonsDataFrame(y, data = bfm) + out <- SpatialPolygonsDataFrame(y, data = bfm, match.ID = FALSE) proj4string(out) <- CRS(projection(x)) } return(out) diff --git a/R/bfmZoo.R b/R/bfmZoo.R index 9df6b26..a90525e 100644 --- a/R/bfmZoo.R +++ b/R/bfmZoo.R @@ -18,6 +18,7 @@ bfmZoo <- function(x, mc.cores = 1, ...) { + ts <- bfastts(x, index(ext), 'irregular') out <- mclapply(X = ts, FUN = bfastmonitor, mc.cores = mc.cores, ...) # Unlist data.frame(breakpoint = sapply(X = out, function(x) x$breakpoint), diff --git a/R/zooExtract.R b/R/zooExtract.R index e388972..acb04e1 100644 --- a/R/zooExtract.R +++ b/R/zooExtract.R @@ -64,4 +64,6 @@ zooExtract <- function(x, sample = 'click', dates = NULL, file = NULL, ...) { saveRDS(out, file = file) } + return(out) + } \ No newline at end of file From 7c214d9fa94310be5bfaa485ac8ecdbb888dbff9 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 28 Nov 2014 19:25:41 +0100 Subject: [PATCH 14/23] Re-oxygenized the packages, bfmSpOver function working for the example --- NAMESPACE | 3 +++ man/bfmSpOver.Rd | 68 +++++++++++++++++++++++++++++++++++++++++++++++ man/bfmZoo.Rd | 24 +++++++++++++++++ man/zooExtract.Rd | 4 ++- 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 man/bfmSpOver.Rd create mode 100644 man/bfmZoo.Rd diff --git a/NAMESPACE b/NAMESPACE index 87802a3..963f008 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,7 +3,9 @@ export(annualSummary) export(areaSieve) export(bfmPixel) +export(bfmSpOver) export(bfmSpatial) +export(bfmZoo) export(changeMonth) export(cleanBrick) export(cleanMODIS) @@ -29,5 +31,6 @@ import(gdalUtils) import(parallel) import(raster) import(rgdal) +import(sp) import(stringr) import(zoo) diff --git a/man/bfmSpOver.Rd b/man/bfmSpOver.Rd new file mode 100644 index 0000000..b40f979 --- /dev/null +++ b/man/bfmSpOver.Rd @@ -0,0 +1,68 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{bfmSpOver} +\alias{bfmSpOver} +\title{Runs bfastmonitor for a spatial subset with aggregation} +\usage{ +bfmSpOver(x, y, start, formula = response ~ trend + harmon, order = 3, + lag = NULL, slag = NULL, history = c("ROC", "BP", "all"), + type = "OLS-MOSUM", h = 0.25, end = 10, level = 0.05, mc.cores = 1, + ...) +} +\arguments{ +\item{x}{A rasterBrick or rasterStack, ideally with time written to the z dimension. In case time is not written to the z dimension, the \code{dates=} argument has to be supplied (see \link{zooExtract})} + +\item{y}{A SpatialPoints, SpatialPointsDataFrame, SpatialPolygons, SpatialPolygonsDataFrame, SpatialLines, SpatialLinesDataFrame, or extent. \link{bfastmonitor} will be ran at these locations. In case each feature of the object covers several pixels (typically SpatialPolygons(DataFrames), SpatialLines(DataFrames) and extent), an aggregation function (\code{fun=}) has to be supplied (see \link{extract}).} + +\item{start}{See \code{\link{bfastmonitor}}} + +\item{formula}{See \code{\link{bfastmonitor}}} + +\item{order}{See \code{\link{bfastmonitor}}} + +\item{lag}{See \code{\link{bfastmonitor}}} + +\item{slag}{See \code{\link{bfastmonitor}}} + +\item{history}{See \code{\link{bfastmonitor}}} + +\item{type}{See \code{\link{bfastmonitor}}} + +\item{level}{See \code{\link{bfastmonitor}}} + +\item{mc.cores}{Numeric NUmber of cores to use (for parallel processing)} + +\item{...}{Arguments to be passed to \link{zooExtract}} + +\item{n}{See \code{\link{bfastmonitor}}} +} +\description{ +Runs \link{bfastmonitor} on a rasterBrick object for a set of locations, determined by an object of class \link{Spatial-class}. +} +\examples{ +# Load data +data(tura) + +# 1- SpatialPoints case +# Generate SpatialPoints +sp <- sampleRegular(x = tura, size = 20, sp=TRUE) + +# Run bfmSpOver with monitoring period starting year 2005 and all other default parameters of bfastmonitor +out <- bfmSpOver(tura, y = sp, start=c(2005,1)) + +# Visualize the results +plot(tura, 166) +points(out, col= out$breakpoint, pch=16, cex = abs(out$magnitude/max(out$magnitude))) + +# 2 - SpatialPolygons case +data(turaSp) +# Run bfmSpOver with monitoring period starting year 2002 and mean spatial aggregation function +out2 <- bfmSpOver(tura, y = turaSp, fun = mean, start=c(2002,1)) + +# Visualize +plot(tura, 166) +plot(out2, col = out2$breakpoint, add = TRUE) +} +\author{ +Loic Dutrieux +} + diff --git a/man/bfmZoo.Rd b/man/bfmZoo.Rd new file mode 100644 index 0000000..8c68f4a --- /dev/null +++ b/man/bfmZoo.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{bfmZoo} +\alias{bfmZoo} +\title{Runs bfastmonitor on a zoo object} +\usage{ +bfmZoo(x, mc.cores = 1, ...) +} +\arguments{ +\item{x}{A zoo object that may contain several time-series} + +\item{mc.cores}{Numeric For parallel processing. Number of workers.} + +\item{...}{Arguments to be passed to \link{bfastmonitor}} +} +\value{ +A dataframe +} +\description{ +This function is analog to the \link{bfastmonitor} function, but differs in terms of inputs. In \link{bfastmonitor} an object of class ts needs to be provided, usually pre-processed using \link{bfastts}; the present function accepts directly zoo time-series. The return also differs from \link{bfastmonitor}; instead of returning an object of class 'bfastmonitor'the present function returns a dataframe. The zoo object may contain several time-series, which results in a return dataframe containing several rows. +} +\author{ +Loic Dutrieux +} + diff --git a/man/zooExtract.Rd b/man/zooExtract.Rd index 9de4d2d..555d364 100644 --- a/man/zooExtract.Rd +++ b/man/zooExtract.Rd @@ -3,7 +3,7 @@ \alias{zooExtract} \title{Utility to extract a zoo object from a raster time-series} \usage{ -zooExtract(x, sample = "click", dates = NULL, file = NULL) +zooExtract(x, sample = "click", dates = NULL, file = NULL, ...) } \arguments{ \item{x}{rasterBrick or rasterStack object, or file name to a multilayer raster object stored on disk.} @@ -13,6 +13,8 @@ zooExtract(x, sample = "click", dates = NULL, file = NULL) \item{dates}{A date vector (optional, only if time is not yet contained in the z dimension of the raster object, or comprised in its layer names.)} \item{file}{character rds filename where to write the output.} + +\item{...}{Arguments to be passed to \link{extract}.} } \value{ A zoo object that may contain multiple time-series From 1bfd8ffd69decd3866a4226ff0106e7a124080df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc?= Date: Sat, 29 Nov 2014 17:22:38 +0100 Subject: [PATCH 15/23] fixed bug in bfmZoo --- R/bfmZoo.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/bfmZoo.R b/R/bfmZoo.R index a90525e..7192c45 100644 --- a/R/bfmZoo.R +++ b/R/bfmZoo.R @@ -18,7 +18,7 @@ bfmZoo <- function(x, mc.cores = 1, ...) { - ts <- bfastts(x, index(ext), 'irregular') + ts <- bfastts(x, index(x), 'irregular') out <- mclapply(X = ts, FUN = bfastmonitor, mc.cores = mc.cores, ...) # Unlist data.frame(breakpoint = sapply(X = out, function(x) x$breakpoint), From e536e75bb80d1bc305025bf2cf1adde5e9dc6c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc?= Date: Sat, 29 Nov 2014 18:12:00 +0100 Subject: [PATCH 16/23] Fixed documentation error in bfmSpOver and bfmSpatial --- R/bfmSpOver.R | 2 +- R/bfmSpatial.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/bfmSpOver.R b/R/bfmSpOver.R index 934db16..bb78c27 100644 --- a/R/bfmSpOver.R +++ b/R/bfmSpOver.R @@ -11,7 +11,7 @@ #' @param slag See \code{\link{bfastmonitor}} #' @param history See \code{\link{bfastmonitor}} #' @param type See \code{\link{bfastmonitor}} -#' @param n See \code{\link{bfastmonitor}} +#' @param h See \code{\link{bfastmonitor}} #' @param level See \code{\link{bfastmonitor}} #' @param mc.cores Numeric NUmber of cores to use (for parallel processing) #' @param ... Arguments to be passed to \link{zooExtract} diff --git a/R/bfmSpatial.R b/R/bfmSpatial.R index 3cb055f..81b57c5 100644 --- a/R/bfmSpatial.R +++ b/R/bfmSpatial.R @@ -14,7 +14,7 @@ #' @param slag See \code{\link{bfastmonitor}} #' @param history See \code{\link{bfastmonitor}} #' @param type See \code{\link{bfastmonitor}} -#' @param n See \code{\link{bfastmonitor}} +#' @param h See \code{\link{bfastmonitor}} #' @param level See \code{\link{bfastmonitor}} #' @param mc.cores Numeric. Number of cores to be used for the job. #' @param returnLayers Character. Result layers to be returned. Can be any combination of \code{c("breakpoint", "magnitude", "error", "history", "r.squared", "adj.r.squared", "coefficients")}. By default, \code{breakpoint}, \code{magnitude} and \code{error} are returned by the function. See \code{details} for more information. From 1458a57271256c709e54aa4f0761804e16f31290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc?= Date: Sat, 29 Nov 2014 18:14:05 +0100 Subject: [PATCH 17/23] Added error catcher to bfmZoo and expanded return object content. Also the way the dataframe is constructed (bfm2df function) is now more flexible for further editing --- R/bfmZoo.R | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/R/bfmZoo.R b/R/bfmZoo.R index 7192c45..6c560bc 100644 --- a/R/bfmZoo.R +++ b/R/bfmZoo.R @@ -18,9 +18,30 @@ bfmZoo <- function(x, mc.cores = 1, ...) { + bfm2df <- function(x) { + if(class(x) == 'bfastmonitor') { + data.frame(breakpoint = x$breakpoint, + magnitude = x$magnitude, + history = (x$history[2] - x$history[1]), + rsq = summary(x$model)$r.squared, + adj_rsq = summary(x$model)$adj.r.squared) + } else if(class(x) == 'try-error') { + data.frame(breakpoint = NA, + magnitude = NA, + history = NA, + rsq = NA, + adj_rsq = NA) + } + + } + + bfastmonitorFun <- function(x, ...) { + bfm <- try(bfastmonitor(x, ...)) + bfm2df(bfm) + } + ts <- bfastts(x, index(x), 'irregular') - out <- mclapply(X = ts, FUN = bfastmonitor, mc.cores = mc.cores, ...) - # Unlist - data.frame(breakpoint = sapply(X = out, function(x) x$breakpoint), - magnitude = sapply(out, function(x) x$magnitude)) + out <- mclapply(X = ts, FUN = bfastmonitorFun, mc.cores = mc.cores, ...) + # Convert list to df + do.call(rbind, out) } \ No newline at end of file From 70bbb3f07a94c580af76fac8c5b20ee80bd7d01f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc?= Date: Sat, 29 Nov 2014 18:17:01 +0100 Subject: [PATCH 18/23] Re-oxygenized package --- man/bfmSpOver.Rd | 4 ++-- man/bfmSpatial.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/man/bfmSpOver.Rd b/man/bfmSpOver.Rd index b40f979..ec2c5ec 100644 --- a/man/bfmSpOver.Rd +++ b/man/bfmSpOver.Rd @@ -27,13 +27,13 @@ bfmSpOver(x, y, start, formula = response ~ trend + harmon, order = 3, \item{type}{See \code{\link{bfastmonitor}}} +\item{h}{See \code{\link{bfastmonitor}}} + \item{level}{See \code{\link{bfastmonitor}}} \item{mc.cores}{Numeric NUmber of cores to use (for parallel processing)} \item{...}{Arguments to be passed to \link{zooExtract}} - -\item{n}{See \code{\link{bfastmonitor}}} } \description{ Runs \link{bfastmonitor} on a rasterBrick object for a set of locations, determined by an object of class \link{Spatial-class}. diff --git a/man/bfmSpatial.Rd b/man/bfmSpatial.Rd index 409cc66..05d912d 100644 --- a/man/bfmSpatial.Rd +++ b/man/bfmSpatial.Rd @@ -32,6 +32,8 @@ bfmSpatial(x, dates = NULL, pptype = "irregular", start, monend = NULL, \item{type}{See \code{\link{bfastmonitor}}} +\item{h}{See \code{\link{bfastmonitor}}} + \item{level}{See \code{\link{bfastmonitor}}} \item{mc.cores}{Numeric. Number of cores to be used for the job.} @@ -41,8 +43,6 @@ bfmSpatial(x, dates = NULL, pptype = "irregular", start, monend = NULL, \item{sensor}{Character. Optional: Limit analysis to one or more particular sensors. Can be any combintation of \code{c("ETM+", "ETM+ SLC-on", "ETM+ SLC-off", "TM", or "OLI")}} \item{...}{Arguments to be passed to \code{\link{mc.calc}}} - -\item{n}{See \code{\link{bfastmonitor}}} } \value{ A rasterBrick with layers depending on what has been supplied to \code{returnLayers}. By default, 3 layers are returned: (1) breakpoint: timing of breakpoints detected for each pixel; (2) magnitude: the median of the residuals within the monitoring period; (3) error: a value of 1 for pixels where an error was encountered by the algorithm (see \code{\link{try}}), and NA where the method was successfully run. See \code{\link{bfastmonitor}} for more information on the other possible layers. From 867b7d1daaecc9f12aa59f8dadacfb223d3ff6e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc?= Date: Sat, 29 Nov 2014 18:37:33 +0100 Subject: [PATCH 19/23] Improved bfmSpOver documentation (better visualization of output in example) --- R/bfmSpOver.R | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/R/bfmSpOver.R b/R/bfmSpOver.R index bb78c27..c95e80f 100644 --- a/R/bfmSpOver.R +++ b/R/bfmSpOver.R @@ -31,7 +31,12 @@ #' #' # Visualize the results #' plot(tura, 166) -#' points(out, col= out$breakpoint, pch=16, cex = abs(out$magnitude/max(out$magnitude))) +#' +#' # Build color palette +#' colfunc <- colorRampPalette(c("yellow", "red")) +#' colList <- colfunc(2013 - 2005) +#' points(out, col= colList[out$breakpoint - 2005], pch=16, cex = abs(out$magnitude/max(out$magnitude))) +#' # Color corresponds to timing of break and size to magnitude #' #' # 2 - SpatialPolygons case #' data(turaSp) @@ -40,7 +45,12 @@ #' #' # Visualize #' plot(tura, 166) -#' plot(out2, col = out2$breakpoint, add = TRUE) +#' # Build color palette +#' colfunc <- colorRampPalette(c("yellow", "red")) +#' colList <- colfunc(2013 - 2002) +#' plot(out2, col = colList[out2$breakpoint - 2002], add = TRUE) +#' # Interpretation: The redder the latter the break was detected. If transparent, no break detected in spatially aggregated polygon time-series. +#' #' #' @import raster #' @import sp From 9c448907c7cf3973bc5b0280289cce9f48f40a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc?= Date: Sat, 29 Nov 2014 18:51:10 +0100 Subject: [PATCH 20/23] Fixed issue 39. Wrong tag in rd doc --- R/cleanMODIS.R | 2 +- man/bfmSpOver.Rd | 13 +++++++++++-- man/cleanMODIS.Rd | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/R/cleanMODIS.R b/R/cleanMODIS.R index ce14159..20a6576 100644 --- a/R/cleanMODIS.R +++ b/R/cleanMODIS.R @@ -6,7 +6,7 @@ #' @param data_SDS Numeric. Index of the SDS containing the data in the hdf infrastructure #' @param QC_SDS Numeric. Index of the SDS containing the Quality control in the hdf infrastructure #' @param bit Logical. Is QC information provided bitwise? -#' @param QC_val Numeric or vector of numerics. Quality control values to \textbf{keep} in the data. If \code{bit} is set to \code{TRUE} \code{QC_val} is a BYTE (hexadecimal or decimal) where each bit refers to a bit in the QC layer element (i.e.: bitpos = 0xA1 targets bits 7, 5 and 0 --- 1010 0001). When bits targetted by \code{QC_val} are activated, the corresponding observation in the data layer is filtered out. +#' @param QC_val Numeric or vector of numerics. Quality control values to \bold{keep} in the data. If \code{bit} is set to \code{TRUE} \code{QC_val} is a BYTE (hexadecimal or decimal) where each bit refers to a bit in the QC layer element (i.e.: bitpos = 0xA1 targets bits 7, 5 and 0 --- 1010 0001). When bits targetted by \code{QC_val} are activated, the corresponding observation in the data layer is filtered out. #' @param fill (vector of) Numeric or NULL. Fill values in the data layer to be filtered out. (important to do before reprojection with resampling method other than Nearest Neighbours) #' @param ... Arguments to be passed to \link{\code{writeRaster}}. #' diff --git a/man/bfmSpOver.Rd b/man/bfmSpOver.Rd index ec2c5ec..c88529c 100644 --- a/man/bfmSpOver.Rd +++ b/man/bfmSpOver.Rd @@ -51,7 +51,12 @@ out <- bfmSpOver(tura, y = sp, start=c(2005,1)) # Visualize the results plot(tura, 166) -points(out, col= out$breakpoint, pch=16, cex = abs(out$magnitude/max(out$magnitude))) + +# Build color palette +colfunc <- colorRampPalette(c("yellow", "red")) +colList <- colfunc(2013 - 2005) +points(out, col= colList[out$breakpoint - 2005], pch=16, cex = abs(out$magnitude/max(out$magnitude))) +# Color corresponds to timing of break and size to magnitude # 2 - SpatialPolygons case data(turaSp) @@ -60,7 +65,11 @@ out2 <- bfmSpOver(tura, y = turaSp, fun = mean, start=c(2002,1)) # Visualize plot(tura, 166) -plot(out2, col = out2$breakpoint, add = TRUE) +# Build color palette +colfunc <- colorRampPalette(c("yellow", "red")) +colList <- colfunc(2013 - 2002) +plot(out2, col = colList[out2$breakpoint - 2002], add = TRUE) +# Interpretation: The redder the latter the break was detected. If transparent, no break detected in spatially aggregated polygon time-series. } \author{ Loic Dutrieux diff --git a/man/cleanMODIS.Rd b/man/cleanMODIS.Rd index 0786515..54d41a3 100644 --- a/man/cleanMODIS.Rd +++ b/man/cleanMODIS.Rd @@ -14,7 +14,7 @@ cleanMODIS(x, data_SDS, QC_SDS, bit = FALSE, QC_val, fill = NULL, ...) \item{bit}{Logical. Is QC information provided bitwise?} -\item{QC_val}{Numeric or vector of numerics. Quality control values to \textbf{keep} in the data. If \code{bit} is set to \code{TRUE} \code{QC_val} is a BYTE (hexadecimal or decimal) where each bit refers to a bit in the QC layer element (i.e.: bitpos = 0xA1 targets bits 7, 5 and 0 --- 1010 0001). When bits targetted by \code{QC_val} are activated, the corresponding observation in the data layer is filtered out.} +\item{QC_val}{Numeric or vector of numerics. Quality control values to \bold{keep} in the data. If \code{bit} is set to \code{TRUE} \code{QC_val} is a BYTE (hexadecimal or decimal) where each bit refers to a bit in the QC layer element (i.e.: bitpos = 0xA1 targets bits 7, 5 and 0 --- 1010 0001). When bits targetted by \code{QC_val} are activated, the corresponding observation in the data layer is filtered out.} \item{fill}{(vector of) Numeric or NULL. Fill values in the data layer to be filtered out. (important to do before reprojection with resampling method other than Nearest Neighbours)} From 17bd5bb4f46d072f402d93dcbeec280dba20400e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc?= Date: Sat, 29 Nov 2014 20:06:34 +0100 Subject: [PATCH 21/23] Issue 41. timeStack now assigns layer names (and not layer.1, layer.2, etc) --- R/timeStack.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/timeStack.R b/R/timeStack.R index 0880cc0..fa8ea82 100644 --- a/R/timeStack.R +++ b/R/timeStack.R @@ -59,8 +59,9 @@ timeStack <- function(x, pattern=NULL, orderChrono = TRUE, ...) { } s <- stack(x) + names(s) <- row.names(getSceneinfo(x)) - time <- getSceneinfo(str_extract(string=basename(x), '(LT4|LT5|LE7|LC8)\\d{13}'))$date + time <- getSceneinfo(x)$date s <- setZ(x=s, z=time) if(hasArg(filename)) { From d9b3fb03d23745dee7361351d70f202e166ff266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc?= Date: Sat, 29 Nov 2014 20:11:11 +0100 Subject: [PATCH 22/23] Issue 42. Documented the possibility to extract single layers with processLandsat(batch) using the vi= argument --- R/processLandsat.R | 2 +- man/processLandsat.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/processLandsat.R b/R/processLandsat.R index c96cf59..1e77674 100644 --- a/R/processLandsat.R +++ b/R/processLandsat.R @@ -3,7 +3,7 @@ #' #' @description Processes a single Landsat scene, from tarball or zip archive (or hdf/tiff if untar is set to FALSE) to vegetation index. Easy to batch using sapply or mclapply for parallel implementation. Data obtained from espi may already contained pre-processed indices layers, in which case they are directly used. #' @param x Character. filename of the tarball or zip archive of the hdf/tiff file. -#' @param vi Character. Vegetation index to be computed or extracted from the archive. Can be either 'ndvi', 'evi', 'savi', 'ndmi'*, 'nbr', 'nbr2'* or 'msavi'*. Indices with * need to be present in the archive. +#' @param vi Character. Vegetation index to be computed or extracted from the archive. Can be either 'ndvi', 'evi', 'savi', 'ndmi'*, 'nbr', 'nbr2'* or 'msavi'*. Indices with * need to be present in the archive. Note that it is also possible to extract single bands using the \code{vi=} argument. \code{vi='sr_band1'} for instance will extract surface reflectance band 1 from the archive and perform the same pre-processing steps as if it was a vegetation index layer. #' @param srdir Character. Directory where the tarball should be uncompressed. Can be ommited if \code{untar} is set to \code{FALSE} #' @param outdir Character. Directory where the vegetation index rasterLayer should be written. #' @param untar Logical. Is there a need to untar data, or have they been previously unpacked. diff --git a/man/processLandsat.Rd b/man/processLandsat.Rd index cb223b2..9fd3d12 100644 --- a/man/processLandsat.Rd +++ b/man/processLandsat.Rd @@ -9,7 +9,7 @@ processLandsat(x, vi = "ndvi", srdir, outdir, untar = TRUE, \arguments{ \item{x}{Character. filename of the tarball or zip archive of the hdf/tiff file.} -\item{vi}{Character. Vegetation index to be computed or extracted from the archive. Can be either 'ndvi', 'evi', 'savi', 'ndmi'*, 'nbr', 'nbr2'* or 'msavi'*. Indices with * need to be present in the archive.} +\item{vi}{Character. Vegetation index to be computed or extracted from the archive. Can be either 'ndvi', 'evi', 'savi', 'ndmi'*, 'nbr', 'nbr2'* or 'msavi'*. Indices with * need to be present in the archive. Note that it is also possible to extract single bands using the \code{vi=} argument. \code{vi='sr_band1'} for instance will extract surface reflectance band 1 from the archive and perform the same pre-processing steps as if it was a vegetation index layer.} \item{srdir}{Character. Directory where the tarball should be uncompressed. Can be ommited if \code{untar} is set to \code{FALSE}} From 9d7051fe316befe813e8f76f7d8714c0ee41f4ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc?= Date: Sat, 29 Nov 2014 20:14:11 +0100 Subject: [PATCH 23/23] Incremented version number to 0.6.0 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 02c1f22..bdda745 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: bfastSpatial Type: Package Title: Utilities to monitor for change on satellite image time-series -Version: 0.5.0 +Version: 0.6.0 Date: 2014-04-06 Author: Loic Dutrieux, Ben DeVries, Jan Verbesselt Maintainer: