r.buffer.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: r.buffer
  6. # AUTHOR(S): Glynn Clements
  7. # PURPOSE: Replacement for r.buffer using r.grow.distance
  8. #
  9. # COPYRIGHT: (C) 2008, 2010 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: Creates a raster map showing buffer zones surrounding cells that contain non-NULL category values.
  18. #% keywords: raster
  19. #% keywords: buffer
  20. #%End
  21. #%Flag
  22. #% key: z
  23. #% description: Ignore zero (0) data cells instead of NULL cells
  24. #%End
  25. #%Option
  26. #% key: input
  27. #% type: string
  28. #% required: yes
  29. #% multiple: no
  30. #% key_desc: name
  31. #% description: Name of input raster map
  32. #% gisprompt: old,cell,raster
  33. #%End
  34. #%Option
  35. #% key: output
  36. #% type: string
  37. #% required: yes
  38. #% multiple: no
  39. #% key_desc: name
  40. #% description: Name for output raster map
  41. #% gisprompt: new,cell,raster
  42. #%End
  43. #%Option
  44. #% key: distances
  45. #% type: double
  46. #% required: yes
  47. #% multiple: yes
  48. #% description: Distance zone(s)
  49. #%End
  50. #%Option
  51. #% key: units
  52. #% type: string
  53. #% required: no
  54. #% multiple: no
  55. #% options: meters,kilometers,feet,miles,nautmiles
  56. #% description: Units of distance
  57. #% answer: meters
  58. #%End
  59. import sys
  60. import os
  61. import atexit
  62. import math
  63. import grass.script as grass
  64. scales = {
  65. 'meters': 1.0,
  66. 'kilometers': 1000.0,
  67. 'feet': 0.3048,
  68. 'miles': 1609.344,
  69. 'nautmiles': 1852.0
  70. }
  71. # what to do in case of user break:
  72. def cleanup():
  73. grass.run_command('g.remove', quiet = True, flags = 'f', rast = temp_src)
  74. grass.run_command('g.remove', quiet = True, flags = 'f', rast = temp_dist)
  75. def main():
  76. global temp_dist, temp_src
  77. input = options['input']
  78. output = options['output']
  79. distances = options['distances']
  80. units = options['units']
  81. zero = flags['z']
  82. tmp = str(os.getpid())
  83. temp_dist = "r.buffer.tmp.%s.dist" % tmp
  84. temp_src = "r.buffer.tmp.%s.src" % tmp
  85. #check if input file exists
  86. if not grass.find_file(input)['file']:
  87. grass.fatal(_("<%s> does not exist.") % input)
  88. scale = scales[units]
  89. distances = distances.split(',')
  90. distances1 = [scale * float(d) for d in distances]
  91. distances2 = [d * d for d in distances1]
  92. s = grass.read_command("g.proj", flags='j')
  93. kv = grass.parse_key_val(s)
  94. if kv['+proj'] == 'longlat':
  95. metric = 'geodesic'
  96. else:
  97. metric = 'squared'
  98. grass.run_command('r.grow.distance', input = input, metric = metric,
  99. distance = temp_dist)
  100. if zero:
  101. exp = "$temp_src = if($input == 0,null(),1)"
  102. else:
  103. exp = "$temp_src = if(isnull($input),null(),1)"
  104. grass.message(_("Extracting buffers (1/2)..."))
  105. grass.mapcalc(exp, temp_src = temp_src, input = input)
  106. exp = "$output = if(!isnull($input),$input,%s)"
  107. for n, dist2 in enumerate(distances2):
  108. exp %= "if($dist <= %f,%d,%%s)" % (dist2,n + 2)
  109. exp %= "null()"
  110. grass.message(_("Extracting buffers (2/2)..."))
  111. grass.mapcalc(exp, output = output, input = temp_src, dist = temp_dist)
  112. p = grass.feed_command('r.category', map = output, rules = '-')
  113. p.stdin.write("1:distances calculated from these locations\n")
  114. d0 = "0"
  115. for n, d in enumerate(distances):
  116. p.stdin.write("%d:%s-%s %s\n" % (n + 2, d0, d, units))
  117. d0 = d
  118. p.stdin.close()
  119. p.wait()
  120. grass.run_command('r.colors', map = output, color = 'rainbow')
  121. # write cmd history:
  122. grass.raster_history(output)
  123. if __name__ == "__main__":
  124. options, flags = grass.parser()
  125. atexit.register(cleanup)
  126. main()