r.mask.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 not 'MASKCATS' in grass.gisenv() and not remove:
  61. ## beware: next check is made with != , not with 'is', otherwise:
  62. #>>> grass.raster_info("basin_50K")['datatype'] is "CELL"
  63. #False
  64. # even if:
  65. #>>> "CELL" is "CELL"
  66. #True
  67. if grass.raster_info(input)['datatype'] != "CELL":
  68. grass.fatal(_("Raster map %s must be integer for maskcats parameter") % input)
  69. mapset = grass.gisenv()['MAPSET']
  70. exists = bool(grass.find_file('MASK', element = 'cell', mapset = mapset)['file'])
  71. if remove:
  72. if exists:
  73. grass.run_command('g.remove', rast = 'MASK')
  74. grass.message(_("Raster MASK removed"))
  75. else:
  76. grass.fatal(_("No existing MASK to remove"))
  77. else:
  78. if exists:
  79. if not grass.overwrite():
  80. grass.fatal(_("MASK already found in current mapset. Delete first or overwrite."))
  81. else:
  82. grass.warning(_("MASK already exists and will be overwritten"))
  83. p = grass.feed_command('r.reclass', input = input, output = 'MASK', overwrite = True, rules = '-')
  84. p.stdin.write("%s = 1" % maskcats)
  85. p.stdin.close()
  86. p.wait()
  87. if invert:
  88. global tmp
  89. tmp = "r_mask_%d" % os.getpid()
  90. grass.run_command('g.rename', rast = ('MASK',tmp), quiet = True)
  91. grass.mapcalc("MASK=if(isnull($tmp),1,null())", tmp = tmp)
  92. grass.run_command('g.remove', rast = tmp, quiet = True)
  93. grass.message(_("Inverted MASK created."))
  94. else:
  95. grass.message(_("MASK created."))
  96. grass.message(_("All subsequent raster operations will be limited to MASK area. ") +
  97. "Removing or renaming raster file named MASK will " +
  98. "restore raster operations to normal")
  99. if __name__ == "__main__":
  100. options, flags = grass.parser()
  101. tmp = None
  102. atexit.register(cleanup)
  103. main()