r.grow.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #% 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 sys
  62. import os
  63. import atexit
  64. import math
  65. import grass.script as grass
  66. from grass.exceptions import CalledModuleError
  67. # what to do in case of user break:
  68. def cleanup():
  69. for map in [temp_dist, temp_val]:
  70. if map:
  71. grass.run_command('g.remove', flags = 'fb', quiet = True,
  72. type='rast', name = map)
  73. def main():
  74. global temp_dist, temp_val
  75. input = options['input']
  76. output = options['output']
  77. radius = float(options['radius'])
  78. metric = options['metric']
  79. old = options['old']
  80. new = options['new']
  81. mapunits = flags['m']
  82. tmp = str(os.getpid())
  83. temp_dist = "r.grow.tmp.%s.dist" % tmp
  84. if new == '':
  85. temp_val = "r.grow.tmp.%s.val" % tmp
  86. new = temp_val
  87. else:
  88. temp_val = None
  89. if old == '':
  90. old = input
  91. if not mapunits:
  92. kv = grass.region()
  93. scale = math.sqrt(float(kv['nsres']) * float(kv['ewres']))
  94. radius *= scale
  95. if metric == 'euclidean':
  96. metric = 'squared'
  97. radius = radius * radius
  98. #check if input file exists
  99. if not grass.find_file(input)['file']:
  100. grass.fatal(_("Raster map <%s> not found") % input)
  101. try:
  102. grass.run_command('r.grow.distance', input=input, metric=metric,
  103. distance=temp_dist, value=temp_val)
  104. except CalledModuleError:
  105. grass.fatal(_("Growing failed. Removing temporary maps."))
  106. grass.mapcalc(
  107. "$output = if(!isnull($input),$old,if($dist < $radius,$new,null()))",
  108. output = output, input = input, radius = radius,
  109. old = old, new = new, dist = temp_dist)
  110. grass.run_command('r.colors', map = output, raster = input)
  111. # write cmd history:
  112. grass.raster_history(output)
  113. if __name__ == "__main__":
  114. options, flags = grass.parser()
  115. atexit.register(cleanup)
  116. main()