r.grow.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env python3
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: r.grow
  6. # AUTHOR(S): Glynn Clements
  7. # PURPOSE: Fast replacement for r.grow using r.grow.distance
  8. #
  9. # COPYRIGHT: (C) 2008 by Glynn Clements
  10. #
  11. # This program is free software under the GNU General Public
  12. # License (>=v2). Read the file COPYING that comes with GRASS
  13. # for details.
  14. #
  15. #############################################################################
  16. # %Module
  17. # % description: Generates a raster map layer with contiguous areas grown by one cell.
  18. # % keyword: raster
  19. # % keyword: distance
  20. # % keyword: proximity
  21. # %end
  22. # %flag
  23. # % key: m
  24. # % description: Radius is in map units rather than cells
  25. # %end
  26. # %option G_OPT_R_INPUT
  27. # %end
  28. # %option G_OPT_R_OUTPUT
  29. # %end
  30. # %option
  31. # % key: radius
  32. # % type: double
  33. # % required: no
  34. # % multiple: no
  35. # % description: Radius of buffer in raster cells
  36. # % answer: 1.01
  37. # %end
  38. # %option
  39. # % key: metric
  40. # % type: string
  41. # % required: no
  42. # % multiple: no
  43. # % options: euclidean,maximum,manhattan
  44. # % description: Metric
  45. # % answer: euclidean
  46. # %end
  47. # %option
  48. # % key: old
  49. # % type: integer
  50. # % required: no
  51. # % multiple: no
  52. # % description: Value to write for input cells which are non-NULL (-1 => NULL)
  53. # %end
  54. # %option
  55. # % key: new
  56. # % type: integer
  57. # % required: no
  58. # % multiple: no
  59. # % description: Value to write for "grown" cells
  60. # %end
  61. import os
  62. import atexit
  63. import math
  64. import grass.script as grass
  65. from grass.exceptions import CalledModuleError
  66. # what to do in case of user break:
  67. def cleanup():
  68. for map in [temp_dist, temp_val]:
  69. if map:
  70. grass.run_command('g.remove', flags='fb', quiet=True,
  71. type='rast', name=map)
  72. def main():
  73. global temp_dist, temp_val
  74. input = options['input']
  75. radius = float(options['radius'])
  76. metric = options['metric']
  77. old = options['old']
  78. new = options['new']
  79. mapunits = flags['m']
  80. tmp = str(os.getpid())
  81. temp_dist = "r.grow.tmp.%s.dist" % tmp
  82. shrink = False
  83. if radius < 0.0:
  84. shrink = True
  85. radius = -radius
  86. if new == '' and not shrink:
  87. temp_val = "r.grow.tmp.%s.val" % tmp
  88. new = '"%s"' % temp_val
  89. else:
  90. temp_val = None
  91. if old == '':
  92. old = '"%s"' % input
  93. if not mapunits:
  94. kv = grass.region()
  95. scale = math.sqrt(float(kv['nsres']) * float(kv['ewres']))
  96. radius *= scale
  97. if metric == 'euclidean':
  98. metric = 'squared'
  99. radius = radius * radius
  100. # check if input file exists
  101. if not grass.find_file(input)['file']:
  102. grass.fatal(_("Raster map <%s> not found") % input)
  103. # Workaround for r.mapcalc bug #3475
  104. # Mapcalc will fail if output is a fully qualified map name
  105. out_name = options['output'].split('@')
  106. if len(out_name) == 2:
  107. if out_name[1] != grass.gisenv()['MAPSET']:
  108. grass.fatal(_("Output can be written only to the current mapset"))
  109. output = out_name[0]
  110. else:
  111. output = out_name[0]
  112. if not shrink:
  113. try:
  114. grass.run_command('r.grow.distance', input=input, metric=metric,
  115. distance=temp_dist, value=temp_val)
  116. except CalledModuleError:
  117. grass.fatal(_("Growing failed. Removing temporary maps."))
  118. grass.mapcalc(
  119. '$output = if(!isnull("$input"),$old,if($dist < $radius,$new,null()))',
  120. output=output, input=input, radius=radius,
  121. old=old, new=new, dist=temp_dist)
  122. else:
  123. # shrink
  124. try:
  125. grass.run_command('r.grow.distance', input=input, metric=metric,
  126. distance=temp_dist, value=temp_val, flags='n')
  127. except CalledModuleError:
  128. grass.fatal(_("Shrinking failed. Removing temporary maps."))
  129. grass.mapcalc(
  130. "$output = if(isnull($dist), $old, if($dist < $radius,null(),$old))",
  131. output=output, radius=radius, old=old, dist=temp_dist)
  132. grass.run_command('r.colors', map=output, raster=input)
  133. # write cmd history:
  134. grass.raster_history(output)
  135. if __name__ == "__main__":
  136. options, flags = grass.parser()
  137. atexit.register(cleanup)
  138. main()