r.unpack.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.unpack
  5. # AUTHOR(S): Hamish Bowman, Otago University, New Zealand
  6. # Converted to Python by Martin Landa <landa.martin gmail.com>
  7. # PURPOSE: Unpack up a raster map packed with r.pack
  8. # COPYRIGHT: (C) 2004-2008, 2010-2012 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: Unpacks a raster map packed with r.pack.
  17. #% keywords: raster
  18. #% keywords: import
  19. #% keywords: copying
  20. #%end
  21. #%option G_OPT_F_INPUT
  22. #% description: Name of input pack file
  23. #% gisprompt: old,bin,file
  24. #% key_desc: name.pack
  25. #%end
  26. #%option G_OPT_R_OUTPUT
  27. #% description: Name for output raster map (default: taken from input file internals)
  28. #% required: no
  29. #% guisection: Output settings
  30. #%end
  31. #%flag
  32. #% key: o
  33. #% description: Override projection check (use current location's projection)
  34. #% guisection: Output settings
  35. #%end
  36. import os
  37. import sys
  38. import shutil
  39. import tarfile
  40. import atexit
  41. import filecmp
  42. from grass.script import core as grass
  43. def cleanup():
  44. grass.try_rmdir(tmp_dir)
  45. def main():
  46. infile = options['input']
  47. global tmp_dir
  48. tmp_dir = grass.tempdir()
  49. grass.debug('tmp_dir = %s' % tmp_dir)
  50. if not os.path.exists(infile):
  51. grass.fatal(_("File <%s> not found") % infile)
  52. gisenv = grass.gisenv()
  53. mset_dir = os.path.join(gisenv['GISDBASE'],
  54. gisenv['LOCATION_NAME'],
  55. gisenv['MAPSET'])
  56. input_base = os.path.basename(infile)
  57. shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
  58. os.chdir(tmp_dir)
  59. tar = tarfile.TarFile.open(name = input_base, mode = 'r')
  60. try:
  61. data_name = tar.getnames()[0]
  62. except:
  63. grass.fatal(_("Pack file unreadable"))
  64. if options['output']:
  65. map_name = options['output']
  66. else:
  67. map_name = data_name.split('@')[0]
  68. gfile = grass.find_file(name = map_name, element = 'cell',
  69. mapset = '.')
  70. if gfile['file']:
  71. if os.environ.get('GRASS_OVERWRITE', '0') != '1':
  72. grass.fatal(_("Raster map <%s> already exists") % map_name)
  73. else:
  74. grass.warning(_("Raster map <%s> already exists and will be overwritten") % map_name)
  75. # extract data
  76. tar.extractall()
  77. os.chdir(data_name)
  78. # check projection compatibility in a rather crappy way
  79. if not grass.compare_key_value_text_files('PROJ_INFO', os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')) or \
  80. not grass.compare_key_value_text_files('PROJ_UNITS', os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')):
  81. if flags['o']:
  82. grass.warning(_("Projection information does not match. Proceeding..."))
  83. else:
  84. grass.fatal(_("Projection information does not match. Aborting."))
  85. # install in $MAPSET
  86. for element in ['cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist']:
  87. if not os.path.exists(element):
  88. continue
  89. path = os.path.join(mset_dir, element)
  90. if not os.path.exists(path):
  91. os.mkdir(path)
  92. if element == 'cell_misc':
  93. path = os.path.join(mset_dir, element, map_name)
  94. if os.path.exists(path):
  95. shutil.rmtree(path)
  96. shutil.copytree('cell_misc', path)
  97. else:
  98. shutil.copyfile(element, os.path.join(mset_dir, element, map_name))
  99. grass.message(_("Raster map <%s> unpacked") % map_name)
  100. if __name__ == "__main__":
  101. options, flags = grass.parser()
  102. atexit.register(cleanup)
  103. sys.exit(main())