r.reclass.area.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. # 8/2012: added fp maps support, cleanup, removed tabs AK
  16. # 3/2007: added label support MN
  17. # 3/2004: added parser support MN
  18. # 11/2001 added mapset support markus
  19. # 2/2001 fixes markus
  20. # 2000: updated to GRASS 5
  21. # 1998 from NRCS, slightly modified for GRASS 4.2.1
  22. #%module
  23. #% description: Reclasses a raster map greater or less than user specified area size (in hectares).
  24. #% keywords: raster
  25. #% keywords: statistics
  26. #% keywords: aggregation
  27. #%end
  28. #%option G_OPT_R_INPUT
  29. #%end
  30. #%option G_OPT_R_OUTPUT
  31. #%end
  32. #%option
  33. #% key: lesser
  34. #% type: double
  35. #% description: Lesser value option that sets the <= area size limit [hectares]
  36. #% guisection: Area
  37. #%end
  38. #%option
  39. #% key: greater
  40. #% type: double
  41. #% description: Greater value option that sets the >= area size limit [hectares]
  42. #% guisection: Area
  43. #%end
  44. import sys
  45. import os
  46. import atexit
  47. import grass.script as grass
  48. TMPRAST = []
  49. def main():
  50. infile = options['input']
  51. lesser = options['lesser']
  52. greater = options['greater']
  53. outfile = options['output']
  54. s = grass.read_command("g.region", flags = 'p')
  55. kv = grass.parse_key_val(s, sep = ':')
  56. s = kv['projection'].strip().split()
  57. if s == '0':
  58. grass.fatal(_("xy-locations are not supported"))
  59. grass.fatal(_("Need projected data with grids in meters"))
  60. if not lesser and not greater:
  61. grass.fatal(_("You have to specify either lesser= or greater="))
  62. if lesser and greater:
  63. grass.fatal(_("lesser= and greater= are mutually exclusive"))
  64. if lesser:
  65. limit = float(lesser)
  66. if greater:
  67. limit = float(greater)
  68. if not grass.find_file(infile)['name']:
  69. grass.fatal(_("Raster map <%s> not found") % infile)
  70. clumpfile = "%s.clump.%s" % (infile.split('@')[0], outfile)
  71. TMPRAST.append(clumpfile)
  72. if not grass.overwrite():
  73. if grass.find_file(clumpfile)['name']:
  74. grass.fatal(_("Temporary raster map <%s> exists") % clumpfile)
  75. grass.message(_("Generating a clumped raster file ..."))
  76. grass.run_command('r.clump', input = infile, output = clumpfile)
  77. if lesser:
  78. grass.message(_("Generating a reclass map with area size less than or equal to %f hectares...") % limit)
  79. else:
  80. grass.message(_("Generating a reclass map with area size greater than or equal to %f hectares...") % limit)
  81. recfile = outfile + '.recl'
  82. TMPRAST.append(recfile)
  83. flags = 'aln'
  84. if grass.raster_info(infile)['datatype'] in ('FCELL', 'DCELL'):
  85. flags += 'i'
  86. p1 = grass.pipe_command('r.stats', flags = flags, input = (clumpfile, infile), sep = '|')
  87. p2 = grass.feed_command('r.reclass', input = clumpfile, output = recfile, rules = '-')
  88. rules = ''
  89. for line in p1.stdout:
  90. f = line.rstrip('\r\n').split('|')
  91. if len(f) < 5:
  92. continue
  93. hectares = float(f[4]) * 0.0001
  94. if lesser:
  95. test = hectares <= limit
  96. else:
  97. test = hectares >= limit
  98. if test:
  99. rules += "%s = %s %s\n" % (f[0], f[2], f[3])
  100. if rules:
  101. p2.stdin.write(rules)
  102. p1.wait()
  103. p2.stdin.close()
  104. p2.wait()
  105. if p2.returncode != 0:
  106. if lesser:
  107. grass.fatal(_("No areas of size less than or equal to %f hectares found.") % limit)
  108. else:
  109. grass.fatal(_("No areas of size greater than or equal to %f hectares found.") % limit)
  110. grass.message(_("Generating output raster map <%s>...") % outfile)
  111. grass.mapcalc("$outfile = $recfile", outfile = outfile, recfile = recfile)
  112. def cleanup():
  113. """!Delete temporary maps"""
  114. TMPRAST.reverse() # reclassed map first
  115. for rast in TMPRAST:
  116. grass.run_command("g.remove",
  117. rast = rast,
  118. quiet = True)
  119. if __name__ == "__main__":
  120. options, flags = grass.parser()
  121. atexit.register(cleanup)
  122. sys.exit(main())