r.mask.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, mask
  20. #%End
  21. #%option
  22. #% key: input
  23. #% type: string
  24. #% gisprompt: old,cell,raster
  25. #% description: Raster map to use as MASK
  26. #% required: no
  27. #%END
  28. #%option
  29. #% key: maskcats
  30. #% type: string
  31. #% description: Category values to use for MASK (format: 1 2 3 thru 7 *)
  32. #% answer: *
  33. #%END
  34. #%flag
  35. #% key: i
  36. #% description: Create inverse MASK from specified 'maskcats' list
  37. #%END
  38. #%flag
  39. #% key: r
  40. #% description: Remove existing MASK (overrides other options)
  41. #%END
  42. import sys
  43. import os
  44. import grass
  45. import atexit
  46. def cleanup():
  47. if tmp:
  48. grass.run_command('g.remove', rast = tmp, quiet = True)
  49. def main():
  50. input = options['input']
  51. maskcats = options['maskcats']
  52. remove = flags['r']
  53. invert = flags['i']
  54. if not remove and not input:
  55. grass.fatal("Required parameter <input> not set")
  56. mapset = grass.gisenv()['MAPSET']
  57. exists = bool(grass.find_file('MASK', element = 'cell', mapset = mapset)['file'])
  58. if remove:
  59. if exists:
  60. grass.run_command('g.remove', rast = 'MASK')
  61. grass.message("Raster MASK removed")
  62. else:
  63. grass.warning("No existing MASK to remove")
  64. else:
  65. if exists and not grass.overwrite():
  66. grass.fatal("MASK already found in current mapset. Delete first or overwrite")
  67. p = grass.feed_command('r.reclass', input = input, output = 'MASK', overwrite = True)
  68. p.stdin.write("%s = 1" % maskcats)
  69. p.stdin.close()
  70. p.wait()
  71. if invert:
  72. global tmp
  73. tmp = "r_mask_%d" % os.getpid()
  74. grass.run_command('g.rename', rast = ('MASK',tmp), quiet = True)
  75. grass.mapcalc("MASK=if(isnull($tmp),1,null())", tmp = tmp)
  76. grass.run_command('g.remove', rast = tmp, quiet = True)
  77. grass.message("Inverted MASK created.")
  78. else:
  79. grass.message("MASK created.")
  80. grass.message("All subsequent raster operations will be limited to MASK area" +
  81. "Removing or renaming raster file named MASK will" +
  82. "restore raster operations to normal")
  83. if __name__ == "__main__":
  84. options, flags = grass.parser()
  85. tmp = None
  86. atexit.register(cleanup)
  87. main()