r.pack.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.pack
  5. # AUTHOR(S): Hamish Bowman, Otago University, New Zealand
  6. # Converted to Python by Martin Landa <landa.martin gmail.com>
  7. # PURPOSE: Pack up a raster map, collect raster map elements => gzip
  8. # COPYRIGHT: (C) 2004-2013 by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General
  11. # Public License (>=v2). Read the file COPYING that
  12. # comes with GRASS for details.
  13. #
  14. #############################################################################
  15. #%module
  16. #% description: Exports a raster map as GRASS GIS specific archive file
  17. #% keyword: raster
  18. #% keyword: export
  19. #% keyword: copying
  20. #%end
  21. #%option G_OPT_R_INPUT
  22. #% description: Name of raster map to pack up
  23. #%end
  24. #%option G_OPT_F_OUTPUT
  25. #% description: Name for output file (default is <input>.pack)
  26. #% required : no
  27. #%end
  28. #%flag
  29. #% key: c
  30. #% description: Switch the compression off
  31. #%end
  32. import os
  33. import sys
  34. import shutil
  35. import atexit
  36. import tarfile
  37. from grass.script.utils import try_rmdir, try_remove
  38. from grass.script import core as grass
  39. # i18N
  40. import gettext
  41. gettext.install('grassmods', os.path.join(os.getenv("GISBASE"), 'locale'))
  42. def cleanup():
  43. try_rmdir(tmp)
  44. def main():
  45. infile = options['input']
  46. compression_off = flags['c']
  47. mapset = None
  48. if '@' in infile:
  49. infile, mapset = infile.split('@')
  50. if options['output']:
  51. outfile_path, outfile_base = os.path.split(os.path.abspath(options['output']))
  52. else:
  53. outfile_path, outfile_base = os.path.split(os.path.abspath(infile + ".pack"))
  54. outfile = os.path.join(outfile_path, outfile_base)
  55. global tmp
  56. tmp = grass.tempdir()
  57. tmp_dir = os.path.join(tmp, infile)
  58. os.mkdir(tmp_dir)
  59. grass.debug('tmp_dir = %s' % tmp_dir)
  60. gfile = grass.find_file(name=infile, element='cell', mapset=mapset)
  61. if not gfile['name']:
  62. grass.fatal(_("Raster map <%s> not found") % infile)
  63. if os.path.exists(outfile):
  64. if os.getenv('GRASS_OVERWRITE'):
  65. grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
  66. try_remove(outfile)
  67. else:
  68. grass.fatal(_("option <output>: <%s> exists.") % outfile)
  69. grass.message(_("Packing <%s> to <%s>...") % (gfile['fullname'], outfile))
  70. basedir = os.path.sep.join(os.path.normpath(gfile['file']).split(os.path.sep)[:-2])
  71. olddir = os.getcwd()
  72. # copy elements
  73. for element in ['cats', 'cell', 'cellhd', 'colr', 'fcell', 'hist']:
  74. path = os.path.join(basedir, element, infile)
  75. if os.path.exists(path):
  76. grass.debug('copying %s' % path)
  77. shutil.copyfile(path,
  78. os.path.join(tmp_dir, element))
  79. if os.path.exists(os.path.join(basedir, 'cell_misc', infile)):
  80. shutil.copytree(os.path.join(basedir, 'cell_misc', infile),
  81. os.path.join(tmp_dir, 'cell_misc'))
  82. if not os.listdir(tmp_dir):
  83. grass.fatal(_("No raster map components found"))
  84. # copy projection info
  85. # (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
  86. gisenv = grass.gisenv()
  87. for support in ['INFO', 'UNITS', 'EPSG']:
  88. path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
  89. 'PERMANENT', 'PROJ_' + support)
  90. if os.path.exists(path):
  91. shutil.copyfile(path, os.path.join(tmp_dir, 'PROJ_' + support))
  92. # pack it all up
  93. os.chdir(tmp)
  94. if compression_off:
  95. tar = tarfile.TarFile.open(name=outfile_base, mode='w:')
  96. else:
  97. tar = tarfile.TarFile.open(name=outfile_base, mode='w:gz')
  98. tar.add(infile, recursive=True)
  99. tar.close()
  100. try:
  101. shutil.move(outfile_base, outfile)
  102. except shutil.Error as e:
  103. grass.fatal(e)
  104. os.chdir(olddir)
  105. grass.verbose(_("Raster map saved to '%s'" % outfile))
  106. if __name__ == "__main__":
  107. options, flags = grass.parser()
  108. atexit.register(cleanup)
  109. sys.exit(main())