r.unpack.py 3.4 KB

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