r.grow.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python
  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. #% keywords: raster
  19. #%End
  20. #%Flag
  21. #% key: m
  22. #% description: radius is in map units rather than cells
  23. #%End
  24. #%Option
  25. #% key: input
  26. #% type: string
  27. #% required: yes
  28. #% multiple: no
  29. #% key_desc: name
  30. #% description: Name of input raster map
  31. #% gisprompt: old,cell,raster
  32. #%End
  33. #%Option
  34. #% key: output
  35. #% type: string
  36. #% required: yes
  37. #% multiple: no
  38. #% key_desc: name
  39. #% description: Name for output raster map
  40. #% gisprompt: new,cell,raster
  41. #%End
  42. #%Option
  43. #% key: radius
  44. #% type: double
  45. #% required: no
  46. #% multiple: no
  47. #% description: Radius of buffer in raster cells
  48. #% answer: 1.01
  49. #%End
  50. #%Option
  51. #% key: metric
  52. #% type: string
  53. #% required: no
  54. #% multiple: no
  55. #% options: euclidean,maximum,manhattan
  56. #% description: Metric
  57. #% answer: euclidean
  58. #%End
  59. #%Option
  60. #% key: old
  61. #% type: integer
  62. #% required: no
  63. #% multiple: no
  64. #% description: Value to write for input cells which are non-NULL (-1 => NULL)
  65. #%End
  66. #%Option
  67. #% key: new
  68. #% type: integer
  69. #% required: no
  70. #% multiple: no
  71. #% description: Value to write for "grown" cells
  72. #%End
  73. import sys
  74. import os
  75. import atexit
  76. import math
  77. import grass.script as grass
  78. # what to do in case of user break:
  79. def cleanup():
  80. for map in [temp_dist, temp_val]:
  81. if map:
  82. grass.run_command('g.remove', quiet = True, flags = 'f', rast = map)
  83. def main():
  84. global temp_dist, temp_val
  85. input = options['input']
  86. output = options['output']
  87. radius = float(options['radius'])
  88. metric = options['metric']
  89. old = options['old']
  90. new = options['new']
  91. mapunits = flags['m']
  92. tmp = str(os.getpid())
  93. temp_dist = "r.grow.tmp.%s.dist" % tmp
  94. if new == '':
  95. temp_val = "r.grow.tmp.%s.val" % tmp
  96. new = temp_val
  97. else:
  98. temp_val = None
  99. if old == '':
  100. old = input
  101. if not mapunits:
  102. kv = grass.region()
  103. scale = math.sqrt(float(kv['nsres']) * float(kv['ewres']))
  104. radius *= scale
  105. if metric == 'euclidean':
  106. metric = 'squared'
  107. radius = radius * radius
  108. #check if input file exists
  109. if not grass.find_file(input)['file']:
  110. grass.fatal(_("<%s> does not exist.") % input)
  111. grass.run_command('r.grow.distance', input = input, metric = metric,
  112. distance = temp_dist, value = temp_val)
  113. grass.mapcalc(
  114. "$output = if(!isnull($input),$old,if($dist < $radius,$new,null()))",
  115. output = output, input = input, radius = radius,
  116. old = old, new = new, dist = temp_dist)
  117. grass.run_command('r.colors', map = output, raster = input)
  118. # write cmd history:
  119. grass.raster_history(output)
  120. if __name__ == "__main__":
  121. options, flags = grass.parser()
  122. atexit.register(cleanup)
  123. main()