r.reclass.area.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.reclass.area
  5. # AUTHOR(S): NRCS
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: Reclasses a raster map greater or less than user specified area size (in hectares)
  8. # COPYRIGHT: (C) 1999,2008 by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (>=v2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. #
  14. #############################################################################
  15. # 3/2007: added label support MN
  16. # 3/2004: added parser support MN
  17. # 11/2001 added mapset support markus
  18. # 2/2001 fixes markus
  19. # 2000: updated to GRASS 5
  20. # 1998 from NRCS, slightly modified for GRASS 4.2.1
  21. #%module
  22. #% description: Reclasses a raster map greater or less than user specified area size (in hectares).
  23. #% keywords: raster
  24. #% keywords: statistics
  25. #% keywords: aggregation
  26. #%end
  27. #%option G_OPT_R_INPUT
  28. #%end
  29. #%option G_OPT_R_OUTPUT
  30. #%end
  31. #%option
  32. #% key: lesser
  33. #% type: double
  34. #% description: Lesser value option that sets the <= area size limit [hectares]
  35. #%end
  36. #%option
  37. #% key: greater
  38. #% type: double
  39. #% description: Greater value option that sets the >= area size limit [hectares]
  40. #%end
  41. import sys
  42. import os
  43. import grass.script as grass
  44. def main():
  45. infile = options['input']
  46. lesser = options['lesser']
  47. greater = options['greater']
  48. outfile = options['output']
  49. s = grass.read_command("g.region", flags = 'p')
  50. kv = grass.parse_key_val(s, sep = ':')
  51. s = kv['projection'].strip().split()
  52. if s == '0':
  53. grass.fatal(_("xy-locations are not supported"))
  54. grass.fatal(_("Need projected data with grids in meters"))
  55. if not lesser and not greater:
  56. grass.fatal(_("You have to specify either lesser= or greater="))
  57. if lesser and greater:
  58. grass.fatal(_("lesser= and greater= are mutually exclusive"))
  59. if lesser:
  60. limit = float(lesser)
  61. if greater:
  62. limit = float(greater)
  63. if not grass.find_file(infile)['name']:
  64. grass.fatal(_("Raster map <%s> not found") % infile)
  65. clumpfile = "%s.clump.%s" % (infile.split('@')[0], outfile)
  66. if not grass.overwrite():
  67. if grass.find_file(clumpfile)['name']:
  68. grass.fatal(_("Temporary raster map <%s> exists") % clumpfile)
  69. grass.message(_("Generating a clumped raster file ..."))
  70. grass.run_command('r.clump', input = infile, output = clumpfile)
  71. if lesser:
  72. grass.message(_("Generating a reclass map with area size less than or equal to %f hectares...") % limit)
  73. else:
  74. grass.message(_("Generating a reclass map with area size greater than or equal to %f hectares...") % limit)
  75. recfile = outfile + '.recl'
  76. p1 = grass.pipe_command('r.stats', flags = 'aln', input = (clumpfile, infile), fs = '|')
  77. p2 = grass.feed_command('r.reclass', input = clumpfile, output = recfile, rules = '-')
  78. for line in p1.stdout:
  79. f = line.rstrip('\r\n').split('|')
  80. if len(f) < 5:
  81. continue
  82. hectares = float(f[4]) * 0.0001
  83. if lesser:
  84. test = hectares <= limit
  85. else:
  86. test = hectares >= limit
  87. if test:
  88. p2.stdin.write("%s = %s %s\n" % (f[0], f[2], f[3]))
  89. p1.wait()
  90. p2.stdin.close()
  91. p2.wait()
  92. grass.message(_("Generating output raster map <%s>...") % outfile)
  93. grass.mapcalc("$outfile = $recfile", outfile = outfile, recfile = recfile)
  94. grass.run_command('g.remove', rast = [recfile, clumpfile], quiet = True)
  95. if __name__ == "__main__":
  96. options, flags = grass.parser()
  97. main()