doOptions.R 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # this function handles the parameter resamplingTpye and must be placed inside runMrt() and runGdal()
  2. checkResamplingType <- function(resamplingType,tool,quiet=FALSE)
  3. {
  4. if (missing(resamplingType))
  5. {
  6. resamplingType <- "near"
  7. }
  8. resamplingType <- raster::trim(tolower(as.character(resamplingType)))
  9. tool <- toupper(tool)
  10. if (!tool %in% c("GDAL","MRT"))
  11. {
  12. stop("Unknown 'tool'. Allowed are 'MRT' or 'GDAL'")
  13. }
  14. if ("GDAL" %in% tool)
  15. {
  16. if (resamplingType %in% c("nn","cc","bil"))
  17. {
  18. if(resamplingType=="nn")
  19. {
  20. resamplingType <- "near"
  21. }
  22. if(resamplingType=="cc")
  23. {
  24. resamplingType <- "cubic"
  25. }
  26. if(resamplingType=="bil")
  27. {
  28. resamplingType <- "bilinear"
  29. }
  30. } else
  31. {
  32. # for efficiency gdv should be stored as variable
  33. gdv <- checkTools('GDAL',quiet=TRUE)$GDAL$vercheck
  34. if (gdv[2] < 10 & resamplingType %in% c("average","mode"))
  35. {
  36. stop("resamplingType= 'average' and 'mode' requires GDAL >= 1.10.0")
  37. }
  38. }
  39. }
  40. if (tool=="MRT")
  41. {
  42. if(resamplingType %in% c("near","nn"))
  43. {
  44. resamplingType <- "nn"
  45. } else if(resamplingType %in% c("cc","cubic"))
  46. {
  47. resamplingType <- "cc"
  48. } else if(resamplingType %in% c("bil","bilinear"))
  49. {
  50. resamplingType <- "bil"
  51. } else if (resamplingType %in% c("cubicspline","lanczos"))
  52. {
  53. if(!quiet)
  54. {
  55. warning(resamplingType," resamling is only supported by GDAL, not by MRT tool. If you use MRT 'near' is used insead")
  56. }
  57. resamplingType='nn'
  58. } else
  59. {
  60. if(!quiet)
  61. {
  62. warning(resamplingType," not supported by 'MRT' using 'NN'")
  63. }
  64. resamplingType='nn'
  65. }
  66. }
  67. if (resamplingType %in% c("cc","bil","bilinear","cubic","cubicspline","lanczos"))
  68. {
  69. if(!quiet)
  70. {
  71. warning("By not using resamplingType='near'/'nn' some SDS become useless (ie all bit encoded Quality SDS's, or 'day of the year' SDS's). It is strongly recommanded to use resamplingType='near'!")
  72. }
  73. }
  74. if (tool=="MRT")
  75. {
  76. toupper(resamplingType)
  77. } else
  78. {
  79. tolower(resamplingType)
  80. }
  81. return(resamplingType)
  82. }
  83. # checks validity of outProj and returns for tool="MRT" the short name (see mrt manual) and in case of "GDAL" the prj4 string!
  84. checkOutProj <- function(proj, tool, quiet=FALSE)
  85. {
  86. tool <- toupper(tool)
  87. if (!tool %in% c("GDAL", "MRT"))
  88. {
  89. stop("checkOptProj Error: Unknown 'tool'. Allowed are 'MRT' or 'GDAL'")
  90. }
  91. if(proj=="asIn") # lot of troubles because of this!
  92. {
  93. return(proj)
  94. }
  95. # this is here because we could think in a conversion between GDAL and MRT inputs! (the available in MRT is the limiting factor)
  96. MRTprojs <- matrix(byrow=T,ncol=2,
  97. c("AEA", "Albers Equal Area", "ER", "Equirectangular", "GEO", "Geographic",
  98. "IGH", "Interrupted Goode Homolosine", "HAM", "Hammer", "ISIN", "Integerized Sinusoidal",
  99. "LA", "Lambert Azimuthal Equal Area", "LCC", "Lambert Conformal Conic",
  100. "MERCAT", "Mercator", "MOL", "Molleweide", "PS", "Polar Stereographic",
  101. "SIN", "Sinusoidal", "TM", "Transverse Mercator", "UTM", "Universal Transverse Mercator"),
  102. dimnames=list(NULL,c("short","long")))
  103. if (tool=="GDAL") # EPRS:xxxx or xxxx or "+proj=sin...."
  104. { # EPSGinfo is lazy loaded (see: minorFuns.R)
  105. inW <- getOption("warn")
  106. on.exit(options(warn=inW))
  107. options(warn=-1)
  108. if(toupper(proj) %in% toupper(MRTprojs))
  109. {
  110. if (toupper(proj) %in% c("GEO","GEOGRAPHIC"))
  111. {
  112. proj <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")@projargs
  113. } else if (toupper(proj) %in% c("SIN","SINUSOIDAL"))
  114. {
  115. proj <- CRS("+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs")@projargs
  116. } else
  117. {
  118. stop("Could not convert 'outProj' argunemt",proj, "to a sp::CRS compatible string!")
  119. }
  120. } else if(!is.na(as.numeric(proj)))
  121. {
  122. proj <- CRS(paste0("+init=epsg:",proj))@projargs
  123. } else if(length(grep(proj,pattern="EPSG:",ignore.case=TRUE))==1)
  124. {
  125. proj <- CRS(gsub(proj,pattern="^EPSG:",replacement="+init=epsg:", ignore.case=TRUE))@projargs
  126. } else if (inherits(proj,"CRS"))
  127. {
  128. proj <- proj@projargs
  129. } else
  130. {
  131. options(warn=inW) # here warning is usefull
  132. proj <- CRS(proj)@projargs
  133. }
  134. return(proj)
  135. }
  136. if (tool == "MRT")
  137. {
  138. ind <- grep(MRTprojs,pattern=paste("^",proj,"$",sep=""),ignore.case=TRUE)
  139. if(length(ind)==0)
  140. {
  141. cat("'outProj' must be one of:\n")
  142. return(MRTprojs)
  143. } else
  144. {
  145. if(ind > nrow(MRTprojs)) # catch short name
  146. {
  147. indL <- ind
  148. ind <- ind-nrow(MRTprojs)
  149. } else
  150. {
  151. indL <- ind+nrow(MRTprojs)
  152. }
  153. return(list(short = MRTprojs[ind],long = MRTprojs[indL]))
  154. }
  155. }
  156. }
  157. # returns 0 if a given GDAL supports HDF4 else 1
  158. checkGdalDriver <- function(path=NULL)
  159. {
  160. inW <- getOption("warn")
  161. on.exit(options(warn=inW))
  162. options(warn=-1)
  163. path <- correctPath(path)
  164. cmd <- paste0(path,'gdalinfo --formats')
  165. if(.Platform$OS=="windows")
  166. {
  167. driver <- try(shell(cmd,intern=TRUE),silent=TRUE)
  168. } else
  169. {
  170. driver <- try(system(cmd,intern=TRUE), silent=TRUE)
  171. }
  172. if (class(driver) == "try-error")
  173. {
  174. options(warn=inW)
  175. warning("No gdal installation found please install 'gdal' on your system first!")
  176. return(FALSE)
  177. }
  178. if(length(grep(driver,pattern="HDF4"))==0)
  179. {
  180. return(FALSE)
  181. } else
  182. {
  183. return(TRUE)
  184. }
  185. }
  186. combineOptions <- function(checkTools = TRUE, ...)
  187. {
  188. opts <- options()
  189. opts <- opts[grep(names(opts),pattern="^MODIS_*.")] # isolate MODIS_opts
  190. if(length(opts)==0) # if nothing available look for initial options
  191. {
  192. # if(!file.exists("~/.MODIS_Opts.R")) {
  193. # warning("File '~/.MODIS_Opts.R' not found. "
  194. # , "Run MODISoptions() to make settings permanent!")
  195. # }
  196. requireNamespace("MODIS", quietly = TRUE)
  197. MODISoptions(save=FALSE, checkTools = checkTools)
  198. opts <- options() # collects all options
  199. opts <- opts[grep(names(opts),pattern="^MODIS_*.")] # isolate MODIS_opts
  200. }
  201. names(opts) <- gsub(names(opts),pattern="MODIS_",replacement="") # convert names to function arg style
  202. Fopts <- list(...) # collects fun args
  203. if (length(Fopts)==0)
  204. {
  205. Fopts <- NULL
  206. }
  207. opts <- c(Fopts, opts[(!names(opts) %in% names(Fopts))])
  208. return(opts)
  209. }