-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
455 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: <[email protected]> | ||
|
@@ -17,5 +17,6 @@ Depends: | |
gdalUtils, | ||
stringr, | ||
rgdal, | ||
bitops | ||
bitops, | ||
zoo | ||
License: tbd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#' 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 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 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} | ||
#' | ||
#' @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) | ||
#' | ||
#' # 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) | ||
#' # 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) | ||
#' # 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 | ||
#' | ||
#' @export | ||
|
||
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, 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')) { | ||
out <- SpatialPolygonsDataFrame(y, data = bfm, match.ID = FALSE) | ||
} else if(inherits(y, 'SpatialLines')) { | ||
out <- SpatialLinesDataFrame(y, data = bfm, match.ID = FALSE) | ||
} else if(class(y) == 'extent') { | ||
y <- polygonFromExtent(y) | ||
out <- SpatialPolygonsDataFrame(y, data = bfm, match.ID = FALSE) | ||
proj4string(out) <- CRS(projection(x)) | ||
} | ||
return(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#' Runs bfastmonitor on a zoo object | ||
#' | ||
#' @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. | ||
#' @param ... Arguments to be passed to \link{bfastmonitor} | ||
#' | ||
#' @return A dataframe | ||
#' | ||
#' @author Loic Dutrieux | ||
#' | ||
#' @import bfast | ||
#' | ||
#' @export | ||
#' | ||
|
||
|
||
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 = bfastmonitorFun, mc.cores = mc.cores, ...) | ||
# Convert list to df | ||
do.call(rbind, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.