r.unpack.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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: Imports a raster map as GRASS GIS specific archive file (packed with r.pack)
  17. #% keyword: raster
  18. #% keyword: import
  19. #% keyword: copying
  20. #%end
  21. #%option G_OPT_F_BIN_INPUT
  22. #% description: Name of input pack file
  23. #% key_desc: name.pack
  24. #%end
  25. #%option G_OPT_R_OUTPUT
  26. #% description: Name for output raster map (default: taken from input file internals)
  27. #% required: no
  28. #% guisection: Output settings
  29. #%end
  30. #%flag
  31. #% key: o
  32. #% description: Override projection check (use location's projection)
  33. #% guisection: Output settings
  34. #%end
  35. import os
  36. import sys
  37. import shutil
  38. import tarfile
  39. import atexit
  40. from grass.script.utils import diff_files, try_rmdir
  41. from grass.script import core as grass
  42. def cleanup():
  43. try_rmdir(tmp_dir)
  44. def main():
  45. infile = options['input']
  46. global tmp_dir
  47. tmp_dir = grass.tempdir()
  48. grass.debug('tmp_dir = {tmpdir}'.format(tmpdir=tmp_dir))
  49. if not os.path.exists(infile):
  50. grass.fatal(_('File {name} not found.'.format(name=infile)))
  51. gisenv = grass.gisenv()
  52. mset_dir = os.path.join(gisenv['GISDBASE'],
  53. gisenv['LOCATION_NAME'],
  54. gisenv['MAPSET'])
  55. input_base = os.path.basename(infile)
  56. shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
  57. os.chdir(tmp_dir)
  58. tar = tarfile.TarFile.open(name=input_base, mode='r')
  59. try:
  60. data_name = tar.getnames()[0]
  61. except:
  62. grass.fatal(_("Pack file unreadable"))
  63. if options['output']:
  64. map_name = options['output']
  65. else:
  66. map_name = data_name.split('@')[0]
  67. gfile = grass.find_file(name=map_name, element='cell', mapset='.')
  68. if gfile['file']:
  69. if os.environ.get('GRASS_OVERWRITE', '0') != '1':
  70. grass.fatal(_('Raster map <{name}> already exists'.format(name=map_name)))
  71. else:
  72. grass.warning(_('Raster map <{name}> already exists and will be overwritten'.format(name=map_name)))
  73. # extract data
  74. tar.extractall()
  75. os.chdir(data_name)
  76. if os.path.exists('cell'):
  77. pass
  78. elif os.path.exists('coor'):
  79. grass.fatal(_('This GRASS GIS pack file contains vector data. Use '
  80. 'v.unpack to unpack <{name}>'.format(name=map_name)))
  81. else:
  82. grass.fatal(_("Pack file unreadable"))
  83. # check projection compatibility in a rather crappy way
  84. if flags['o']:
  85. grass.warning(_("Overriding projection check (using current location's projection)."))
  86. else:
  87. diff_result_1 = diff_result_2 = None
  88. proj_info_file_1 = 'PROJ_INFO'
  89. proj_info_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')
  90. if not grass.compare_key_value_text_files(filename_a=proj_info_file_1,
  91. filename_b=proj_info_file_2,
  92. proj=True):
  93. diff_result_1 = diff_files(proj_info_file_1, proj_info_file_2)
  94. proj_units_file_1 = 'PROJ_UNITS'
  95. proj_units_file_2 = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')
  96. if not grass.compare_key_value_text_files(filename_a=proj_units_file_1,
  97. filename_b=proj_units_file_2,
  98. units=True):
  99. diff_result_2 = diff_files(proj_units_file_1, proj_units_file_2)
  100. if diff_result_1 or diff_result_2:
  101. if diff_result_1:
  102. grass.warning(_("Difference between PROJ_INFO file of packed map "
  103. "and of current location:\n{diff}").format(diff=''.join(diff_result_1)))
  104. if diff_result_2:
  105. grass.warning(_("Difference between PROJ_UNITS file of packed map "
  106. "and of current location:\n{diff}").format(diff=''.join(diff_result_2)))
  107. grass.fatal(_("Projection information does not match. Aborting."))
  108. # install in $MAPSET
  109. for element in ['cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist']:
  110. if not os.path.exists(element):
  111. continue
  112. path = os.path.join(mset_dir, element)
  113. if not os.path.exists(path):
  114. os.mkdir(path)
  115. if element == 'cell_misc':
  116. path = os.path.join(mset_dir, element, map_name)
  117. if os.path.exists(path):
  118. shutil.rmtree(path)
  119. shutil.copytree('cell_misc', path)
  120. else:
  121. shutil.copyfile(element, os.path.join(mset_dir, element, map_name))
  122. grass.message(_('Raster map <{name}> unpacked'.format(name=map_name)))
  123. if __name__ == "__main__":
  124. options, flags = grass.parser()
  125. atexit.register(cleanup)
  126. sys.exit(main())