r.mask.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: r.mask
  6. # AUTHOR(S): Michael Barton, Arizona State University
  7. # Markus Neteler
  8. # Converted to Python by Glynn Clements
  9. # PURPOSE: Facilitates creation of raster MASK using r.reclass
  10. # COPYRIGHT: (C) 2005, 2007, 2008 by the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General Public
  13. # License (>=v2). Read the file COPYING that comes with GRASS
  14. # for details.
  15. #
  16. #############################################################################
  17. #%Module
  18. #% description: Create a MASK for limiting raster operation
  19. #% keywords: raster
  20. #% keywords: mask
  21. #%End
  22. #%option
  23. #% key: input
  24. #% type: string
  25. #% gisprompt: old,cell,raster
  26. #% description: Raster map to use as MASK
  27. #% required: no
  28. #%END
  29. #%option
  30. #% key: maskcats
  31. #% type: string
  32. #% description: Category values to use for MASK (format: 1 2 3 thru 7 *)
  33. #% answer: *
  34. #%END
  35. #%flag
  36. #% key: i
  37. #% description: Create inverse MASK from specified 'maskcats' list
  38. #%END
  39. #%flag
  40. #% key: r
  41. #% description: Remove existing MASK (overrides other options)
  42. #%END
  43. import sys
  44. import os
  45. import grass.script as grass
  46. import atexit
  47. def cleanup():
  48. if tmp:
  49. grass.run_command('g.remove', rast = tmp, quiet = True)
  50. def main():
  51. input = options['input']
  52. maskcats = options['maskcats']
  53. remove = flags['r']
  54. invert = flags['i']
  55. if not remove and not input:
  56. grass.fatal(_("Required parameter <input> not set"))
  57. #check if input file exists
  58. if not grass.find_file(input)['file'] and not remove:
  59. grass.fatal(_("<%s> does not exist.") % input)
  60. mapset = grass.gisenv()['MAPSET']
  61. exists = bool(grass.find_file('MASK', element = 'cell', mapset = mapset)['file'])
  62. if remove:
  63. if exists:
  64. grass.run_command('g.remove', rast = 'MASK')
  65. grass.message(_("Raster MASK removed"))
  66. else:
  67. grass.warning(_("No existing MASK to remove"))
  68. else:
  69. if exists and not grass.overwrite():
  70. grass.fatal(_("MASK already found in current mapset. Delete first or overwrite"))
  71. p = grass.feed_command('r.reclass', input = input, output = 'MASK', overwrite = True)
  72. p.stdin.write("%s = 1" % maskcats)
  73. p.stdin.close()
  74. p.wait()
  75. if invert:
  76. global tmp
  77. tmp = "r_mask_%d" % os.getpid()
  78. grass.run_command('g.rename', rast = ('MASK',tmp), quiet = True)
  79. grass.mapcalc("MASK=if(isnull($tmp),1,null())", tmp = tmp)
  80. grass.run_command('g.remove', rast = tmp, quiet = True)
  81. grass.message(_("Inverted MASK created."))
  82. else:
  83. grass.message(_("MASK created."))
  84. grass.message(_("All subsequent raster operations will be limited to MASK area. ") +
  85. "Removing or renaming raster file named MASK will " +
  86. "restore raster operations to normal")
  87. if __name__ == "__main__":
  88. options, flags = grass.parser()
  89. tmp = None
  90. atexit.register(cleanup)
  91. main()