-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.R
128 lines (90 loc) · 3.58 KB
/
main.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# setting up workspace and libraries
library(raster)
library(devtools)
# install and load bfastspatial from github
install_github('dutri001/bfastSpatial') #requires devtools
library(bfastSpatial)
# Set the working directory
projectPath <- "C:/Rony/dev/DeforestationPeru"
inputdata <- "data/path5_row68"
outputdata <- "output/"
setwd(projectPath)
# # # # # Scene ID extraction # # # #
# Read both csv files
csv1 <- read.csv(file = "data/Orderlist/path5_row68/LSR_LANDSAT_8_98327.csv")
csv2 <- read.csv(file = "data/Orderlist/path5_row68/LSR_LANDSAT_ETM_COMBINED_98328.csv")
# Create a text file
order_list <- file("output/orderlist.txt", "w")
# Retrieve scene ID's from first csv file
sceneID <- as.character(csv1$Landsat.Scene.Identifier)
# Write the ID's in the text file line by line
for(i in 1:length(sceneID)) {
ID <- sceneID[i]
print(ID)
writeLines(ID, order_list, sep="\n")
}
# Retrieve scene ID's from second csv file
sceneID <- as.character(csv2$Landsat.Scene.Identifier)
# Write the ID's in the text file line by line
for(i in 1:length(sceneID)) {
ID <- sceneID[i]
print(ID)
writeLines(ID, order_list, sep="\n")
}
# close connection to file
close(order_list)
# # # # # Pre-processing the Landsat scenes # # # #
# Create a temporary directory to store the output files.
srdir <- dirout <- file.path(dirname(rasterTmpFile()), 'bfmspatial')
dir.create(dirout, showWarning=FALSE)
# Create an extent variable
newExtent <- extent(c(580485, 617265, -1251615, -1204275))
# List the Landsat scenes
inputList <- list.files(inputdata, full.names=TRUE)
# Process new landsat scenes
processLandsatBatch(x = inputList, outdir = dirout, srdir = srdirNDMI, delete = TRUE, vi = 'ndmi',
mask = 'cfmask', keep = 0, e = newExtent, overwrite = TRUE)
# List the processed NDMI scenes for stacking
nmdiList <- list.files(dirout, pattern=glob2rx('ndmi*.grd'), full.names = TRUE)
# Generate a file name for the output stack
stackName <- file.path(outputdata, 'stackNDMI_path5_row68.grd')
# Stack the NDMI scenes
ndmiStack <- timeStack(x=ndmiList, filename=stackName, datatype='INT2S', overwrite=TRUE)
#Plot the results
plot(bfm$bfm)
# # # # # Exploring brick with bfmPixel # # # #
#Load brick
ndmiBrick <- brick("output/stack/stackNDMI.grd")
#Plot one scene from the Brick
plot(ndmiBrick[[1]])
# run bfmPixel() in interactive mode with a monitoring period
# starting @ the 1st day in 2015
bfm <- bfmPixel(ndmiBrick, start=c(2014,1), sensor = "ETM+", formula = response~harmon, order = 1, history = c(2005, 1), interactive=TRUE)
#Click on a pixel in the plot.
#Plot the results
plot(bfm$bfm)
# # # # # Run bfmSpatial on # # # #
out <- file.path(outputdata, "bfmSpatial.grd")
bfmSpatial(infl, start = c(2014, 1), sensor = "ETM+", formula = response~harmon, order = 1, history = c(2005, 1), filename = out)
# # # # # Post processing # # # #
#load the bfm
bfm <- brick("output/bfmSpatial/bfmSpatial_path5_row68.grd")
#extract change raster
change <- raster(bfm, 1)
#convert breakpoint values to change months
months <- changeMonth(change)
# set up labels and colourmap for months
monthlabs <- c("jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec")
cols <- rainbow(12)
plot(months, col=cols, breaks=c(1:12), legend=FALSE)
# insert custom legend
legend("bottomright", legend=monthlabs, cex=0.5, fill=cols, ncol=2)
# extract magn raster
magn <- raster(bfm, 2)
# make a version showing only breakpoing pixels
magn_bkp <- magn
magn_bkp[is.na(change)] <- NA
op <- par(mfrow=c(1, 2))
plot(magn_bkp, main="Magnitude: breakpoints")
plot(magn, main="Magnitude: all pixels")