Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. ## runs from Grass command prompt, called by Rscript
  2. ## cutting a GDAL imagefile and creating a new, creating GRASS raster and a reclassing of it based on a rules file
  3. ## tested with rt90 and GTiff
  4. ## tested on Ubuntu 14 and windows 10
  5. ## arguments from command line
  6. ## can create new location based on settings of input imagefile or use existing
  7. ## paths to executables, imagefile type and class hardcoded in script for windows and else
  8. ## Folke Larsson, Boden Sweden, 2017
  9.  
  10.  
  11. library(sp)
  12. library(gdalUtils)
  13. library(rgdal)
  14. library(spgrass6)
  15. library(raster)
  16. library(grid)
  17. library(stringr)
  18.  
  19.  
  20. # init
  21. args<-commandArgs(TRUE)
  22. options(warn=0) #
  23.  
  24.  
  25. # command line arguments
  26. in_MapFile <- args[1] # source GTiff map file
  27. in_ulx <- args[2] # Upper-Left X-coordinate
  28. in_uly <- args[3] # Upper-Left Y-coordinate
  29. in_lrx <- args[4] # Lower-Left X-coordinate
  30. in_lry <- args[5] # Lower-Left Y-coordinate
  31. in_Prefix <- args[6] # directory name for log/error files
  32. in_Suffix <- args[7] # specific name of the new map, like area-name, added to given source filename
  33. in_ClassRules <- args[8] # ASCII-file with rules for creating classes in Grass raster
  34. in_location <- args[9] # new or existing Grass location
  35. in_mapset <- args[10] # existing Grass mapset, if new localion always PERMANENT
  36. in_isNewLocation <- args[11] # TRUE or FALSE if a new location should be created
  37.  
  38.  
  39. ## variables
  40. # projwin_str: area of destination file, to cut out from given image
  41. projwin_str <- paste(in_ulx, ", ", in_uly, ", ", in_lrx, ", ", in_lry, sep="")
  42. imagefile_type <- "GTiff"
  43. imagefile_class <- "RasterBrick"
  44.  
  45.  
  46. ## functions
  47. # a fix if using older versions of R
  48. paste0 <- function(..., collapse = NULL) {
  49. paste(..., sep = "", collapse = collapse)
  50. }
  51.  
  52. # check if valid installation of gdal exists
  53. gdal_OK = function() {
  54. gdal_setInstallation()
  55. valid_install <- !is.null(getOption("gdalUtils_gdalPath"))
  56. return(valid_install) # TRUE or FALSE
  57. } # gdal_OK
  58.  
  59. # from given file, like a GTiff and given clip-window, creating new smaller file.
  60. # returns name of new file if its a raster, else returns the other type
  61. cut_mapfile = function(v_in_mapfile, v_file_sep, v_suffix, v_lrx, v_lry, v_ulx, v_uly) {
  62. projwin_str = paste(v_ulx, ", ", v_uly, ", ", v_lrx, ", ", v_lry, sep="")
  63. in_file_length <- str_length(as.character(v_in_mapfile))
  64. cut_pos <- as.numeric(str_locate(v_in_mapfile, '\\.'))
  65. in_file_name <- substr(v_in_mapfile, 1, cut_pos-1)
  66. in_file_type <- substr(v_in_mapfile, cut_pos+1, in_file_length)
  67. dest_file_name <- paste(substr(v_in_mapfile, 1, nchar(v_in_mapfile)-4), v_suffix, sep="")
  68. dest_file <- paste(dest_file_name, paste(".", in_file_type, sep=""), sep="")
  69. can_cut_mapfile <- tryCatch( {
  70. gdal_translate(v_in_mapfile, dest_file, of=imagefile_type, projwin=projwin_str, output_Raster=TRUE, verbose=TRUE)
  71. },
  72. error=function(err) {
  73. stop(err)
  74. },
  75. warning=function( war) {
  76. message(" could not cut off GTiff file with gdal_translate ")
  77. },
  78. finally={
  79. print( paste(" new GTiff created: ", dest_file) )
  80. }
  81. ) # can_cut_mapfile
  82. if( class(can_cut_mapfile) == imagefile_class ) {
  83. return(dest_file)
  84. } else {
  85. return(class(can_cut_mapfile))
  86. } # class
  87. } # cut_mapfile
  88.  
  89. # create Grass raster from given image file, returns name of rastermap
  90. create_grass_rastermap = function(v_mapfile, v_file_sep, v_location, v_mapset, v_is_new_location) {
  91. vect_filepath <- unlist(str_split(v_mapfile, v_file_sep)) # unlist to create vector from list
  92. l_filepath <- length(vect_filepath)
  93. raster_name <- paste(vect_filepath[l_filepath], sep="")
  94. raster_name_t <- substr(raster_name, 1, nchar(raster_name)-4)
  95. if(v_is_new_location == TRUE) {
  96. err_exec <- execGRASS("r.in.gdal", input=v_mapfile, output=raster_name_t, location=new_location, flags=c("overwrite", "e"))
  97. } else if(v_is_new_location == FALSE) {
  98. init_grass <- initGRASS(gisBase=curr_gisBase, home = tempdir(), gisDbase=curr_gisDbase, location=v_location, mapset=v_mapset, override = TRUE)
  99. print(" --- init ----------")
  100. print(init_grass)
  101. print(" --- end init ----------")
  102. err_exec <- execGRASS("r.in.gdal", input=v_mapfile, output=raster_name_t, flags=c("overwrite", "o"))
  103. } else {
  104. stop(" in create_grass_rastermap, v_is_new_location must be TRUE or FALSE !! ")
  105. } # v_is_new_location
  106.  
  107. if (err_exec == 0) {
  108. return(raster_name_t)
  109. } else {
  110. stop( paste(" error creating grass raster map", as.character(err_exec)) )
  111. } # err_exec
  112. } # create_grass_rastermap
  113.  
  114. # reclassify a Grass rastermap from given rules file
  115. create_grass_classes_rastermap = function(v_raster_name, v_rules_file) {
  116. raster_name_classes <- paste("classes_", v_raster_name, sep="")
  117. classes_title <- paste("Reclassed_", raster_name_classes, sep="")
  118. err_exec <- execGRASS("r.reclass", input=raster_name, output=raster_name_classes, rules=v_rules_file, title=classes_title, flags=("overwrite"))
  119. if (err_exec == 0) {
  120. return(raster_name_classes)
  121. } else {
  122. stop( paste(" error creating grass classes raster map", as.character(err_exec)) )
  123. }
  124. }# create_grass_classes_rastermap
  125.  
  126.  
  127. # directory for log- and error-files and some initial logging of system parameters
  128. work_dir <- paste("dir_", in_Prefix, sep="")
  129. file_sep <- .Platform$file.sep
  130. file_psep <- .Platform$path.sep
  131. OS_type <- .Platform$OS.type
  132. os_version <- version$os
  133. system_name <- Sys.info()['sysname']
  134. system_info <- str(Sys.info)
  135.  
  136. # for command prompt
  137. print("-------- command line arguments ------------- ")
  138. print(args)
  139. if (is.na(in_isNewLocation)) {
  140. stop(" no in_isNewLocation argument !! ")
  141. }
  142. print("-------- end command line arguments --------- ")
  143.  
  144. # setting file separator if OS is Windows
  145. # then init Grass paths depending on OS, only tested on windows 10 and Ubuntu 14
  146. if (OS_type == "windows") {
  147. file_sep <- "\\\\"
  148. curr_gisBase <- "C:\\OSGeo4W64\\apps\\grass\\grass-6.4.3"
  149. curr_gisDbase <- "C:\\GIS\\grass\\grassdata"
  150. } else {
  151. curr_gisBase <- "/usr/lib/grass64/"
  152. curr_gisDbase <- "/home/folke/grass/grassdata/"
  153. }
  154.  
  155.  
  156. # create dir for logging
  157. could_create_dir <- dir.create(work_dir)
  158. print(paste(" logging directory path: ", could_create_dir))
  159. dir_path = paste(work_dir, file_sep, sep="")
  160. tmp_path <- paste(dir_path, paste(file_sep, "tmp", sep=""), sep="")
  161. print(paste(" dir: ", dir_path))
  162.  
  163. # directing output to files
  164. msg <- file(paste(dir_path, "errors_and_warnings.log", sep=""), open="wt")
  165. out <- file(paste(dir_path, "logging_output.log" , sep=""), open="wt")
  166. sink(msg, type="message")
  167. sink(out, type="output")
  168.  
  169. # logging system parameters and arguments
  170. print(paste(" projwin ", projwin_str))
  171. print(paste(" file sep: ", file_sep))
  172. print(paste(" file psep: ", file_psep))
  173. print(paste(" OS type: ", OS_type))
  174. print(paste(" sysname: ", system_name))
  175. print(paste(" sysinfo: ", system_info))
  176.  
  177.  
  178. # the "script itself"
  179. is_gdal_OK <- gdal_OK()
  180. print( paste("----- valid install of gdal :", is_gdal_OK) )
  181. if (is_gdal_OK == TRUE) {
  182. new_mapfile <- cut_mapfile(in_MapFile, file_sep, in_Suffix, in_lrx, in_lry, in_ulx, in_uly)
  183. print(paste("new map file: ", new_mapfile))
  184. raster_name <- create_grass_rastermap(new_mapfile, file_sep, in_location, in_mapset, in_isNewLocation)
  185. print(paste("raster name: ", raster_name))
  186. raster_name_classes <- create_grass_classes_rastermap(raster_name, in_ClassRules)
  187. print(paste("raster name classes: ", raster_name_classes))
  188. } else {
  189. stop(" found no valid GDAL installation ")
  190. } # is_gdal_OK
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement