gdalcompare.py 10.9 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#!C:\OSGEO4~1\bin\python.exe
# -*- coding: utf-8 -*-
# ******************************************************************************
#
#  Project:  GDAL
#  Purpose:  Compare two files for differences and report.
#  Author:   Frank Warmerdam, warmerdam@pobox.com
#
# ******************************************************************************
#  Copyright (c) 2012, Frank Warmerdam <warmerdam@pobox.com>
#
#  Permission is hereby granted, free of charge, to any person obtaining a
#  copy of this software and associated documentation files (the "Software"),
#  to deal in the Software without restriction, including without limitation
#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
#  and/or sell copies of the Software, and to permit persons to whom the
#  Software is furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included
#  in all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#  DEALINGS IN THE SOFTWARE.
# ******************************************************************************

import os
import sys
import filecmp

from osgeo import gdal
from osgeo import osr

#######################################################


def compare_metadata(golden_md, new_md, ident, options=None):
    # pylint: disable=unused-argument

    if golden_md is None and new_md is None:
        return 0

    found_diff = 0

    if len(list(golden_md.keys())) != len(list(new_md.keys())):
        print('Difference in %s metadata key count' % ident)
        print('  Golden Keys: ' + str(list(golden_md.keys())))
        print('  New Keys: ' + str(list(new_md.keys())))
        found_diff += 1

    for key in list(golden_md.keys()):
        if key not in new_md:
            print('New %s metadata lacks key \"%s\"' % (ident, key))
            found_diff += 1
        elif new_md[key] != golden_md[key]:
            print('Metadata value difference for key "' + key + '"')
            print('  Golden: "' + golden_md[key] + '"')
            print('  New:    "' + new_md[key] + '"')
            found_diff += 1

    return found_diff


#######################################################
# Review and report on the actual image pixels that differ.
def compare_image_pixels(golden_band, new_band, ident, options=None):
    # pylint: disable=unused-argument

    diff_count = 0
    max_diff = 0

    for line in range(golden_band.YSize):
        golden_line = golden_band.ReadAsArray(0, line, golden_band.XSize, 1)[0]
        new_line = new_band.ReadAsArray(0, line, golden_band.XSize, 1)[0]
        diff_line = golden_line.astype(float) - new_line.astype(float)
        max_diff = max(max_diff, abs(diff_line).max())
        diff_count += len(diff_line.nonzero()[0])

    print('  Pixels Differing: ' + str(diff_count))
    print('  Maximum Pixel Difference: ' + str(max_diff))

#######################################################


def compare_band(golden_band, new_band, ident, options=None):
    found_diff = 0

    options = [] if options is None else options

    if golden_band.DataType != new_band.DataType:
        print('Band %s pixel types differ.' % ident)
        print('  Golden: ' + gdal.GetDataTypeName(golden_band.DataType))
        print('  New:    ' + gdal.GetDataTypeName(new_band.DataType))
        found_diff += 1

    if golden_band.GetNoDataValue() != new_band.GetNoDataValue():
        print('Band %s nodata values differ.' % ident)
        print('  Golden: ' + str(golden_band.GetNoDataValue()))
        print('  New:    ' + str(new_band.GetNoDataValue()))
        found_diff += 1

    if golden_band.GetColorInterpretation() != new_band.GetColorInterpretation():
        print('Band %s color interpretation values differ.' % ident)
        print('  Golden: ' + gdal.GetColorInterpretationName(golden_band.GetColorInterpretation()))
        print('  New:    ' + gdal.GetColorInterpretationName(new_band.GetColorInterpretation()))
        found_diff += 1

    if golden_band.Checksum() != new_band.Checksum():
        print('Band %s checksum difference:' % ident)
        print('  Golden: ' + str(golden_band.Checksum()))
        print('  New:    ' + str(new_band.Checksum()))
        found_diff += 1
        compare_image_pixels(golden_band, new_band, ident, options)

    # Check overviews
    if golden_band.GetOverviewCount() != new_band.GetOverviewCount():
        print('Band %s overview count difference:' % ident)
        print('  Golden: ' + str(golden_band.GetOverviewCount()))
        print('  New:    ' + str(new_band.GetOverviewCount()))
        found_diff += 1
    else:
        for i in range(golden_band.GetOverviewCount()):
            found_diff += compare_band(golden_band.GetOverview(i),
                                       new_band.GetOverview(i),
                                       ident + ' overview ' + str(i),
                                       options)

    # Metadata
    if 'SKIP_METADATA' not in options:
        found_diff += compare_metadata(golden_band.GetMetadata(),
                                       new_band.GetMetadata(),
                                       'Band ' + ident, options)

xuebingbing's avatar
xuebingbing committed
138 139 140 141 142 143 144 145 146 147 148 149 150
    # Mask band
    if golden_band.GetMaskFlags() != new_band.GetMaskFlags():
        print('Band %s mask flags difference:' % ident)
        print('  Golden: ' + str(golden_band.GetMaskFlags()))
        print('  New:    ' + str(new_band.GetMaskFlags()))
        found_diff += 1
    elif golden_band.GetMaskFlags() in (gdal.GMF_PER_DATASET, 0):
        found_diff += compare_band(golden_band.GetMaskBand(),
                                   new_band.GetMaskBand(),
                                   ident + ' mask band',
                                   options)

    # TODO: Color Table, gain/bias, units, blocksize, min/max
xuebingbing's avatar
xuebingbing committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

    return found_diff

#######################################################


def compare_srs(golden_wkt, new_wkt):
    if golden_wkt == new_wkt:
        return 0

    print('Difference in SRS!')

    golden_srs = osr.SpatialReference(golden_wkt)
    new_srs = osr.SpatialReference(new_wkt)

    if golden_srs.IsSame(new_srs):
        print('  * IsSame() reports them as equivalent.')
    else:
        print('  * IsSame() reports them as different.')

    print('  Golden:')
    print('  ' + golden_srs.ExportToPrettyWkt())
    print('  New:')
    print('  ' + new_srs.ExportToPrettyWkt())

    return 1

#######################################################


def compare_db(golden_db, new_db, options=None):
    found_diff = 0

    options = [] if options is None else options

    # SRS
    if 'SKIP_SRS' not in options:
        found_diff += compare_srs(golden_db.GetProjection(),
                                  new_db.GetProjection())

    # GeoTransform
    if 'SKIP_GEOTRANSFORM' not in options:
        golden_gt = golden_db.GetGeoTransform()
        new_gt = new_db.GetGeoTransform()
        if golden_gt != new_gt:
            print('GeoTransforms Differ:')
            print('  Golden: ' + str(golden_gt))
            print('  New:    ' + str(new_gt))
            found_diff += 1

    # Metadata
    if 'SKIP_METADATA' not in options:
        found_diff += compare_metadata(golden_db.GetMetadata(),
                                       new_db.GetMetadata(),
                                       'Dataset', options)

    # Bands
    if golden_db.RasterCount != new_db.RasterCount:
        print('Band count mismatch (golden=%d, new=%d)'
              % (golden_db.RasterCount, new_db.RasterCount))
        found_diff += 1

    # Dimensions
    for i in range(golden_db.RasterCount):
        gSzX = golden_db.GetRasterBand(i + 1).XSize
        nSzX = new_db.GetRasterBand(i + 1).XSize
        gSzY = golden_db.GetRasterBand(i + 1).YSize
        nSzY = new_db.GetRasterBand(i + 1).YSize

        if gSzX != nSzX or gSzY != nSzY:
            print('Band size mismatch (band=%d golden=[%d,%d], new=[%d,%d])' %
                  (i, gSzX, gSzY, nSzX, nSzY))
            found_diff += 1

    # If so-far-so-good, then compare pixels
    if found_diff == 0:
        for i in range(golden_db.RasterCount):
            found_diff += compare_band(golden_db.GetRasterBand(i + 1),
                                       new_db.GetRasterBand(i + 1),
                                       str(i + 1),
                                       options)

    return found_diff

#######################################################


def compare_sds(golden_db, new_db, options=None):
    found_diff = 0

    options = [] if options is None else options

    golden_sds = golden_db.GetMetadata('SUBDATASETS')
    new_sds = new_db.GetMetadata('SUBDATASETS')

xuebingbing's avatar
xuebingbing committed
246
    count = len(list(golden_sds.keys())) // 2
xuebingbing's avatar
xuebingbing committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    for i in range(count):
        key = 'SUBDATASET_%d_NAME' % (i + 1)

        sub_golden_db = gdal.Open(golden_sds[key])
        sub_new_db = gdal.Open(new_sds[key])

        sds_diff = compare_db(sub_golden_db, sub_new_db, options)
        found_diff += sds_diff
        if sds_diff > 0:
            print('%d differences found between:\n  %s\n  %s'
                  % (sds_diff, golden_sds[key], new_sds[key]))

    return found_diff

#######################################################


def Usage():
    print('Usage: gdalcompare.py [-sds] <golden_file> <new_file>')
    sys.exit(1)

#######################################################
#
# Mainline
#


if __name__ == '__main__':

    # Default GDAL argument parsing.
    argv = gdal.GeneralCmdLineProcessor(sys.argv)
    if argv is None:
        sys.exit(0)

    if len(argv) == 1:
        Usage()

    # Script argument parsing.
    golden_file = None
    new_file = None
    check_sds = 0

    i = 1
    while i < len(argv):

        if argv[i] == '-sds':
            check_sds = 1

        elif golden_file is None:
            golden_file = argv[i]

        elif new_file is None:
            new_file = argv[i]

        else:
            print('Urecognised argument: ' + argv[i])
            Usage()

        i = i + 1
        # next argument

    # Compare Files

    found_diff = 0

    # compare raw binary files.
    try:
        os.stat(golden_file)

        if not filecmp.cmp(golden_file, new_file):
            print('Files differ at the binary level.')
            found_diff += 1
    except OSError:
        print('Skipped binary file comparison, golden file not in filesystem.')

    # compare as GDAL Datasets.
    golden_db = gdal.Open(golden_file)
    new_db = gdal.Open(new_file)
    found_diff += compare_db(golden_db, new_db)

    if check_sds:
        found_diff += compare_sds(golden_db, new_db)

    print('Differences Found: ' + str(found_diff))

    sys.exit(found_diff)