gdalControls.R 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ### input projection -----
  2. InProj <- function(product) {
  3. if (product$SENSOR[1] == "MODIS") {
  4. if (product$TYPE[1] == "Tile") {
  5. paste0(' -s_srs ', shQuote("+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"))
  6. } else {
  7. paste0(' -s_srs ', shQuote("+proj=longlat +ellps=clrk66 +no_defs"))
  8. }
  9. } else NULL
  10. }
  11. ### output projection -----
  12. OutProj <- function(product, extent, opts = NULL, ...) {
  13. if (is.null(opts))
  14. opts <- combineOptions(...)
  15. cat("########################\n")
  16. if(!is.null(extent@target$outProj)) {
  17. outProj <- checkOutProj(extent@target$outProj, tool = "GDAL")
  18. cat("outProj = ", outProj, " (derived from Raster*/Spatial* object)\n")
  19. } else {
  20. outProj <- checkOutProj(opts$outProj, tool = "GDAL")
  21. cat("outProj = ", outProj, "\n")
  22. }
  23. if (outProj == "asIn") {
  24. if (product$SENSOR[1] == "MODIS") {
  25. if (product$TYPE[1] == "Tile") {
  26. outProj <- "+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
  27. } else {
  28. outProj <- "+proj=longlat +ellps=clrk66 +no_defs" # CMG proj
  29. }
  30. }
  31. }
  32. paste0(' -t_srs ', shQuote(outProj))
  33. }
  34. ### output pixel size -----
  35. PixelSize <- function(extent, opts = NULL, ...) {
  36. if (is.null(opts))
  37. opts <- combineOptions(...)
  38. if(!is.null(extent@target$pixelSize)) {
  39. pixelSize <- extent@target$pixelSize
  40. cat("pixelSize = ", pixelSize, " (derived from Raster* object)\n")
  41. } else {
  42. pixelSize <- opts$pixelSize
  43. cat("pixelSize = ", pixelSize, "\n")
  44. }
  45. if (pixelSize[1] != "asIn") {
  46. if (length(pixelSize) == 1) {
  47. paste(" -tr", pixelSize, pixelSize)
  48. } else {
  49. paste(" -tr", paste0(pixelSize, collapse = " "))
  50. }
  51. } else NULL
  52. }
  53. ### resampling type -----
  54. ResamplingType <- function(opts = NULL, ...) {
  55. if (is.null(opts))
  56. opts <- combineOptions(...)
  57. opts$resamplingType <- checkResamplingType(opts$resamplingType, tool = "gdal")
  58. cat("resamplingType = ", opts$resamplingType, "\n")
  59. paste(" -r", opts$resamplingType)
  60. }
  61. ### target extent -----
  62. TargetExtent <- function(extent, outProj) {
  63. te <- NULL # if extent comes from tileV/H
  64. if (!is.null(extent@target$extent)) { # all extents but not tileV/H
  65. if (is.null(extent@target$outProj)) { # map or list extents (always LatLon)
  66. rx <- raster(extent@target$extent, crs = "+init=epsg:4326")
  67. rx <- projectExtent(rx, outProj)
  68. rx <- extent(rx)
  69. } else {
  70. rx <- extent@target$extent
  71. }
  72. }
  73. if (is.null(extent@target)) {
  74. if(!is.null(extent@extent)) {
  75. rx <- raster(extent@extent, crs = "+init=epsg:4326")
  76. rx <- projectExtent(rx, outProj)
  77. rx <- extent(rx)
  78. }
  79. }
  80. if (exists("rx")) te <- paste(" -te", rx@xmin, rx@ymin, rx@xmax, rx@ymax)
  81. return(te)
  82. }
  83. ### block size -----
  84. BlockSize <- function(opts = NULL, ...) {
  85. if (is.null(opts))
  86. opts <- combineOptions(...)
  87. if (is.null(opts$blockSize)) {
  88. NULL
  89. } else {
  90. opts$blockSize <- as.integer(opts$blockSize)
  91. paste0(" -co BLOCKYSIZE=", opts$blockSize)
  92. }
  93. }
  94. ### output compression -----
  95. OutputCompression <- function(opts = NULL, ...) {
  96. if (is.null(opts))
  97. opts <- combineOptions(...)
  98. if (is.null(opts$compression)) {
  99. " -co compress=lzw -co predictor=2"
  100. } else if (isTRUE(opts$compression)) {
  101. " -co compress=lzw -co predictor=2"
  102. } else {
  103. NULL
  104. }
  105. }
  106. ### quiet output -----
  107. QuietOutput <- function(opts = NULL, ...) {
  108. if (is.null(opts))
  109. opts <- combineOptions(...)
  110. ## if 'quiet = FALSE' or not available, show full console output
  111. if ("quiet" %in% names(opts)) {
  112. if (opts$quiet) " -q" else NULL
  113. } else {
  114. NULL
  115. }
  116. }