r.buffer.lowmem.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python3
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: r.buffer.lowmem
  6. # AUTHOR(S): Glynn Clements
  7. # PURPOSE: Low-memory 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. # % label: Creates a raster map showing buffer zones surrounding cells that contain non-NULL category values.
  18. # % description: This is the low-memory alternative to the classic r.buffer module.
  19. # % keyword: raster
  20. # % keyword: buffer
  21. # %end
  22. # %flag
  23. # % key: z
  24. # % description: Ignore zero (0) data cells instead of NULL cells
  25. # %end
  26. # %option G_OPT_R_INPUT
  27. # %end
  28. # %option G_OPT_R_OUTPUT
  29. # %end
  30. # %option
  31. # % key: distances
  32. # % type: double
  33. # % required: yes
  34. # % multiple: yes
  35. # % description: Distance zone(s)
  36. # %end
  37. # %option G_OPT_M_UNITS
  38. # % options: meters,kilometers,feet,miles,nautmiles
  39. # % description: Units of distance
  40. # % answer: meters
  41. # %end
  42. import os
  43. import atexit
  44. import grass.script as grass
  45. from grass.script.utils import encode
  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='fb', type='raster', name=temp_src)
  57. if grass.find_file(temp_dist)['file']:
  58. grass.run_command('g.remove', quiet=True, flags='fb', type='raster', name=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(_("Raster map <%s> not found") % 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, flags='m')
  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. if metric == 'squared':
  92. for n, dist2 in enumerate(distances2):
  93. exp %= "if($dist <= %f,%d,%%s)" % (dist2, n + 2)
  94. else:
  95. for n, dist2 in enumerate(distances1):
  96. exp %= "if($dist <= %f,%d,%%s)" % (dist2, n + 2)
  97. exp %= "null()"
  98. grass.message(_("Extracting buffers (2/2)..."))
  99. grass.mapcalc(exp, output=output, input=temp_src, dist=temp_dist)
  100. p = grass.feed_command('r.category', map=output,
  101. separator=':', rules='-')
  102. msg = "1:distances calculated from these locations\n"
  103. p.stdin.write(encode(msg))
  104. d0 = "0"
  105. for n, d in enumerate(distances):
  106. msg = "%d:%s-%s %s\n" % (n + 2, d0, d, units)
  107. p.stdin.write(encode(msg))
  108. d0 = d
  109. p.stdin.close()
  110. p.wait()
  111. grass.run_command('r.colors', map=output, color='rainbow')
  112. # write cmd history:
  113. grass.raster_history(output)
  114. if __name__ == "__main__":
  115. options, flags = grass.parser()
  116. atexit.register(cleanup)
  117. main()