r.reclass.area.py 3.7 KB

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