Setup

install the bfastSpatial package

if(!require(bfastSpatial)) {
  devtools::install_github('loicdtx/bfastSpatial')
}
devtools::install_github('loicdtx/bfastSpatial')
library(bfastSpatial)

Set paths

path <- '~/sandbox/changeHungary'
inDir <- file.path(path, 'in')
tmpDir <- rasterOptions()$tmpdir

# StepDir is where we store intermediary outputs
stepDir <- file.path(path, 'step')

# ndviDir is a subdirectory of stepDir
# it is where individual NDVI layers will be stored before being stacked
ndviDir <- file.path(stepDir, 'ndvi')

# OutDir is where the output will be stored
outDir <- file.path(path, 'out')

Data download

# Download the archive with the data (~100MB)
download.file('http://www.geo-informatie.nl/dutri001/hungaryData.tar.gz',
              destfile = file.path(path, 'hungaryData.tar.gz'))
# Alternative download: https://googledrive.com/host/0B6NK5CdpGEa7ajFuQ1dBZHUwMW8

# Unpack the data
untar(file.path(path, 'hungaryData.tar.gz'), exdir = inDir)

Make sure all directories exist

for (i in c(tmpDir, stepDir, ndviDir, outDir)) {
  dir.create(i, showWarnings = FALSE)
}

Pre-processing

# Extract
processLandsatBatch(x = inDir, outdir = ndviDir, srdir = tmpDir, delete = TRUE, mask = 'fmask', vi = 'ndvi')

# Make temporal ndvi stack
ndviStack <- timeStack(x = ndviDir, pattern = '^.*\\.grd$', filename = file.path(stepDir, 'ndvi_stack.grd'), datatype = 'INT2S')

Run bfastmonitor on the rasterBrick

# Run bfastmonitor (that part takes a long time)
bfm <- bfmSpatial(x = ndviStack, pptype = 'irregular', start = 2010, formula = response ~ trend + harmon, order = 3, mc.cores = 3, filename = file.path(outDir, 'bfm_2010.grd'), monend = 2013)

visualize the output

plot(ndviStack[[204]], col = grey.colors(255), legend = F)
plot(bfm[[1]], add=TRUE)

Look at individual pixels

With bfmPixel()

# Plot a cloud free recent NDVI layer
plot(ndviStack, 394)

# Call bfmPixel in interactive mode
bfmPixel(x = ndviStack, start = 2010, monend = 2013, interactive = TRUE, plot = TRUE)

# Click on a pixel

With bfmApp

Create time-series file

# Sample regular points over the extent of the data
sp <- sampleRegular(ndviStack, size = 100, sp = TRUE)
# Extract the time-series corresponding to the locations of the sample points
# and store the multiple time-series object in the stepDir of the project
zooTs <- zooExtract(x = ndviStack, sample = sp, file = file.path(stepDir, 'zooTs.rds'))

Install apps dependencies

install.packages('shiny', 'zoo', 'bfast', 'strucchange', 'ggplot2', 'lubridate', 'dplyr')

Run bfmApp

library(shiny)
runGitHub('loicdtx/bfmApp')

With breakpoints App

runGitHub('loicdtx/bfastApp')