r.buffer.py 3.5 KB

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