i.image.mosaic.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 grass.script as grass
  44. def copy_colors(fh, map, offset):
  45. p = grass.pipe_command('r.colors.out', map = map)
  46. for line in p.stdout:
  47. f = line.rstrip('\r\n').split(' ')
  48. if offset:
  49. if f[0] in ['nv', 'default']:
  50. continue
  51. f[0] = str(float(f[0]) + offset)
  52. fh.write(' '.join(f) + '\n')
  53. p.wait()
  54. def get_limit(map):
  55. return grass.raster_info(map)['max']
  56. def make_expression(i, count):
  57. if i > count:
  58. return "null()"
  59. else:
  60. e = make_expression(i + 1, count)
  61. return "if(isnull($image%d),%s,$image%d+$offset%d)" % (i, e, i, i)
  62. def main():
  63. images = options['images'].split(',')
  64. output = options['output']
  65. if not output:
  66. output = '.'.join(images) + '.mosaic'
  67. count = len(images)
  68. grass.warning('Do not forget to set region properly to cover all images.')
  69. offset = 0
  70. offsets = []
  71. parms = {}
  72. for n, img in enumerate(images):
  73. offsets.append(offset)
  74. parms['image%d' % (n + 1)] = img
  75. parms['offset%d' % (n + 1)] = offset
  76. offset += get_limit(img) + 1
  77. grass.message("Mosaicing %d images..." % count)
  78. grass.mapcalc("$output = " + make_expression(1, count),
  79. output = output, **parms)
  80. #modify the color table:
  81. p = grass.feed_command('r.colors', map = output, rules='-')
  82. for img, offset in zip(images, offsets):
  83. print img, offset
  84. copy_colors(p.stdin, img, offset)
  85. p.stdin.close()
  86. p.wait()
  87. grass.message("Ready. File %s created." % output)
  88. # write cmd history:
  89. grass.raster_history(output)
  90. if __name__ == "__main__":
  91. options, flags = grass.parser()
  92. main()