i.image.mosaic.py 2.8 KB

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