1 | #!/usr/bin/env python |
---|
2 | #****************************************************************************** |
---|
3 | # $Id: gdalimport.py 18194 2009-12-06 20:07:45Z rouault $ |
---|
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 | try: |
---|
33 | from osgeo import gdal |
---|
34 | except ImportError: |
---|
35 | import gdal |
---|
36 | |
---|
37 | import sys |
---|
38 | import os.path |
---|
39 | |
---|
40 | gdal.AllRegister() |
---|
41 | argv = gdal.GeneralCmdLineProcessor( sys.argv ) |
---|
42 | if argv is None: |
---|
43 | sys.exit( 0 ) |
---|
44 | |
---|
45 | if len(argv) < 2: |
---|
46 | print("Usage: gdalimport.py [--help-general] source_file [newfile]") |
---|
47 | sys.exit(1) |
---|
48 | |
---|
49 | def progress_cb( complete, message, cb_data ): |
---|
50 | print(cb_data, complete) |
---|
51 | |
---|
52 | |
---|
53 | filename = argv[1] |
---|
54 | dataset = gdal.Open( filename ) |
---|
55 | if dataset is None: |
---|
56 | print('Unable to open ', filename) |
---|
57 | sys.exit(1) |
---|
58 | |
---|
59 | geotiff = gdal.GetDriverByName("GTiff") |
---|
60 | if geotiff is None: |
---|
61 | print('GeoTIFF driver not registered.') |
---|
62 | sys.exit(1) |
---|
63 | |
---|
64 | if len(argv) < 3: |
---|
65 | newbase, ext = os.path.splitext(os.path.basename(filename)) |
---|
66 | newfile = newbase + ".tif" |
---|
67 | i = 0 |
---|
68 | while os.path.isfile(newfile): |
---|
69 | i = i+1 |
---|
70 | newfile = newbase+"_"+str(i)+".tif" |
---|
71 | else: |
---|
72 | newfile = argv[2] |
---|
73 | |
---|
74 | print('Importing to Tiled GeoTIFF file:', newfile) |
---|
75 | new_dataset = geotiff.CreateCopy( newfile, dataset, 0, |
---|
76 | ['TILED=YES',], |
---|
77 | callback = progress_cb, |
---|
78 | callback_data = 'Translate: ' ) |
---|
79 | dataset = None |
---|
80 | |
---|
81 | print('Building overviews') |
---|
82 | new_dataset.BuildOverviews( "average", callback=progress_cb, |
---|
83 | callback_data = 'Overviews: ' ) |
---|
84 | new_dataset = None |
---|
85 | |
---|
86 | print('Done') |
---|
87 | |
---|
88 | |
---|
89 | |
---|