r.unpack.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 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,file,bin_input
  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. #%end
  29. #%flag
  30. #% key: o
  31. #% description: Override projection check (use current location's projection)
  32. #%end
  33. import os
  34. import sys
  35. import shutil
  36. import tarfile
  37. import atexit
  38. import filecmp
  39. from grass.script import core as grass
  40. def cleanup():
  41. grass.try_rmdir(tmp_dir)
  42. def main():
  43. infile = options['input']
  44. global tmp_dir
  45. tmp_dir = grass.tempdir()
  46. grass.debug('tmp_dir = %s' % tmp_dir)
  47. if not os.path.exists(infile):
  48. grass.fatal(_("File <%s> not found" % infile))
  49. gisenv = grass.gisenv()
  50. mset_dir = os.path.join(gisenv['GISDBASE'],
  51. gisenv['LOCATION_NAME'],
  52. gisenv['MAPSET'])
  53. input_base = os.path.basename(infile)
  54. shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
  55. os.chdir(tmp_dir)
  56. tar = tarfile.TarFile.open(name = input_base, mode = 'r')
  57. try:
  58. data_name = tar.getnames()[0]
  59. except:
  60. grass.fatal(_("Pack file unreadable"))
  61. if options['output']:
  62. map_name = options['output']
  63. else:
  64. map_name = data_name
  65. gfile = grass.find_file(name = map_name, element = 'cell',
  66. mapset = '.')
  67. overwrite = os.getenv('GRASS_OVERWRITE')
  68. if gfile['file'] and overwrite != '1':
  69. grass.fatal(_("Raster map <%s> already exists") % map_name)
  70. # extract data
  71. tar.extractall()
  72. os.chdir(data_name)
  73. # check projection compatibility in a rather crappy way
  74. if not grass.compare_key_value_text_files('PROJ_INFO', os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')) or\
  75. not grass.compare_key_value_text_files('PROJ_UNITS', os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')):
  76. if flags['o']:
  77. grass.warning(_("Projection information does not match. Proceeding..."))
  78. else:
  79. grass.fatal(_("Projection information does not match. Aborting."))
  80. # install in $MAPSET
  81. for element in ['cats', 'cell', 'cellhd', 'cell_misc', 'colr', 'fcell', 'hist']:
  82. if not os.path.exists(element):
  83. continue
  84. path = os.path.join(mset_dir, element)
  85. if not os.path.exists(path):
  86. os.mkdir(path)
  87. if element == 'cell_misc':
  88. path = os.path.join(mset_dir, element, map_name)
  89. if os.path.exists(path):
  90. shutil.rmtree(path)
  91. shutil.copytree('cell_misc', path)
  92. else:
  93. shutil.copyfile(element, os.path.join(mset_dir, element, map_name))
  94. grass.verbose(_("Raster map saved to <%s>") % map_name)
  95. if __name__ == "__main__":
  96. options, flags = grass.parser()
  97. atexit.register(cleanup)
  98. sys.exit(main())