r.buffer.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 G_OPT_R_INPUT
  26. #%end
  27. #%option G_OPT_R_OUTPUT
  28. #%end
  29. #%option
  30. #% key: distances
  31. #% type: double
  32. #% required: yes
  33. #% multiple: yes
  34. #% description: Distance zone(s)
  35. #%end
  36. #%option G_OPT_M_UNITS
  37. #% options: meters,kilometers,feet,miles,nautmiles
  38. #% description: Units of distance
  39. #% answer: meters
  40. #%end
  41. import sys
  42. import os
  43. import atexit
  44. import math
  45. import grass.script as grass
  46. scales = {
  47. 'meters': 1.0,
  48. 'kilometers': 1000.0,
  49. 'feet': 0.3048,
  50. 'miles': 1609.344,
  51. 'nautmiles': 1852.0
  52. }
  53. # what to do in case of user break:
  54. def cleanup():
  55. if grass.find_file(temp_src)['file']:
  56. grass.run_command('g.remove', quiet = True, flags = 'f', rast = temp_src)
  57. if grass.find_file(temp_dist)['file']:
  58. grass.run_command('g.remove', quiet = True, flags = 'f', rast = temp_dist)
  59. def main():
  60. global temp_dist, temp_src
  61. input = options['input']
  62. output = options['output']
  63. distances = options['distances']
  64. units = options['units']
  65. zero = flags['z']
  66. tmp = str(os.getpid())
  67. temp_dist = "r.buffer.tmp.%s.dist" % tmp
  68. temp_src = "r.buffer.tmp.%s.src" % tmp
  69. #check if input file exists
  70. if not grass.find_file(input)['file']:
  71. grass.fatal(_("<%s> does not exist.") % input)
  72. scale = scales[units]
  73. distances = distances.split(',')
  74. distances1 = [scale * float(d) for d in distances]
  75. distances2 = [d * d for d in distances1]
  76. s = grass.read_command("g.proj", flags='j')
  77. kv = grass.parse_key_val(s)
  78. if kv['+proj'] == 'longlat':
  79. metric = 'geodesic'
  80. else:
  81. metric = 'squared'
  82. grass.run_command('r.grow.distance', input = input, metric = metric,
  83. distance = temp_dist)
  84. if zero:
  85. exp = "$temp_src = if($input == 0,null(),1)"
  86. else:
  87. exp = "$temp_src = if(isnull($input),null(),1)"
  88. grass.message(_("Extracting buffers (1/2)..."))
  89. grass.mapcalc(exp, temp_src = temp_src, input = input)
  90. exp = "$output = if(!isnull($input),$input,%s)"
  91. for n, dist2 in enumerate(distances2):
  92. exp %= "if($dist <= %f,%d,%%s)" % (dist2,n + 2)
  93. exp %= "null()"
  94. grass.message(_("Extracting buffers (2/2)..."))
  95. grass.mapcalc(exp, output = output, input = temp_src, dist = temp_dist)
  96. p = grass.feed_command('r.category', map = output, rules = '-')
  97. p.stdin.write("1:distances calculated from these locations\n")
  98. d0 = "0"
  99. for n, d in enumerate(distances):
  100. p.stdin.write("%d:%s-%s %s\n" % (n + 2, d0, d, units))
  101. d0 = d
  102. p.stdin.close()
  103. p.wait()
  104. grass.run_command('r.colors', map = output, color = 'rainbow')
  105. # write cmd history:
  106. grass.raster_history(output)
  107. if __name__ == "__main__":
  108. options, flags = grass.parser()
  109. atexit.register(cleanup)
  110. main()