r.unpack.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, import, copying
  18. #%end
  19. #%option
  20. #% key: input
  21. #% type: string
  22. #% gisprompt: old,file,input
  23. #% description: Name of input pack file
  24. #% key_desc: path
  25. #% required : yes
  26. #%end
  27. #%option
  28. #% key: output
  29. #% type: string
  30. #% gisprompt: new,cell,raster
  31. #% description: Name for output raster map (default: taken from input file internals)
  32. #% key_desc: name
  33. #% required : no
  34. #%end
  35. #%flag
  36. #% key: o
  37. #% description: Override projection check (use current location's projection)
  38. #%end
  39. import os
  40. import sys
  41. import shutil
  42. import tarfile
  43. import atexit
  44. import filecmp
  45. from grass.script import core as grass
  46. def cleanup():
  47. grass.try_rmdir(tmp_dir)
  48. def main():
  49. infile = options['input']
  50. global tmp_dir
  51. tmp_dir = grass.tempdir()
  52. grass.debug('tmp_dir = %s' % tmp_dir)
  53. if not os.path.exists(infile):
  54. grass.fatal(_("File <%s> not found" % infile))
  55. gisenv = grass.gisenv()
  56. mset_dir = os.path.join(gisenv['GISDBASE'],
  57. gisenv['LOCATION_NAME'],
  58. gisenv['MAPSET'])
  59. input_base = os.path.basename(infile)
  60. shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
  61. os.chdir(tmp_dir)
  62. tar = tarfile.TarFile.open(name = input_base, mode = 'r:gz')
  63. try:
  64. data_name = tar.getnames()[0]
  65. except:
  66. grass.fatal(_("Pack file unreadable"))
  67. if options['output']:
  68. map_name = options['output']
  69. else:
  70. map_name = data_name
  71. gfile = grass.find_file(name = map_name, element = 'cell',
  72. mapset = '.')
  73. overwrite = os.getenv('GRASS_OVERWRITE')
  74. if gfile['file'] and overwrite != '1':
  75. grass.fatal(_("Raster map <%s> already exists") % map_name)
  76. # extract data
  77. tar.extractall()
  78. os.chdir(data_name)
  79. # check projection compatibility in a rather crappy way
  80. if not filecmp.cmp('PROJ_INFO', os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')):
  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.verbose(_("Raster map saved to <%s>") % map_name)
  100. if __name__ == "__main__":
  101. options, flags = grass.parser()
  102. atexit.register(cleanup)
  103. sys.exit(main())