Lib_CheckConvertData.R 9.69 KB
Newer Older
jbferet's avatar
jbferet committed
1
# ==============================================================================
Florian de Boissieu's avatar
Florian de Boissieu committed
2
# biodivMapR
jbferet's avatar
jbferet committed
3
4
5
6
7
8
9
10
11
12
13
14
# Lib_CheckConvertData.R
# ==============================================================================
# PROGRAMMERS:
# Jean-Baptiste FERET <jb.feret@irstea.fr>
# Copyright 2018/07 Jean-Baptiste FERET
# ==============================================================================
# This library checks if a raster in format expected for diversity mapping
# if not, can convert the raster into BIL format expected for diversity mapping
# ==============================================================================

#' converts a raster into BIL format as expected by DivMapping codes
#'
15
16
17
18
19
20
#' @param Raster.Path character. Full path for the raster to be converted
#' @param Sensor character. Name of the sensor
#' @param Convert.Integer boolean. Should data be converted into integer ?
#' @param Multiplying.Factor numeric. Multiplying factor (eg convert real reflectance values between 0 and 1 into integer between 0 and 10000).
#' @param Output.Directory character. Path to output directory.
#' @param Multiplying.Factor.Last numeric. Multiplying factor for last band.
jbferet's avatar
jbferet committed
21
22
#'
#' @return Output.Path path for the image converted into ENVI BIL format
23
#' @import raster
24
#' @import tools
jbferet's avatar
jbferet committed
25
#' @export
De Boissieu Florian's avatar
De Boissieu Florian committed
26
raster2BIL <- function(Raster.Path, Sensor = "unknown", Output.Directory = FALSE, Convert.Integer = TRUE, Multiplying.Factor = 1, Multiplying.Factor.Last = 1) {
jbferet's avatar
jbferet committed
27
28

  # get directory and file name of original image
29
30
  Input.File <- basename(Raster.Path)
  Input.Dir <- dirname(Raster.Path)
jbferet's avatar
jbferet committed
31
  # define path where data will be stored
32
33
  if (Output.Directory == FALSE) {
    Output.Path <- paste(Input.Dir, "/Converted_DivMAP/", file_path_sans_ext(Input.File), sep = "")
jbferet's avatar
jbferet committed
34
  } else {
35
36
    dir.create(Output.Directory, showWarnings = FALSE, recursive = TRUE)
    Output.Path <- paste(Output.Directory, "/", file_path_sans_ext(Input.File), sep = "")
jbferet's avatar
jbferet committed
37
  }
38
  message("The converted file will be written in the following location:")
jbferet's avatar
jbferet committed
39
  print(Output.Path)
40
41
  Output.Dir <- dirname(Output.Path)
  dir.create(Output.Dir, showWarnings = FALSE, recursive = TRUE)
jbferet's avatar
jbferet committed
42
43

  # apply multiplying factors
44
45
46
47
  message("reading initial file")
  Output.Img <- Multiplying.Factor * brick(Raster.Path)
  Last.Band.Name <- Output.Img@data@names[length(Output.Img@data@names)]
  Output.Img[[Last.Band.Name]] <- Multiplying.Factor.Last * Output.Img[[Last.Band.Name]]
jbferet's avatar
jbferet committed
48
49

  # convert into integer
50
51
  if (Convert.Integer == TRUE) {
    Output.Img <- round(Output.Img)
jbferet's avatar
jbferet committed
52
53
54
  }

  # write raster
55
56
57
  message("writing converted file")
  if (Convert.Integer == TRUE) {
    r <- writeRaster(Output.Img, filename = Output.Path, format = "EHdr", overwrite = TRUE, datatype = "INT2S")
jbferet's avatar
jbferet committed
58
  } else {
59
    r <- writeRaster(Output.Img, filename = Output.Path, format = "EHdr", overwrite = TRUE)
jbferet's avatar
jbferet committed
60
  }
61
  hdr(r, format = "ENVI")
jbferet's avatar
jbferet committed
62
63

  # remove unnecessary files
64
65
  File2Remove <- paste(Output.Path, ".aux.xml", sep = "")
  File2Remove2 <- paste(file_path_sans_ext(Output.Path), ".aux.xml", sep = "")
jbferet's avatar
jbferet committed
66
67
  file.remove(File2Remove)

68
69
70
  File2Remove <- paste(Output.Path, ".prj", sep = "")
  File2Remove2 <- paste(file_path_sans_ext(Output.Path), ".prj", sep = "")
  if (file.exists(File2Remove)) {
jbferet's avatar
jbferet committed
71
    file.remove(File2Remove)
72
  } else if (file.exists(File2Remove2)) {
jbferet's avatar
jbferet committed
73
74
75
    file.remove(File2Remove2)
  }

76
77
78
  File2Remove <- paste(Output.Path, ".sta", sep = "")
  File2Remove <- paste(file_path_sans_ext(Output.Path), ".sta", sep = "")
  if (file.exists(File2Remove)) {
jbferet's avatar
jbferet committed
79
    file.remove(File2Remove)
80
  } else if (file.exists(File2Remove2)) {
jbferet's avatar
jbferet committed
81
82
83
    file.remove(File2Remove2)
  }

84
85
86
  File2Remove <- paste(Output.Path, ".stx", sep = "")
  File2Remove2 <- paste(file_path_sans_ext(Output.Path), ".stx", sep = "")
  if (file.exists(File2Remove)) {
jbferet's avatar
jbferet committed
87
    file.remove(File2Remove)
88
  } else if (file.exists(File2Remove2)) {
jbferet's avatar
jbferet committed
89
90
91
    file.remove(File2Remove2)
  }

92
93
94
95
  File2Rename <- paste(file_path_sans_ext(Output.Path), ".hdr", sep = "")
  File2Rename2 <- paste(Output.Path, ".hdr", sep = "")
  if (file.exists(File2Rename)) {
    file.rename(from = File2Rename, to = File2Rename2)
jbferet's avatar
jbferet committed
96
97
98
  }

  # change dot into underscore
99
100
101
102
103
104
  Output.Path.US <- file.path(
    dirname(Output.Path),
    gsub(basename(Output.Path), pattern = "[.]", replacement = "_")
  )
  if (!Output.Path.US == Output.Path) {
    file.rename(from = Output.Path, to = Output.Path.US)
jbferet's avatar
jbferet committed
105
106
  }

107
108
109
110
111
  Output.Path.US.HDR <- paste0(Output.Path.US, ".hdr")
  if (!Output.Path.US.HDR == paste0(Output.Path, ".hdr")) {
    file.rename(from = paste0(Output.Path, ".hdr"), to = Output.Path.US.HDR)
    ### UTILITY?? ###
    file.rename(from = Output.Path.US.HDR, to = Output.Path.US.HDR)
jbferet's avatar
jbferet committed
112
113
114
  }


115
  if (!Sensor == "unknown") {
Florian de Boissieu's avatar
Florian de Boissieu committed
116
    HDR.Temp.Path <- system.file("extdata", "HDR", paste0(Sensor, ".hdr"), package = "biodivMapR")
117
118
    if (file.exists(HDR.Temp.Path)) {
      message("reading header template corresponding to the sensor located here:")
jbferet's avatar
jbferet committed
119
120
      print(HDR.Temp.Path)
      # get raster template corresponding to the sensor
121
      HDR.Template <- read.ENVI.header(HDR.Temp.Path)
jbferet's avatar
jbferet committed
122
123
      # get info to update hdr file
      # read hdr
124
125
126
      HDR.input <- read.ENVI.header(Get.HDR.Name(Output.Path))
      if (!is.null(HDR.Template$wavelength)) {
        HDR.input$wavelength <- HDR.Template$wavelength
jbferet's avatar
jbferet committed
127
      }
128
129
      if (!is.null(HDR.Template$`sensor type`)) {
        HDR.input$`sensor type` <- HDR.Template$`sensor type`
jbferet's avatar
jbferet committed
130
      }
131
132
      if (!is.null(HDR.Template$`band names`)) {
        HDR.input$`band names` <- HDR.Template$`band names`
jbferet's avatar
jbferet committed
133
      }
134
135
      if (!is.null(HDR.Template$`wavelength units`)) {
        HDR.input$`wavelength units` <- HDR.Template$`wavelength units`
jbferet's avatar
jbferet committed
136
137
      }
      # define visual stretch in the VIS domain
138
      HDR.input$`default stretch` <- "0 1000 linear"
jbferet's avatar
jbferet committed
139
140
      # write corresponding hdr file
      write.ENVI.header(HDR.input, Get.HDR.Name(Output.Path))
141
142
    } else if (!file.exists(HDR.Temp.Path)) {
      message("Header template corresponding to the sensor expected to be found here")
jbferet's avatar
jbferet committed
143
      print(HDR.Temp.Path)
144
      message("please provide this header template in order to write info in HDR file")
jbferet's avatar
jbferet committed
145
      print(Get.HDR.Name(Output.Path))
146
      message("or manually add wavelength location in HDR file, if relevant")
jbferet's avatar
jbferet committed
147
    }
148
149
  } else if (Sensor == "unknown") {
    message("please make sure that the follozing header file contains information required")
jbferet's avatar
jbferet committed
150
    print(Get.HDR.Name(Output.Path))
151
    message("or manually add wavelength location in HDR file, if relevant")
jbferet's avatar
jbferet committed
152
153
154
155
156
157
158
159
160
161
  }
  return(Output.Path)
}

#' Checks if the data to be processed has the format type expected
#'
#' @param Raster.Path full path for the raster to be converted
#' @param Mask is the raster a mask?
#'
#' @export
De Boissieu Florian's avatar
De Boissieu Florian committed
162
check_data <- function(Raster.Path, Mask = FALSE) {
163
  HDR.Path <- Get.HDR.Name(Raster.Path)
jbferet's avatar
jbferet committed
164
  # check if the hdr file exists
165
166
167
168
169
170
171
172
173
174
  if (file.exists(HDR.Path)) {
    HDR <- read.ENVI.header(HDR.Path)
    if (Mask == FALSE & (!HDR$interleave == "bil") & (!HDR$interleave == "BIL")) {
      message("")
      message("*********************************************************")
      message("The image format may not compatible with the processing chain")
      message("Image format expected:")
      message("ENVI hdr file with band interleaved by line (BIL) file format")
      message("")
      message("Current Image format")
jbferet's avatar
jbferet committed
175
      print(HDR$interleave)
176
177
178
179
180
181
      message("Please run the function named ")
      print("Convert.Raster2BIL")
      message("in order to convert your raster data")
      message("or use appropriate software")
      message("*********************************************************")
      message("")
jbferet's avatar
jbferet committed
182
      stop()
183
184
185
186
187
188
189
190
    } else if (Mask == FALSE & ((HDR$interleave == "bil") | (HDR$interleave == "BIL"))) {
      if (HDR$`wavelength units` == "Unknown") {
        message("")
        message("*********************************************************")
        message("Please make sure the wavelengths are in nanometers")
        message("if not, stop processing and convert wavelengths in nanometers in HDR file")
        message("*********************************************************")
        message("")
jbferet's avatar
jbferet committed
191
      }
192
193
194
195
196
197
198
      if ((!HDR$`wavelength units` == "Nanometers") & (!HDR$`wavelength units` == "nanometers")) {
        message("")
        message("*********************************************************")
        message("Please make sure the wavelengths are in nanometers")
        message("if not, stop processing and convert wavelengths in nanometers in HDR file")
        message("*********************************************************")
        message("")
jbferet's avatar
jbferet committed
199
      }
200
201
202
203
204
205
      if (HDR$`wavelength units` == "micrometers") {
        message("")
        message("*********************************************************")
        message("Please convert wavelengths in nanometers in HDR file")
        message("*********************************************************")
        message("")
jbferet's avatar
jbferet committed
206
207
        stop()
      }
208
209
210
211
212
213
      if ((HDR$`wavelength units` == "nanometers") | (HDR$`wavelength units` == "Nanometers")) {
        message("")
        message("*********************************************************")
        message("       All information seem OK for image processing      ")
        message("*********************************************************")
        message("")
jbferet's avatar
jbferet committed
214
215
216
      }
    }
  } else {
217
218
219
    message("")
    message("*********************************************************")
    message("The following HDR file was expected, but could not be found:")
jbferet's avatar
jbferet committed
220
    print(HDR.Path)
221
222
223
224
225
226
227
228
229
    message("The image format may not compatible with the processing chain")
    message("Image format expected:")
    message("ENVI hdr file with band interleaved by line (BIL) file format")
    message("")
    message("Please run the function named ")
    print("Convert.Raster2BIL")
    message("in order to convert your raster data")
    message("*********************************************************")
    message("")
jbferet's avatar
jbferet committed
230
231
    stop()
  }
232
  return(invisible())
jbferet's avatar
jbferet committed
233
}