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

# Source Code for Module osgeo.utils.gdalchksum

      1  #!/usr/bin/env python3
      2  # ******************************************************************************
      3  #  $Id$
      4  #
      5  #  Project:  GDAL
      6  #  Purpose:  Application to checksum a GDAL image file.
      7  #  Author:   Frank Warmerdam, warmerdam@pobox.com
      8  #
      9  # ******************************************************************************
     10  #  Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
     11  #
     12  #  Permission is hereby granted, free of charge, to any person obtaining a
     13  #  copy of this software and associated documentation files (the "Software"),
     14  #  to deal in the Software without restriction, including without limitation
     15  #  the rights to use, copy, modify, merge, publish, distribute, sublicense,
     16  #  and/or sell copies of the Software, and to permit persons to whom the
     17  #  Software is furnished to do so, subject to the following conditions:
     18  #
     19  #  The above copyright notice and this permission notice shall be included
     20  #  in all copies or substantial portions of the Software.
     21  #
     22  #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     23  #  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24  #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
     25  #  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     26  #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     27  #  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     28  #  DEALINGS IN THE SOFTWARE.
     29  # ******************************************************************************
     30
     31  import sys
     32
     33  from osgeo import gdal
     34
     35
     36 -def Usage():
     37      print('Usage: gdalchksum.py [-b band] [-srcwin xoff yoff xsize ysize] file')
     38      sys.exit(1)
     39
     40
     41 -def main(argv):
     42      srcwin = None
     43      bands = []
     44
     45      filename = None
     46
     47      gdal.AllRegister()
     48      argv = gdal.GeneralCmdLineProcessor(argv)
     49      if argv is None:
     50          sys.exit(0)
     51
     52      # Parse command line arguments.
     53      i = 1
     54      while i < len(argv):
     55          arg = argv[i]
     56
     57          if arg == '-b':
     58              i = i + 1
     59              bands.append(int(argv[i]))
     60
     61          elif arg == '-srcwin':
     62              srcwin = [int(argv[i + 1]), int(argv[i + 2]),
     63                        int(argv[i + 3]), int(argv[i + 3])]
     64              i = i + 4
     65
     66          elif filename is None:
     67              filename = argv[i]
     68
     69          else:
     70              Usage()
     71
     72          i = i + 1
     73
     74      if filename is None:
     75          Usage()
     76
     77      # Open source file
     78
     79      ds = gdal.Open(filename)
     80      if ds is None:
     81          print('Unable to open %s' % filename)
     82          sys.exit(1)
     83
     84      # Default values
     85
     86      if srcwin is None:
     87          srcwin = [0, 0, ds.RasterXSize, ds.RasterYSize]
     88
     89      if not bands:
     90          bands = list(range(1, (ds.RasterCount + 1)))
     91
     92
     93      # Generate checksums
     94
     95      for band_num in bands:
     96          oBand = ds.GetRasterBand(band_num)
     97          result = oBand.Checksum(srcwin[0], srcwin[1], srcwin[2], srcwin[3])
     98          print(result)
     99
    100      ds = None
    101
    102
    103  if __name__ == '__main__':
    104      sys.exit(main(sys.argv))
    105