example_script.R 7.43 KB
Newer Older
Florian de Boissieu's avatar
Florian de Boissieu committed
1
# ===============================================================================
Florian de Boissieu's avatar
Florian de Boissieu committed
2
# biodivMapR
Florian de Boissieu's avatar
Florian de Boissieu committed
3
4
5
6
7
8
9
# ===============================================================================
# PROGRAMMERS:
#
# Jean-Baptiste FERET <jb.feret@irstea.fr>
#
# Copyright 2019/06 Jean-Baptiste FERET
#
Florian de Boissieu's avatar
Florian de Boissieu committed
10
# biodivMapR is free software: you can redistribute it and/or modify
Florian de Boissieu's avatar
Florian de Boissieu committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>
#
# ===============================================================================

################################################################################
##              DEFINE PARAMETERS FOR DATASET TO BE PROCESSED                 ##
################################################################################
# path (absolute or relative) for the image to process
# expected to be in ENVI HDR format, BIL interleaved
Florian de Boissieu's avatar
Florian de Boissieu committed
30
Input.Image.File  = system.file('extdata', 'RASTER', 'S2A_T33NUD_20180104_Subset', package = 'biodivMapR')
31
check_data(Input.Image.File)
Florian de Boissieu's avatar
Florian de Boissieu committed
32
33

# convert the image using Convert.Raster2BIL if not in the proper format
34
Input.Image.File  = raster2BIL(Raster.Path = Input.Image.File,
Florian de Boissieu's avatar
Florian de Boissieu committed
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
                                       Sensor = 'SENTINEL_2A',
                                       Convert.Integer = TRUE,
                                       Output.Directory = '~/test')

# full path for the Mask raster corresponding to image to process
# expected to be in ENVI HDR format, 1 band, integer 8bits
# expected values in the raster: 0 = masked, 1 = selected
# set to FALSE if no mask available
Input.Mask.File   = FALSE

# relative or absolute path for the Directory where results will be stored
# For each image processed, a subdirectory will be created after its name
Output.Dir        = 'RESULTS'

# SPATIAL RESOLUTION
# resolution of spatial units for alpha and beta diversity maps (in pixels), relative to original image
# if Res.Map = 10 for images with 10 m spatial resolution, then spatial units will be 10 pixels x 10m = 100m x 100m surfaces
# rule of thumb: spatial units between 0.25 and 4 ha usually match with ground data
# too small Spatial.Res results in low number of pixels per spatial unit, hence limited range of variation of diversity in the image
Spatial.Res       = 10

# PCA FILTERING: 		Set to TRUE if you want second filtering based on PCA outliers to be processed. Slower
FilterPCA         = TRUE

################################################################################
##      Check if the image format is compatible with codes (ENVI BIL)         ##
################################################################################
62
check_data(Input.Image.File)
Florian de Boissieu's avatar
Florian de Boissieu committed
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

################################################################################
##                    DEFINE PARAMETERS FOR METHOD                            ##
################################################################################
nbCPU         = 4
MaxRAM        = 0.5
nbclusters    = 50

################################################################################
##                              PROCESS IMAGES                                ##
################################################################################
# 1- Filter data in order to discard non vegetated / shaded / cloudy pixels
NDVI.Thresh = 0.5
Blue.Thresh = 500
NIR.Thresh  = 1500
print("PERFORM RADIOMETRIC FILTERING")
ImPathShade         = Perform.Radiometric.Filtering(Input.Image.File,Input.Mask.File,Output.Dir,
                                                    NDVI.Thresh = NDVI.Thresh, Blue.Thresh = Blue.Thresh,
                                                    NIR.Thresh = NIR.Thresh)

# 2- Compute PCA for a random selection of pixels in the raster
print("PERFORM PCA ON RASTER")
PCA.Files           = Perform.PCA.Image(Input.Image.File,ImPathShade,Output.Dir,FilterPCA=TRUE,nbCPU=nbCPU,MaxRAM = MaxRAM)

# 3- Select principal components from the PCA raster
Select.Components(Input.Image.File,Output.Dir,PCA.Files,File.Open = TRUE)

################################################################################
##                      MAP ALPHA AND BETA DIVERSITY                          ##
################################################################################

print("MAP SPECTRAL SPECIES")
Map.Spectral.Species(Input.Image.File,Output.Dir,PCA.Files,nbCPU=nbCPU,MaxRAM=MaxRAM)


print("MAP ALPHA DIVERSITY")
# Index.Alpha   = c('Shannon','Simpson')
Index.Alpha   = c('Shannon')
101
map_alpha_div(Input.Image.File,Output.Dir,Spatial.Res,nbCPU=nbCPU,MaxRAM=MaxRAM,Index.Alpha = Index.Alpha)
Florian de Boissieu's avatar
Florian de Boissieu committed
102
103

print("MAP BETA DIVERSITY")
104
map_beta_div(Input.Image.File,Output.Dir,Spatial.Res,nbCPU=nbCPU,MaxRAM=MaxRAM)
Florian de Boissieu's avatar
Florian de Boissieu committed
105
106
107
108
109
110
111
112
113
114
115
116

################################################################################
##          COMPUTE ALPHA AND BETA DIVERSITY FROM FIELD PLOTS                 ##
################################################################################

# location of the spectral species raster needed for validation
TypePCA     = 'SPCA'
Dir.Raster  = file.path(Output.Dir,basename(Input.Image.File),TypePCA,'SpectralSpecies')
Name.Raster = 'SpectralSpecies'
Path.Raster = file.path(Dir.Raster,Name.Raster)

# location of the directory where shapefiles used for validation are saved
Florian de Boissieu's avatar
Florian de Boissieu committed
117
vect        = system.file('extdata', 'VECTOR', package = 'biodivMapR')
Florian de Boissieu's avatar
Florian de Boissieu committed
118
119
120
Shannon.All = list()

# list vector data
121
Path.Vector         = list.shp(vect)
Florian de Boissieu's avatar
Florian de Boissieu committed
122
123
124
125
Name.Vector         = tools::file_path_sans_ext(basename(Path.Vector))

# read raster data including projection
RasterStack         = stack(Path.Raster)
126
Projection.Raster   = projection.file(Path.Raster,'raster')
Florian de Boissieu's avatar
Florian de Boissieu committed
127
128

# get alpha and beta diversity indicators corresponding to shapefiles
129
Biodiv.Indicators           = diversity_from_plots(Raster = Path.Raster, Plots = Path.Vector,NbClusters = nbclusters)
Florian de Boissieu's avatar
Florian de Boissieu committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# if no name
Biodiv.Indicators$Name.Plot = seq(1,length(Biodiv.Indicators$Shannon[[1]]),by = 1)
Shannon.RS                  = c(Biodiv.Indicators$Shannon)[[1]]

####################################################
# write RS indicators
####################################################
# write indicators for alpha diversity
Path.Results = paste(Output.Dir,'/',basename(Input.Image.File),'/',TypePCA,'/VALIDATION/',sep='')
dir.create(Path.Results, showWarnings = FALSE,recursive = TRUE)
write.table(Shannon.RS, file = paste(Path.Results,"ShannonIndex.csv",sep=''), sep="\t", dec=".", na=" ", row.names = Biodiv.Indicators$Name.Plot, col.names= F,quote=FALSE)

Results         =  data.frame(Name.Vector, Biodiv.Indicators$Richness, Biodiv.Indicators$Fisher, Biodiv.Indicators$Shannon,Biodiv.Indicators$Simpson)
names(Results)  = c("ID_Plot", "Species_Richness", "Fisher", "Shannon", "Simpson")
write.table(Results, file = paste(Path.Results,"AlphaDiversity.csv",sep=''), sep="\t", dec=".", na=" ", row.names = F, col.names= T,quote=FALSE)

# write indicators for beta diversity
BC_mean = Biodiv.Indicators$BCdiss
colnames(BC_mean) = rownames(BC_mean) = Biodiv.Indicators$Name.Plot
write.table(BC_mean, file = paste(Path.Results,"BrayCurtis.csv",sep=''), sep="\t", dec=".", na=" ", row.names = F, col.names= T,quote=FALSE)