r.reclass.area.py 4.8 KB

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