r.grow.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 G_OPT_R_INPUT
  25. #%end
  26. #%option G_OPT_R_OUTPUT
  27. #%end
  28. #%option
  29. #% key: radius
  30. #% type: double
  31. #% required: no
  32. #% multiple: no
  33. #% description: Radius of buffer in raster cells
  34. #% answer: 1.01
  35. #%end
  36. #%option
  37. #% key: metric
  38. #% type: string
  39. #% required: no
  40. #% multiple: no
  41. #% options: euclidean,maximum,manhattan
  42. #% description: Metric
  43. #% answer: euclidean
  44. #%end
  45. #%option
  46. #% key: old
  47. #% type: integer
  48. #% required: no
  49. #% multiple: no
  50. #% description: Value to write for input cells which are non-NULL (-1 => NULL)
  51. #%end
  52. #%option
  53. #% key: new
  54. #% type: integer
  55. #% required: no
  56. #% multiple: no
  57. #% description: Value to write for "grown" cells
  58. #%end
  59. import sys
  60. import os
  61. import atexit
  62. import math
  63. import grass.script as grass
  64. # what to do in case of user break:
  65. def cleanup():
  66. for map in [temp_dist, temp_val]:
  67. if map:
  68. grass.run_command('g.remove', flags = 'fb', quiet = True,
  69. type='rast', pattern = map)
  70. def main():
  71. global temp_dist, temp_val
  72. input = options['input']
  73. output = options['output']
  74. radius = float(options['radius'])
  75. metric = options['metric']
  76. old = options['old']
  77. new = options['new']
  78. mapunits = flags['m']
  79. tmp = str(os.getpid())
  80. temp_dist = "r.grow.tmp.%s.dist" % tmp
  81. if new == '':
  82. temp_val = "r.grow.tmp.%s.val" % tmp
  83. new = temp_val
  84. else:
  85. temp_val = None
  86. if old == '':
  87. old = input
  88. if not mapunits:
  89. kv = grass.region()
  90. scale = math.sqrt(float(kv['nsres']) * float(kv['ewres']))
  91. radius *= scale
  92. if metric == 'euclidean':
  93. metric = 'squared'
  94. radius = radius * radius
  95. #check if input file exists
  96. if not grass.find_file(input)['file']:
  97. grass.fatal(_("Raster map <%s> not found") % input)
  98. if grass.run_command('r.grow.distance', input = input, metric = metric,
  99. distance = temp_dist, value = temp_val) != 0:
  100. grass.fatal(_("Growing failed. Removing temporary maps."))
  101. grass.mapcalc(
  102. "$output = if(!isnull($input),$old,if($dist < $radius,$new,null()))",
  103. output = output, input = input, radius = radius,
  104. old = old, new = new, dist = temp_dist)
  105. grass.run_command('r.colors', map = output, raster = input)
  106. # write cmd history:
  107. grass.raster_history(output)
  108. if __name__ == "__main__":
  109. options, flags = grass.parser()
  110. atexit.register(cleanup)
  111. main()