i.image.mosaic.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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: imagery
  25. #% keywords: geometry
  26. #% keywords: mosaicking
  27. #%end
  28. #%option G_OPT_R_INPUTS
  29. #%end
  30. #%option G_OPT_R_OUTPUT
  31. #%end
  32. import sys
  33. import os
  34. import grass.script as grass
  35. def copy_colors(fh, map, offset):
  36. p = grass.pipe_command('r.colors.out', map = map)
  37. for line in p.stdout:
  38. f = line.rstrip('\r\n').split(' ')
  39. if offset:
  40. if f[0] in ['nv', 'default']:
  41. continue
  42. f[0] = str(float(f[0]) + offset)
  43. fh.write(' '.join(f) + '\n')
  44. p.wait()
  45. def get_limit(map):
  46. return grass.raster_info(map)['max']
  47. def make_expression(i, count):
  48. if i > count:
  49. return "null()"
  50. else:
  51. e = make_expression(i + 1, count)
  52. return "if(isnull($image%d),%s,$image%d+$offset%d)" % (i, e, i, i)
  53. def main():
  54. images = options['input'].split(',')
  55. output = options['output']
  56. count = len(images)
  57. grass.warning(_('Do not forget to set region properly to cover all images.'))
  58. offset = 0
  59. offsets = []
  60. parms = {}
  61. for n, img in enumerate(images):
  62. offsets.append(offset)
  63. parms['image%d' % (n + 1)] = img
  64. parms['offset%d' % (n + 1)] = offset
  65. offset += get_limit(img) + 1
  66. grass.message(_("Mosaicing %d images...") % count)
  67. grass.mapcalc("$output = " + make_expression(1, count),
  68. output = output, **parms)
  69. #modify the color table:
  70. p = grass.feed_command('r.colors', map = output, rules='-')
  71. for img, offset in zip(images, offsets):
  72. print img, offset
  73. copy_colors(p.stdin, img, offset)
  74. p.stdin.close()
  75. p.wait()
  76. grass.message(_("Done. Raster map <%s> created.") % output)
  77. # write cmd history:
  78. grass.raster_history(output)
  79. if __name__ == "__main__":
  80. options, flags = grass.parser()
  81. main()