r.reclass.area.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #% guisection: Area
  36. #%end
  37. #%option
  38. #% key: greater
  39. #% type: double
  40. #% description: Greater value option that sets the >= area size limit [hectares]
  41. #% guisection: Area
  42. #%end
  43. import sys
  44. import os
  45. import grass.script as grass
  46. def main():
  47. infile = options['input']
  48. lesser = options['lesser']
  49. greater = options['greater']
  50. outfile = options['output']
  51. s = grass.read_command("g.region", flags = 'p')
  52. kv = grass.parse_key_val(s, sep = ':')
  53. s = kv['projection'].strip().split()
  54. if s == '0':
  55. grass.fatal(_("xy-locations are not supported"))
  56. grass.fatal(_("Need projected data with grids in meters"))
  57. if not lesser and not greater:
  58. grass.fatal(_("You have to specify either lesser= or greater="))
  59. if lesser and greater:
  60. grass.fatal(_("lesser= and greater= are mutually exclusive"))
  61. if lesser:
  62. limit = float(lesser)
  63. if greater:
  64. limit = float(greater)
  65. if not grass.find_file(infile)['name']:
  66. grass.fatal(_("Raster map <%s> not found") % infile)
  67. clumpfile = "%s.clump.%s" % (infile.split('@')[0], outfile)
  68. if not grass.overwrite():
  69. if grass.find_file(clumpfile)['name']:
  70. grass.fatal(_("Temporary raster map <%s> exists") % clumpfile)
  71. grass.message(_("Generating a clumped raster file ..."))
  72. grass.run_command('r.clump', input = infile, output = clumpfile)
  73. if lesser:
  74. grass.message(_("Generating a reclass map with area size less than or equal to %f hectares...") % limit)
  75. else:
  76. grass.message(_("Generating a reclass map with area size greater than or equal to %f hectares...") % limit)
  77. recfile = outfile + '.recl'
  78. p1 = grass.pipe_command('r.stats', flags = 'aln', input = (clumpfile, infile), fs = '|')
  79. p2 = grass.feed_command('r.reclass', input = clumpfile, output = recfile, rules = '-')
  80. for line in p1.stdout:
  81. f = line.rstrip('\r\n').split('|')
  82. if len(f) < 5:
  83. continue
  84. hectares = float(f[4]) * 0.0001
  85. if lesser:
  86. test = hectares <= limit
  87. else:
  88. test = hectares >= limit
  89. if test:
  90. p2.stdin.write("%s = %s %s\n" % (f[0], f[2], f[3]))
  91. p1.wait()
  92. p2.stdin.close()
  93. p2.wait()
  94. grass.message(_("Generating output raster map <%s>...") % outfile)
  95. grass.mapcalc("$outfile = $recfile", outfile = outfile, recfile = recfile)
  96. grass.run_command('g.remove', rast = [recfile, clumpfile], quiet = True)
  97. if __name__ == "__main__":
  98. options, flags = grass.parser()
  99. main()