r.mask.py 3.5 KB

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