r.mask.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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-2009 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: Creates a MASK for limiting raster operation.
  19. #% keywords: raster
  20. #% keywords: mask
  21. #%end
  22. #%option G_OPT_R_INPUT
  23. #% description: Name of raster map to use as mask
  24. #%end
  25. #%option
  26. #% key: maskcats
  27. #% type: string
  28. #% description: Category values to use for mask (format: 1 2 3 thru 7 *)
  29. #% answer: *
  30. #% guisection: Create
  31. #%end
  32. #%flag
  33. #% key: i
  34. #% description: Create inverse mask from specified 'maskcats' list
  35. #% guisection: Create
  36. #%end
  37. #%flag
  38. #% key: r
  39. #% description: Remove existing mask (overrides other options)
  40. #% guisection: Remove
  41. #% suppress_required: yes
  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. if maskcats != '*' and not remove:
  61. if grass.raster_info(input)['datatype'] != "CELL":
  62. grass.fatal(_("Raster map %s must be integer for maskcats parameter") % input)
  63. mapset = grass.gisenv()['MAPSET']
  64. exists = bool(grass.find_file('MASK', element = 'cell', mapset = mapset)['file'])
  65. if remove:
  66. if exists:
  67. grass.run_command('g.remove', rast = 'MASK')
  68. grass.message(_("Raster MASK removed"))
  69. else:
  70. grass.fatal(_("No existing MASK to remove"))
  71. else:
  72. if exists:
  73. if not grass.overwrite():
  74. grass.fatal(_("MASK already found in current mapset. Delete first or overwrite."))
  75. else:
  76. grass.warning(_("MASK already exists and will be overwritten"))
  77. p = grass.feed_command('r.reclass', input = input, output = 'MASK', overwrite = True, rules = '-')
  78. p.stdin.write("%s = 1" % maskcats)
  79. p.stdin.close()
  80. p.wait()
  81. if invert:
  82. global tmp
  83. tmp = "r_mask_%d" % os.getpid()
  84. grass.run_command('g.rename', rast = ('MASK', tmp), quiet = True)
  85. grass.mapcalc("MASK=if(isnull($tmp),1,null())", tmp = tmp)
  86. grass.message(_("Inverted MASK created."))
  87. else:
  88. grass.message(_("MASK created."))
  89. grass.message(_("All subsequent raster operations will be limited to MASK area. ") +
  90. "Removing or renaming raster file named MASK will " +
  91. "restore raster operations to normal")
  92. if __name__ == "__main__":
  93. options, flags = grass.parser()
  94. tmp = None
  95. atexit.register(cleanup)
  96. main()