i.image.mosaic.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python
  2. # written by Markus Neteler 18. August 1998 / 20. Jan. 1999
  3. # neteler geog.uni-hannover.de
  4. # mosaic code from Felix Gershunov (Felix spsl.nsc.ru)
  5. # updated for GRASS 5.7 by Michael Barton 2004/04/05
  6. # converted to Python by Glynn Clements
  7. #
  8. # COPYRIGHT: (C) 1999,2007,2008 by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (>=v2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. #
  14. # TODO: - implement g.findfile for 3 and 4 maps (currently only current mapset supported)
  15. # [done for 2 maps]
  16. # - fix isnull() in r.mapcalc for 3 and 4 maps composites
  17. # [done for 2 maps]
  18. # - fix color table length (currently only 256 cols supported, make
  19. # flexible)
  20. # [done for 2 maps]
  21. #--------------------------------------------------
  22. #% Module
  23. #% description: Mosaics several images and extends colormap
  24. #% keywords: raster, imagery, mosaicking
  25. #% End
  26. #% option
  27. #% key: images
  28. #% type: string
  29. #% gisprompt: old,cell,raster
  30. #% description: maps for mosaic.
  31. #% required: yes
  32. #% multiple: yes
  33. #% end
  34. #%option
  35. #% key: output
  36. #% type: string
  37. #% gisprompt: new,cell,raster
  38. #% description: reclass raster output map
  39. #% required : no
  40. #%END
  41. import sys
  42. import os
  43. import string
  44. import grass
  45. def copy_colors(fh, map, offset):
  46. p = grass.pipe_command('r.colors.out', map = map)
  47. for line in p.stdout:
  48. f = line.rstrip('\r\n').split(' ')
  49. if offset:
  50. if f[0] in ['nv', 'default']:
  51. continue
  52. f[0] = str(float(f[0]) + offset)
  53. fh.write(' '.join(f) + '\n')
  54. p.wait()
  55. def get_limit(map):
  56. return grass.raster_info(map)['max']
  57. def make_expression(i, count):
  58. if i > count:
  59. return "null()"
  60. else:
  61. e = make_expression(i + 1, count)
  62. return "if(isnull($image%d),%s,$image%d+$offset%d)" % (i, e, i, i)
  63. def main():
  64. images = options['images'].split(',')
  65. output = options['output']
  66. if not output:
  67. output = '.'.join(images) + '.mosaic'
  68. count = len(images)
  69. grass.warning('Do not forget to set region properly to cover all images.')
  70. offset = 0
  71. offsets = []
  72. parms = {}
  73. for n, img in enumerate(images):
  74. offsets.append(offset)
  75. parms['image%d' % (n + 1)] = img
  76. parms['offset%d' % (n + 1)] = offset
  77. offset += get_limit(img) + 1
  78. grass.message("Mosaicing %d images..." % count)
  79. t = string.Template("$output = " + make_expression(1, count))
  80. print t.template
  81. e = t.substitute(output = output, **parms)
  82. grass.run_command('r.mapcalc', expression = e)
  83. #modify the color table:
  84. p = grass.feed_command('r.colors', map = output, rules='-')
  85. for img, offset in zip(images, offsets):
  86. print img, offset
  87. copy_colors(p.stdin, img, offset)
  88. p.stdin.close()
  89. p.wait()
  90. grass.message("Ready. File %s created." % output)
  91. # write cmd history:
  92. grass.raster_history(output)
  93. if __name__ == "__main__":
  94. options, flags = grass.parser()
  95. main()