Package osgeo :: Package utils :: Module gdalimport |
[hide private]
[frames] | no frames]

# Source Code for Module osgeo.utils.gdalimport

     1  #!/usr/bin/env python3
     2  # ******************************************************************************
     3  #  $Id$
     4  #
     5  #  Name:     gdalimport
     6  #  Project:  GDAL Python Interface
     7  #  Purpose:  Import a GDAL supported file to Tiled GeoTIFF, and build overviews
     8  #  Author:   Frank Warmerdam, warmerdam@pobox.com
     9  #
    10  # ******************************************************************************
    11  #  Copyright (c) 2000, Frank Warmerdam
    12  #
    13  #  Permission is hereby granted, free of charge, to any person obtaining a
    14  #  copy of this software and associated documentation files (the "Software"),
    15  #  to deal in the Software without restriction, including without limitation
    16  #  the rights to use, copy, modify, merge, publish, distribute, sublicense,
    17  #  and/or sell copies of the Software, and to permit persons to whom the
    18  #  Software is furnished to do so, subject to the following conditions:
    19  #
    20  #  The above copyright notice and this permission notice shall be included
    21  #  in all copies or substantial portions of the Software.
    22  #
    23  #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    24  #  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    26  #  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    28  #  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    29  #  DEALINGS IN THE SOFTWARE.
    30  # ******************************************************************************
    31
    32  import os.path
    33  import sys
    34
    35  from osgeo import gdal
    36
    37
    38 -def progress_cb(complete, message, cb_data):
    39      print('%s %d' % (cb_data, complete))
    40
    41
    42 -def main(argv):
    43      gdal.AllRegister()
    44      argv = gdal.GeneralCmdLineProcessor(argv)
    45      if argv is None:
    46          sys.exit(0)
    47
    48      if len(argv) < 2:
    49          print("Usage: gdalimport.py [--help-general] source_file [newfile]")
    50          sys.exit(1)
    51
    52      filename = argv[1]
    53      dataset = gdal.Open(filename)
    54      if dataset is None:
    55          print('Unable to open %s' % filename)
    56          sys.exit(1)
    57
    58      geotiff = gdal.GetDriverByName("GTiff")
    59      if geotiff is None:
    60          print('GeoTIFF driver not registered.')
    61          sys.exit(1)
    62
    63      if len(argv) < 3:
    64          newbase, ext = os.path.splitext(os.path.basename(filename))
    65          newfile = newbase + ".tif"
    66          i = 0
    67          while os.path.isfile(newfile):
    68              i = i + 1
    69              newfile = newbase + "_" + str(i) + ".tif"
    70      else:
    71          newfile = argv[2]
    72
    73      print('Importing to Tiled GeoTIFF file: %s' % newfile)
    74      new_dataset = geotiff.CreateCopy(newfile, dataset, 0,
    75                                       ['TILED=YES', ],
    76                                       callback=progress_cb,
    77                                       callback_data='Translate: ')
    78      dataset = None
    79
    80      print('Building overviews')
    81      new_dataset.BuildOverviews("average", callback=progress_cb,
    82                                 callback_data='Overviews: ')
    83      new_dataset = None
    84
    85      print('Done')
    86
    87
    88  if __name__ == '__main__':
    89      sys.exit(main(sys.argv))
    90