r.reclass.area.py 3.7 KB

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