i.image.mosaic.py 2.9 KB

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