i.image.mosaic.py 2.9 KB

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